-2

さらに別のプロジェクトの質問...次のようなBookOrderクラスがあります...

import java.text.DecimalFormat;

public class BookOrder
{
    private String author;      
    private String title;
    private int quantity;
    private double costPerBook;
    private String orderDate;
    private double weight;
    private char type;      //R,O,F,U,N

    public BookOrder (String author, String title)
    {
    }

    public BookOrder(String author, String title, int quanitity, double costPerBook, String orderDate, double weight, char type)
    {
    }

    public BookOrder(BookOrder bookOrder)
    {
    }

    public void setAuthor(String author)
    {
        this.author= author;
    }

    public void setTitle(String title)
    {
        this.title= title;
    }

    public void setQuantity(int quantity)
    {
        if (quantity >=0)
            this.quantity= quantity;
    }

    public void setCostPerBook(double costPerBook)
    {
        if (costPerBook >= 0)
            this.costPerBook= costPerBook;
    }

    public void setOrderDate(String orderDate)
    {
        this.orderDate= orderDate;
    }

    public void setWeight(double weight)
    {
        if (weight >=0)
            this.weight=weight;
    }

    public void setType(char type)
    {
        if (type=='r' || type=='R' || type=='o' || type=='O' || type=='f' || type=='F' || type=='u'|| type=='U'|| type=='n'|| type=='N')
        {   
            if(type < 97)       // all values below 97 are caps
                this.type = type;
            else    
                this.type = ((String.valueOf(type)).toUpperCase()).charAt(0);       // had to research, but converted char to a string, capitalized it, then converted back to char.
        }
        else
            this.type= 'N';
    }

    public void assignValues(int quantity, double costPerBook, double weight, char type)
    {
    }


    public String getAuthor()
    {
        String strAuthor;
        strAuthor=this.author;
        return strAuthor;
    }   //end Public String getAuthor

    public String getTitle()
    {
        String strTitle;
        strTitle=this.title;
        return strTitle;
    }

    public int getQuantity()
    {
        int iQuant;
        iQuant=this.quantity;
        return iQuant;
    }

    public double getCostPerBook()
    {
        double dCostPerBook;
        dCostPerBook=this.costPerBook;
        return dCostPerBook;
    }

    public String getOrderDate()
    {
        String strOrderDate;
        strOrderDate=this.orderDate;
        return strOrderDate;
    }

    public double getWeight()
    {
        double dWeight;
        dWeight=this.weight;
        return dWeight;
    }

    public char getType()
    {
        char cType;
        cType=this.type;
        return cType;
    }

    public void adjustQuantity(int adjustingQuantity)
    { 
        int iNewQuantity = (this.quantity + adjustingQuantity);
        if (iNewQuantity >= 0)
            this.quantity = iNewQuantity;
        else
            this.quantity = 0;
    }

    public double totalWeight()
    {
        double dTotalWeight;
        dTotalWeight= (this.quantity * this.weight);
        return dTotalWeight;
    }

    public double calcCost()
    {
    double dCalculatedCost;
    dCalculatedCost= (this.quantity * this.costPerBook);
    return dCalculatedCost;
    }

    public double shipping()
    {
        double dShipping;
        double dShippingCost;

        if(this.type=='R')
        {
            dShippingCost= 0.30;
            dShipping=(dShippingCost * (this.quantity * this.weight));
        }
        else if (this.type=='O')
        {
            dShippingCost= 0.50;
            dShipping=(dShippingCost *(this.quantity * this.weight));
        }
        else if (this.type=='P')
        {
            dShippingCost= 0.10;
            dShipping=(dShippingCost * (this.quantity * this.weight));
        }
        else if (this.type=='F')
        {
            dShippingCost=0.25;
            dShipping=(dShippingCost * (this.quantity * this.weight));
        }
        else if (this.type=='U')
        {
            dShippingCost=0.30;
            dShipping=(dShippingCost * (this.quantity * this.weight));
        }
        else
        {
            dShippingCost= .05;
            dShipping=(dShippingCost * (this.quantity * this.weight));
        }
        return dShipping;
    }

