2013-12-10 148 views
1

我創建了一個將創建比薩對象的類。它考慮到它的尺寸(以英寸爲單位的直徑),切片的數量,餡餅的成本以及披薩的類型。爲對象創建類

這是我第一次做這樣的事情,所以我遇到了一些混亂。

下面是類披薩的代碼:

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()); 

有關如何解決此問題的任何輸入?感謝您幫助開始的程序員!

+0

都屬於同一包中的「Pizza」和「PizzaTest」類? – codeMan

回答

0

您的函數採用非可選字符串。

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

應該是兩個功能

String getName() { // needed a string before. 
    return typeOfPizza; 
} 

void setName(String name) { 
    typeOfPizza = name; 
} 

另外,你或許應該稱之爲場name或這些方法(S)應getTypeOfPizzasetTypeOfPizza

0

我不認爲你的Pizza類會編譯。

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

首先讓那一個正確。

然後

myPizza.getName() // need input argument as String 

那麼這應該是

myPizza.getName("inputString"); 

還有另外兩個具有相同的輸入參數的問題。

myPizza.areaPerSlice()// two int input arguments. 

更正爲

myPizza.areaPerSlice(1,2); 

下一個

myPizza.costPerSlice();// double and int input arguments. 

正確的,因爲

myPizza.costPerSlice(2.0,2); 

最後

myPizza.costPerSquareInch() // two double input arguments. 

更正爲

myPizza.costPerSquareInch(2.0,1.0) ; 
0

錯誤說這一切......

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

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

,你正在服用2名ARGS ...但同時呼籲您使用0參數。 ..這適用於其他錯誤也。

0
problem 

你的功能要求一個parameter.As你沒有功能getNamesingle parameter

Solution 

你必須定義像

String getName() 
{ 
    return typeOfPizza; 
} 

,你還是要的方法也爲這些電話做。

myPizza.areaPerSlice() 
myPizza.costPerSlice() 
myPizza.costPerSquareInch() 
1

變化

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)); 
0
public class Pizza 
{ 

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


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

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

public Pizza (int size, int slices, int pieCost) 
{ 
    this.size = size; 
    this.slices = slices; 
    this.pieCost = pieCost; 
    typeOfPizza = "????"; 
} 

public Pizza (String typeOfPizza, int size, int slices, int pieCost) 
{ 
    this.typeOfPizza = typeOfPizza 
    this.size = size; 
    this.slices = slices; 
    this.pieCost = pieCost; 
} 

//behavior 

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

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

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

public 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"+myPizza.getName()+" pizza has "+myPizza.areaPerSlice() +".2f square inches per slice.\n"); 

    System.Out.Printf ("One slice costs "+myPizza.costPerSlice()+".2f, which comes to "+myPizza.costPerSquareInch()+".3f per square inch.\n"); 
} 
} 

希望這會幫助你。