类 和对象。计算价格时得到 0.0 作为答案。 - JAVA

Classes and Objects. Getting 0.0 as answer when calculating price. - JAVA

我正在从实验表中解决一个问题,但当 运行 程序时我只得到 0.0 作为答案。我找不到问题,请帮忙。

问题:
实施具有属性直径(以厘米为单位)、cost_sq_cm(每平方厘米成本)和面积的 class 披萨。它的方法是:
• 用于创建具有给定直径和给定 price_sq_cm 的 Pizza 类型对象的构造函数。
• 直径和 cost_sq_cm 的增变器和访问器方法。
• calcArea 用于计算给定比萨饼的面积。
• getPrice 计算和 return 披萨的价格。

使用 main 方法编写一个 class TestPizza,该方法声明一个 Pizza 类型的对象,该对象具有用户输入的直径和用户输入的 cost_sq_cm 圆形比萨饼,并显示该比萨饼的价格披萨。

披萨class:

package Number3;

public class Pizza {
private int diameter;
private float cost_sq_cm;
private double area;
private double price;

public Pizza() //default constructor
{
    diameter = 0;
    cost_sq_cm = 0;
    area = 0;
    price = 0;
}

public Pizza(int d,float cost,double a,double p) //overloaded constructor
{
    d = diameter;
    cost = cost_sq_cm;
    a = area;
    p = price;  
}

public void Constructor() //method
{
    Pizza P = new Pizza();
}

public void setDiameter(int d) //mutator
{
    d = diameter;
}

public int getDiameter() //accessor
{
    return diameter;
}

public void setCost(float c)
{
    c = cost_sq_cm;
}

public float getCost()
{
    return cost_sq_cm;
}

public double calcArea()
{
    area = 3.142 * (diameter * diameter);
    return area;
}

public double getPrice()
{ 
    price = area * cost_sq_cm;
    return price;
}

public void display()
{
    System.out.print("The area is: "+this.price);
}
}

测试披萨:

package Number3;
import java.util.Scanner;

public class TestPizza {


public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    float area = 0;
    Pizza P = new Pizza();

    int d; float c,a = 0;
    System.out.print("Enter a value for the diameter: ");
    d = input.nextInt();
    P.setDiameter(d);

    System.out.print("Enter a value for the cost: ");
    c = input.nextFloat();
    P.setCost(c);

    P.display();    
}

}

我是 JAVA 的新手。请宽容

而不是:

System.out.print("The area is: "+this.price);

使用:

System.out.print("The area is: "+this.getPrice());

你还需要计算面积。所以在你的主要方法中这样调用它:

P.calcArea();//to calculate area

您在调用 new Pizza() 时将价格初始化为 0,而您从未调用计算价格的 getPrice。

同时将您的 setter 费用更改为:

public void setCost(float c) {
   c = cost_sq_cm;
}

public void setCost(float c) {
    cost_sq_cm = c;
}

您应该将每平方厘米的成本乘以面积来得出价格。如果其中一个等于零,您将得到零。我看到你在哪里设置了直径,但没有设置面积。

你设置了直径,但是你设置的时候没有计算面积

public void setDiameter(int d) //mutator; lose this comment.  worthless clutter.
{
    d = diameter;
    area = calcArea();
}

我建议遵循 Java 习语。不要写 display() 方法;最好覆盖 toString().

我会这样写:

package cruft;

import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * Pizza
 * @author Michael
 * @link 
 * @since 2/22/2015 12:27 PM
 */
public class Pizza {

    private static final int DEFAULT_DIAMETER = 38;
    private static final double DEFAULT_COST = 15.0;
    private static final double DEFAULT_COST_PER_AREA = 0.013226;     // 15 euro for a 38 cm diameter pizza
    private static final NumberFormat DEFAULT_FORMAT = new DecimalFormat("#.####");

    private final int diameter;
    private final double costPerArea;
    private final double price;

    public static void main(String[] args) {
        int diameter = ((args.length > 0) ? Integer.valueOf(args[0]) : DEFAULT_DIAMETER);
        double costPerArea = ((args.length > 1) ? Double.valueOf(args[1]) : DEFAULT_COST_PER_AREA);
        Pizza pizza = new Pizza(diameter, costPerArea);
        System.out.println(pizza);
    }

    public Pizza(int diameter, double costPerArea) {
        if (diameter <= 0) throw new IllegalArgumentException("diameter must be positive");
        if (costPerArea <= 0) throw new IllegalArgumentException("cost per area must be positive");
        this.diameter = diameter;
        this.costPerArea = costPerArea;
        this.price = this.costPerArea*this.calculateArea();
    }

    public double getPrice() {
        return price;
    }

    private double calculateArea() {
        return Math.PI*this.diameter*this.diameter/4.0;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("Pizza{");
        sb.append("diameter=").append(diameter);
        sb.append(", costPerArea=").append(DEFAULT_FORMAT.format(costPerArea));
        sb.append(", price=").append(NumberFormat.getCurrencyInstance().format(getPrice()));
        sb.append('}');
        return sb.toString();
    }
}

设置字段或其他值是

variable = value;

所以

diameter = d;

看来您的 setCostsetDiameter 方法需要更改,

来自

d = diameter;

this.diameter = d;