    public double totalCost()       //calcCost+shipping
    {
        double dTotalCost;
        dTotalCost= (calcCost() + shipping());
        return dTotalCost;
    }

    public String invoice()
    {
        DecimalFormat df= new DecimalFormat ("$#,#00.00");
        String strInvoice;

        strInvoice="\n Author: " + this.author;
        strInvoice +="\n Title: " + this.title;
        strInvoice +="\n Quantity: " + this.quantity;
        strInvoice +="\n Cost Per Book: " + df.format(this.costPerBook);
        strInvoice +="\n Order Date: " + this.orderDate;
        strInvoice +="\n Total Weight: " + totalWeight();
        strInvoice +="\n Shipping Type: " + this.type;
        strInvoice +="\n Shipping Cost: " + df.format(shipping());
        strInvoice +="\n Total Cost: " + df.format(totalCost());

        return strInvoice;
    }
}

問題なくコンパイルできました。それから私はメインメソッドを実行していました...そして入れました

BookOrder BookOrder1 = new BookOrder("Jonathan", "Book1", 12, 6.75, "11/5/2013", 8.75, 'r');
System.out.print(""+ BookOrder1.invoice());

それでも、結果を出力するたびに、すべての結果が null またはゼロでした。これがなぜなのか誰にも分かりますか?簡単なことだと思いますが、何かわかりません... }

4

4 に答える 4

2

コンストラクターで何も初期化していないため、クラス内のすべての変数に対してデフォルト値が使用されます。したがって、コンストラクターを次のように変更できます。

private String author;      
private String title;
private int quantity;
private double costPerBook;
private String orderDate;
private double weight;
private char type;      //R,O,F,U,N

public BookOrder(String author, String title, int quanitity, double costPerBook, String orderDate, double weight, char type)
{
  this.author = author; 
  this.title =title;
  this.quanitity =quanitity;

  // others also
}
于 2013-11-06T05:38:11.330 に答える
1

コンストラクターがインスタンス変数に値を割り当てていないためです。したがって、デフォルトでは、すべての参照変数が に割り当てられ、nullプリミティブがなどに割り当てられます。00.0

public BookOrder(String author, String title, int quanitity, 
                  double costPerBook, String orderDate, double weight, char type)
{
   // empty body
}

コンストラクターの呼び出し中に渡されたパラメーターを、次のようなインスタンス変数に割り当てる必要があります。

public BookOrder(String author, String title, int quanitity, 
                  double costPerBook, String orderDate, double weight, char type)
{
   this.author = author;
   ..............
}
于 2013-11-06T05:37:58.780 に答える
0

コンストラクター内で値を割り当てる必要があります

コンストラクタを変更する必要があります

    public BookOrder(String author, String title, int quanitity, double costPerBook, String orderDate, double weight, char type)
    {

    }

これが好き

    public BookOrder(String author, String title, int quanitity, double costPerBook, String orderDate, double weight, char type)
    {
           this.author =author;      
           this.title =title;
           this.quantity=quantity;
           this.costPerBook=costPerBook;
           this.orderDate=orderDate;
           this.weight=weight;
           this.type =type ;

    }
于 2013-11-06T05:41:42.763 に答える
0

コンストラクターを初期化する

インスタンス変数の数量に対するコンストラクタパラメータの数量(スペルミス)に注意してください(害はありません)。

以下のコードを使用できます。

public BookOrder(String author, String title, int quanitity, double costPerBook, String orderDate, double weight, char type)
{
    this.author=author;
    this.title=title;
    this.quantity=quanitity;
    this.costPerBook=costPerBook;
    this.orderDate=orderDate;
    this.weight=weight;
    this.type=type;
}
于 2013-11-06T05:40:11.430 に答える