0

So, I have a twoparter question. The first part I did, which was creating an interface and then utilizing that interface in two different classes. However the second part of the question has me stumped. I'm not understanding the logic aspect of it. It feels as though the question order should be in reverse. This is two things I need to do to finish up the last portion of the assignment. If anyone here could give me just some tiny guidance as to to the direction I should be taking I'd greatly appreciate it.

Part II: (1)Create an array of Calculatable objects in main and call the sumCalculate method.

For the most part I understand this, I already began doing it but decided to start working on (2) since that was way more difficult for me. It's basically creating an array of Calculatable objects of a certain size ( I chose 5) and populating it with different calculatable objects ( could be rectangles or squares in my example). But the second half of this question confuses me? Am I calling the sumCalculate method that I'm GOING to be making in question 2? Or am I calling it before I even make the (2) method.

(2)Make a method that accepts an array of Calculatable objects and sums up the values that are returned by each object's call to calculate.

What I'm trying to figure out here in question (2) is this. When it asks me to make the method? Does this mean that I'm making a new method in interface called sumCalc for example, that has parameters that accepts an array of calculatable objects? And then as far as summing up the values that are returned. I'd assume that I'd be adding the calculation double that is returned by calculate methods.

I'd ask my professor but this professor I decided to take has made it a habit of being excessively difficult to reach. Sorry to bother you guys with what is most likely an elementary and not difficult question.

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


   }
}
3
  • 2
    x is never used in your calculate method Commented Dec 7, 2015 at 6:42
  • I see that x is used in one of the cases, but it seems really odd that calculate takes a parameter. As for the questions, I get the impression, despite the confusing wording, that sumCalculate() is the method you need to implement in (2). Commented Dec 7, 2015 at 6:51
  • Yeah the professor basically wanted us to have it take a parameter for some reason. I suppose just for the sake of it. She wanted us, and this is in her own words to have a method that " returns a double based on some calculations that are done with data members and the integer parameter." Commented Dec 7, 2015 at 6:55

4 Answers 4

1

I would create sumCalculate beside main method and be over with it. Lesson is to implement an interface method and use it too.

And beside that I suggest reading Java naming convention and correcting your code accordingly.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. I was over complicating this by a long shot then lol. Once you explained this I realized how ridiculous I was making this. Thank you! Also I'm going to work on the java conventions on my own time once I'm done with this course. I haven't had much time to really hammer down the conventions with how short our deadlines are. Plus my professor never talks about the naming conventions at all T_T. Appreciate the help!
1

The way I understand it you should not change the Interface, especially if the Interface was provided to you!

Just write your sumCalculate below your main method like this

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

and call it in your main method like this

double sum = sumCalculate(perimetersums);

1 Comment

Thank you very much. I ended up doing it exactly the way you described here. I guess I was thinking too much about overusing interfaces when in reality it was way more simple than that.
0

few confusion is in my mind.. In the implementation rectangle your are not using the x in the calculate method. I changed the whole class structure a little bit.. Please look.. I think it will help you...

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;
    }

}

4 Comments

Your solution is not very good. It assumes that user calls calculate() before calling getPerimeter(), otherwise result is not right. This sort of assumptions are code smell.
@TaaviIlves... you can call the calculate method from the sumCalculate() method too..basically the calculate method signature is not perfect.. calling the calculate method from the sumCalculate() is not a grt deal..
True, that calculate signature is not very good. I would resolve this issue more OO way and introduce new class that holds Calculatable and x. Your solution is very fragile to refactoring.
@TaaviIlves i came up with the solution keeping the thing in mind that i have to do a minimum changes to existing class structure. Changing the class introducing the new class could lead to a better solution obviously. Taking the almost same class structure and giving the best solution is the task. .
0

I changed the class structure a little bit...

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;
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.