【问题标题】:Creating a class for an object为对象创建类
【发布时间】:2013-12-10 05:16:53
【问题描述】:

我正在创建一个将创建 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());

关于如何解决此问题的任何意见?感谢您帮助初学者!

【问题讨论】:

  • 是两个类,即PizzaPizzaTest 在同一个包中?

标签: java class object parameters double


【解决方案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));

【讨论】:

    【解决方案2】:

    您的函数采用非可选字符串。

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

    应该是两个函数

    String getName() { // needed a string before.
      return typeOfPizza;
    }
    
    void setName(String name) {
      typeOfPizza = name;
    }
    

    此外,您可能应该将该字段称为 name,或者这些方法应该是 getTypeOfPizzasetTypeOfPizza

    【讨论】:

      【解决方案3】:

      错误说明了一切......

      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 个参数...但是在调用您时使用了 0 个参数...这也适用于其他错误。

      【讨论】:

        【解决方案4】:
        problem
        

        你的函数要求一个参数。因为你没有函数 getNamesingle parameter

        Solution 
        

        你必须定义一个像

        这样的方法
        String getName( )
        {
            return typeOfPizza;
        }
        

        你也必须为这些调用做同样的事情。

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

        【讨论】:

          【解决方案5】:

          我认为您的 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) ; 
          

          【讨论】:

            【解决方案6】:
            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");
            }
            }
            

            希望这会对你有所帮助。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2015-12-30
              • 2012-11-27
              • 1970-01-01
              • 2019-08-03
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多