创建一个接受接口对象数组的方法

时间:2015-12-07 06:33:20

标签: java arrays object

所以,我有一个两个问题。我做的第一部分是创建一个接口,然后在两个不同的类中使用该接口。然而,问题的第二部分让我难过。我不理解它的逻辑方面。感觉问题顺序应该是相反的。这是完成作业最后一部分所需要做的两件事。如果这里有人能给我一些关于我应该采取的方向的微小指导,我会非常感激。

第二部分: (1)在main中创建一个Calculatable对象数组,并调用sumCalculate方法。

  

在大多数情况下我理解这一点,我已经开始这样做了,但决定开始研究(2),因为这对我来说更难。它基本上创建了一个特定大小的可计算对象数组(我选择了5个)并用不同的可计算对象填充它(在我的例子中可以是矩形或正方形)。但是这个问题的后半部分让我感到困惑?我是否正在调用我将要在问题2中制作的sumCalculate方法?或者我甚至在制作(2)方法之前调用它。

(2)创建一个接受可计算对象数组的方法,并总结每个对象调用计算所返回的值。

  

我想在这里弄清楚问题(2)是这个。当它要求我制作方法时?这是否意味着我在名为sumCalc的接口中创建了一个新方法,例如,它具有接受可计算对象数组的参数?然后总结返回的值。我假设我要添加计算方法返回的计算双。

我会问我的教授,但我决定采取的这位教授已经养成了一种难以接触的习惯。很抱歉打扰你们,这很可能是一个基本而不困难的问题。

interface Calculatable {

   public double calculate(int x);



}

class square implements Calculatable {

   public double side;


   square(double side){
      this.side = side;
   }

   public double getside(){
      return side;
   }

   public double calculate(int x) {
      double perimeter = side * 4;
      System.out.println("This calculate method will output the perimeter divided by the parameter x");
      double calculation = perimeter / x;
      System.out.println("The original perimeter was " + perimeter + ". And the calculated perimeter is " + calculation +".");
      return calculation; 
   }


}

public class rectangle implements Calculatable {

   public double length;
   public double width;

   rectangle(double length , double width){

      this.length = length;
      this.width = width;
   }

   public double getlength(){
      return length;
   }
   public double getwidth(){
      return width;
   }

   public double calculate(int x) {
      double perimeter = 2 * (length + width);
      double calculation = 2 * perimeter;
      System.out.println("This will return the perimeter of the rectangle times the x paramater");
      System.out.println("Your current perimeter is " + perimeter + " and your perimeter after the calculation is " + calculation + ".");
      return calculation;

   }


   public static void main(String [] args){

      Calculatable perimeter1 = new rectangle(20.5 , 50);
      perimeter1.calculate(5);
      Calculatable perimeter2 = new square(10.5);
      perimeter2.calculate(2);
      Calculatable[] perimetersums = new Calculatable[5];
      perimetersums[0] = new rectangle(20.5 , 50);
      perimetersums[1] = new rectangle(10 , 25);


   }
}

4 个答案:

答案 0 :(得分:1)

我会在main方法旁边创建sumCalculate并结束它。课程是实现一个接口方法并使用它。

除此之外,我建议阅读Java命名约定并相应地更正代码。

答案 1 :(得分:1)

我理解它的方式你不应该改变界面,特别是如果提供了界面的话!

只需在您的sumCalculate方法下面写下main,就像这样

private static double sumCalculate(Calculateable[] c) {
    // do your sum up and return the result
}

并在您的主要方法中调用它

double sum = sumCalculate(perimetersums);

答案 2 :(得分:0)

在我的脑海中几乎没有混淆..在实现矩形中,你没有在calculate方法中使用x。我改变了整个班级结构..请看..我认为它会帮助你...

public interface Calculatable {

     public double calculate(int x);

     public double getPerimeter();

} 

public class square implements Calculatable {

    public double side;
    private double perimeter;


       square(double side){
          this.side = side;
       }

       public double getside(){
          return side;
       }

