Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using Java please help me design Write an interface called Calculatable that has

ID: 3843968 • Letter: U

Question

Using Java please help me design

Write an interface called Calculatable that has a method called calculate that accepts an integer as a parameter and returns a double based on some calculations that are done with data members and the integer parameter. public interface Calculatable {double calculate (int i);} Define at least two classes that implement Calculatable and have different calculate methods. Create objects an call calculate on each one. Print out the output of these method calls. Create an array of Calculatable objects in main and call the sumCalculate method with the array and an integer of your choice. You can define the array as follows Calculatable [] calcob; Make a method called su accepts that an array of Calculatable objects and an integer parameter x. It should return the sum of the values that are returned by each object's call to its calculate method with the same parameter x. Realize that even though it's not possible to make instances of an interface (like Calculatable), it is possible to have a reference of type Calculatable and then assign an object to the reference. Of course, the object must be of a type that implements the Calculatable interface. Exercise (not graded) Make your Item class implement Comparable and then have the ShoppingCart keep the items in sorted order using the Arrays.sort method.

Explanation / Answer

PART I:

PROGRAM CODE:

import java.util.*;
import java.lang.*;
import java.io.*;

public interface Calculatable
{
   double calculate(int i);  
}

public class Addition implements Calculatable
{
   private int counter;
   public Addition(int a)
   {
       counter = a;
   }
  
   public double calculate(int i)
   {
       return counter+i;
   }
}

public class Subtraction implements Calculatable
{
   private int counter;
public Subtraction(int a)
   {
       counter = a;
   }
  
   public double calculate(int i)
   {
       return counter-i;
   }
}
public class Tester
{
   public static void main (String[] args) throws java.lang.Exception
   {
       Calculatable add = new Addition(100);
       Calculatable sub = new Subtraction(100);
       System.out.println("After adding 50: " + add.calculate(50));
       System.out.println("After subtracting 50: " + sub.calculate(50));
   }
}

OUTPUT:

PART II:

PROGRAM CODE:

import java.util.*;
import java.lang.*;
import java.io.*;

public interface Calculatable
{
   double calculate(int i);  
}

public class Addition implements Calculatable
{
   private int counter;
   public Addition(int a)
   {
       counter = a;
   }
  
   public double calculate(int i)
   {
       return counter+i;
   }
}

public class Subtraction implements Calculatable
{
   private int counter;
public Subtraction(int a)
   {
       counter = a;
   }
  
   public double calculate(int i)
   {
       return counter-i;
   }
}
public class Tester
{
  
   public static double sumCalculate(Calculatable calcOb[], int x)
   {
       double sum = 0;
       for(int i=0; i<calcOb.length; i++)
           sum += calcOb[i].calculate(x);
       return sum;
   }
   public static void main (String[] args) throws java.lang.Exception
   {
       Calculatable calcObj[] = new Calculatable[5];
       for(int i=0; i<calcObj.length; i++)
           calcObj[i] = new Addition(i+1);
       System.out.println("The total sum is " + sumCalculate(calcObj, 100));
   }
}

OUTPUT:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote