我创建了一个将创建比萨对象的类。它考虑到它的尺寸(以英寸为单位的直径),切片的数量,馅饼的成本以及披萨的类型。为对象创建类
这是我第一次做这样的事情,所以我遇到了一些混乱。
下面是类披萨的代码:
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;
}
}
下面是在披萨类调用主方法的代码:
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());
有关如何解决此问题的任何输入?感谢您帮助开始的程序员!
都属于同一包中的“Pizza”和“PizzaTest”类? – codeMan