Java program Consider the following interface public interfa
Java program:
Consider the following interface: public interface Addable {
public Addable add(Addable x); }
Any object of a class that implements Addable has an add method that allows it to add two objects of class Addable - the current object and the object x. The current object is implementing Addable. Object x is viewed also as having type Addable. Since Addable is an interface, it means that x also implements Addable. The method returns a result of the same type as x. In other words, if x is actually of type Time discussed in class and Time implements Addable, then method add returns an object of type Time, representing the sum of the time periods corresponding to the current object and x.
Your tasks:
1) Modify the following class (available on the course website) so that it implements Addable:
import java.util.*;
public class Time implements Addable{
private int hours = 0;
private int minutes = 0;
private static int numberOfTimeObjects = 0; StringTokenizer myTokens;
public Time(String timeString){
/*
* timeString gives a time period as “n hours m minutes” with any
* number of spaces in before and after n and m.
* Examples:
* \" 2 hours
* \"3 hours
* \"2 hours */
String token;
myTokens = new StringTokenizer(timeString, \" \");
token = myTokens.nextToken(); // This gives the number of
// hours as a string hours = Integer.parseInt(token);
myTokens.nextToken();
token = myTokens.nextToken(); // This gives the number of
// minutes as a string minutes = Integer.parseInt(token);
myTokens.nextToken();
45 minutes \" 04 minutes\" 35 minutes \"
} }
numberOfTimeObjects++; }
public String toString(){ if (minutes < 10){
return hours + \":0\" + minutes ; } else {
return hours + \":\" + minutes ; }
Assume that the parameter x of method add is also of type Time. At this stage I suggest you test your definition. To do so, just comment out the last 2 executable lines of the tester code (also available on the web).
2) Define a class AddListOfAddable as follows:
a. It should contain a static method with the following method header:
public static Addable addList(Addable list[],
int numberOfElementsInList)
This method allows you to find the total of all elements in list where each
element of list implements the Addable interface.
b. This method should return an object of some class that implements
Addable. In your case you are dealing with objects of type Time. So list will be a list of objects of type Time and numberOfElementsInList gives the number of elements in list.
c. In the tester program we have the following: result = (Time) AddListOfAddable.addList(t, 3);
This means we want to find the sum of the three time periods we have in the array t. Verify that the time periods we have in the first three elements of t are as follows:
\" 2 hours \"3 hours \"2 hours
45 minutes \" 04 minutes\" 35 minutes \"
The total of these should be 8 hours and 24 minutes.
d. Make sure you get this output using the tester I have provided.
3) Finally appreciate the fact that this class AddListOfAddable give you a general purpose tool to add a list of addable objects – we don’t know what steps are actually used to determine the result returned by the add method.
Solution
Firstly Addable is a interface, so we cannot create Addable object. So the add method is not correct. In your test class you are trying to add time objects, so method is need to change a bit.
public interface Addable {
public Time add(Time x);
}
----------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.*;
public class Time implements Addable {
private int hours = 0;
private int minutes = 0;
private static int numberOfTimeObjects = 0;
StringTokenizer myTokens;
public Time(String timeString) {
String token;
myTokens = new StringTokenizer(timeString, \" \");
token = myTokens.nextToken(); // This gives the number of
// hours as a string hours = Integer.parseInt(token);
myTokens.nextToken();
token = myTokens.nextToken(); // This gives the number of
// minutes as a string minutes = Integer.parseInt(token);
myTokens.nextToken();
numberOfTimeObjects++;
}
public String toString() {
if (minutes < 10) {
return hours + \":0\" + minutes;
} else {
return hours + \":\" + minutes;
}
}
public Time add(Time x) {
int hours = this.hours + x.hours;
int minutes = this.minutes + x.minutes;
Time obj = new Time(hours+\" \"+minutes);
return obj;
}
}


