Question: JAVA Part I: Write an interface called Calculatable that has a method called calculate that accepts an integer as a parameter and returns a double

JAVA

Part I:

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 and call calculate on each one. Print out the output of these method calls.

Part II:

Create a public class with two methods.

A main method. 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;

A method called sumCalculate that accepts 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. double sumCalculate(Calculatable [] array, int 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 Calculatableinterface.

Exercise (not graded)

Make your Item class implement the Comparable interface and then have the ShoppingCart keep the items in sorted order using the Arrays.sort method.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!