java - 为对象创建类

标签 java class object parameters double

我正在创建一个类来创建一个 Pizza 对象。它考虑了它的大小(直径以英寸为单位)、切片数量、馅饼的成本以及披萨的类型。

这是我第一次做这样的事情,所以我遇到了一些麻烦。

这是 Pizza 类的代码:

    class Pizza
{

    //instance variables
    int size;
    int slices;
    int pieCost;
    String typeOfPizza;


    //constructors
    Pizza (String typeOfPizza)
    {
        System.out.println (typeOfPizza);
    }

    Pizza ()
    {
        System.out.println ("pizza");
    }

    Pizza (int s, int sl, int c)
    {
        size = s;
        slices = sl;
        pieCost = c;
        typeOfPizza = "????";
    }

    Pizza (String name, int s, int sl, int c)
    {
        typeOfPizza = name;
        size = s;
        slices = sl;
        pieCost = c;
    }

    //behavior

    double areaPerSlice(int size, int slices)
    {
        double wholeArea = Math.PI * Math.pow ((size/2), 2);
        double sliceArea = wholeArea/slices;
        return sliceArea;
    }

    double costPerSlice (double pieCost, int slices)
    {
        double sliceCost = pieCost/slices; 
        return sliceCost;
    }

    double costPerSquareInch (double sliceCost, double sliceArea)
    {
        double costPerSquareInch = sliceCost/sliceArea;
    }

    String getName(String name)
    {
        String typeOfPizza = name;
        return typeOfPizza;
    }
}

以下是调用 Pizza 类的 main 方法的代码:

class PizzaTest
{
    public static void main (String [] args)
    {
        String typeOfPizza = "Cheese";
        int size = 10;                  //in inches, referring to the diameter of the pizza
        int numberOfSlices = 10;            //number of slices
        int costOfPie = 20;

        Pizza myPizza = new Pizza (typeOfPizza, size, numberOfSlices, costOfPie);

        System.out.printf ("Your %s pizza has %.2f square inches per slice.\n", myPizza.getName(),
                   myPizza.areaPerSlice() );

        System.out.printf ("One slice costs $%.2f, which comes to $%.3f per square inch.\n", 
                   myPizza.costPerSlice(), myPizza.costPerSquareInch());
    }
}

本质上,输出应打印以下内容:

您的意大利辣香肠披萨每片有 20.11 平方英寸。 一片的成本为 1.05 美元,即每平方英寸 0.052 美元。

这些值可以忽略,它们来自具有不同参数的示例。当我编译这个程序时,出现以下错误:

getName(java.lang.String) in Pizza cannot be applied to ()
        System.out.printf ("Your %s pizza has %.2f square inches per slice.\n", myPizza.getName(),
                                                                                       ^
PizzaTest.java:20: areaPerSlice(int,int) in Pizza cannot be applied to ()
                   myPizza.areaPerSlice() );
                          ^
PizzaTest.java:23: costPerSlice(double,int) in Pizza cannot be applied to ()
                   myPizza.costPerSlice(), myPizza.costPerSquareInch());
                          ^
PizzaTest.java:23: costPerSquareInch(double,double) in Pizza cannot be applied to ()
                   myPizza.costPerSlice(), myPizza.costPerSquareInch());

有任何关于如何解决这个问题的意见吗?感谢您对新手程序员的帮助!

最佳答案

改变

String getName(String name)
{
    String typeOfPizza = name;
    return typeOfPizza;
}

String getName()
{
    return typeOfPizza;
}

double costPerSquareInch (double sliceCost, double sliceArea){
    double costPerSquareInch = sliceCost/sliceArea;
}

double costPerSquareInch (double sliceCost, double sliceArea){
    return costPerSquareInch = sliceCost/sliceArea;
}

    System.out.printf ("Your %s pizza has %.2f square inches per slice.\n", myPizza.getName(),
               myPizza.areaPerSlice() );

    System.out.printf ("One slice costs $%.2f, which comes to $%.3f per square inch.\n", 
               myPizza.costPerSlice(costOfPie,numberOfSlices), myPizza.costPerSquareInch(sliceCost,sliceArea));

    System.out.printf ("Your %s pizza has %.2f square inches per slice.\n", myPizza.getName(),
               myPizza.areaPerSlice(size, numberOfSlices) );

    System.out.printf ("One slice costs $%.2f, which comes to $%.3f per square inch.\n", 
               myPizza.costPerSlice(costOfPie,numberOfSlices), myPizza.costPerSquareInch(sliceCost,sliceArea));

关于java - 为对象创建类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20486708/

相关文章:

java - 在单击位置绘制新圆圈,同时保存以前的圆圈

java - 在java中构建测试程序

c# - 从另一个类文件访问 MainForm

javascript - 无法在 JavaScript 中查看对象的内容

object - 属性错误: module 'numpy' has no attribute 'object'

java - 在 Java 中验证电子邮件

java - 如何找到特定类型的 toString() 方法的隐式用法

java - 检查类是否存在于 Java 类路径中而不运行其静态初始化程序?

c++ - 在构造函数中接受某些类型

php - 结果在 MySQL 前端给出 undefined