gpt4 book ai didi

java - 为对象创建类

转载 作者:太空宇宙 更新时间:2023-11-04 07:12:10 26 4
gpt4 key购买 nike

我正在创建一个类来创建一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com