       public double calculate(int x) {
          double perimeter = side * 4;
          System.out.println("This calculate method will output the perimeter divided by the parameter x");
          double calculation = perimeter / x;
          System.out.println("The original perimeter was " + perimeter + ". And the calculated perimeter is " + calculation +".");
          this.perimeter=calculation;
          return calculation; 
       }

    @Override
    public double getPerimeter() {
        // TODO Auto-generated method stub
        return perimeter;
    }
}


public class rectangle implements Calculatable {

       public double length;
       public double width;

       private double perimeter;

       rectangle(double length , double width){

          this.length = length;
          this.width = width;
       }

       public double getlength(){
          return length;
       }
       public double getwidth(){
          return width;
       }

       public double calculate(int x) {
          double perimeter = 2 * (length + width);
          double calculation = 2 * perimeter;
          System.out.println("This will return the perimeter of the rectangle times the x paramater");
          System.out.println("Your current perimeter is " + perimeter + " and your perimeter after the calculation is " + calculation + ".");
          this.perimeter=calculation;
          return calculation;
       }

    @Override
    public double getPerimeter() {
        // TODO Auto-generated method stub
        return perimeter;
    }


}



public class MainMethod {

    public static void main(String[] args) {

         Calculatable perimeter1 = new rectangle(20.5 , 50);
          perimeter1.calculate(5);
          Calculatable perimeter2 = new square(10.5);
          perimeter2.calculate(2);
          Calculatable[] perimetersums = new Calculatable[5];
          perimetersums[0] = perimeter1; //new rectangle(20.5 , 50);
          perimetersums[1] = perimeter2;// new rectangle(10 , 25);

          System.out.println("the sum is= "+sumCalculate(perimetersums));
    }


    private static double sumCalculate ( Calculatable[] perimetersums)
    {
        double sum=0.0;
        for(int i=0;i<perimetersums.length;i++)
        {
            Calculatable cal=perimetersums[i];
            if(cal!=null)
             sum=sum+cal.getPerimeter();
        }
        return sum;
    }

}

答案 3 :(得分:0)

我改变了类结构......

public interface Calculatable {

     public double calculate();
}



public class square implements Calculatable {

    private final int x=2;
    public double side;

       square(double side){
          this.side = side;
       }

       public double getside(){
          return side;
       }

       public double calculate() {
          double perimeter = side * 4;
          System.out.println("This calculate method will output the perimeter divided by the parameter x");
          double calculation = perimeter / x;
          System.out.println("The original perimeter was " + perimeter + ". And the calculated perimeter is " + calculation +".");
          return calculation; 
       }


}

public class rectangle implements Calculatable {

       private final int x=5;
       public double length;
       public double width;


       rectangle(double length , double width){

          this.length = length;
          this.width = width;
       }

       public double getlength(){
          return length;
       }
       public double getwidth(){
          return width;
       }

       public double calculate() {
          double perimeter = 2 * (length + width);
          double calculation = 2 * perimeter;
          System.out.println("This will return the perimeter of the rectangle times the x paramater");
          System.out.println("Your current perimeter is " + perimeter + " and your perimeter after the calculation is " + calculation + ".");
          return calculation;
       }


}

public class MainMethod {

    public static void main(String[] args) {

         Calculatable perimeter1 = new rectangle(20.5 , 50);
         // perimeter1.calculate(5);
          Calculatable perimeter2 = new square(10.5);
          //perimeter2.calculate(2);
          Calculatable[] perimetersums = new Calculatable[5];
          perimetersums[0] = new rectangle(20.5 , 50);
          perimetersums[1] = new rectangle(10 , 25);

          System.out.println("the sum is= "+sumCalculate(perimetersums));
    }


    private static double sumCalculate ( Calculatable[] perimetersums)
    {
        double sum=0.0;
        for(int i=0;i<perimetersums.length;i++)
        {
            Calculatable cal=perimetersums[i];
            if(cal!=null)
            {
                sum=sum+cal.calculate();
            }
        }
        return sum;
    }

}