Using Java define an interface called Relatable. It should have one abstract met
ID: 644158 • Letter: U
Question
Using Java define an interface called Relatable. It should have one abstract method called .isGreatThan() that takes another Relatable as an arguement and returns a boolean. It's supposed to be a method that returns true if the given Relatable is greater than the arguement. But note that this is an abstract method, so you won't be implementing it in the Relatable interface definition, just giving the method header.
Creat a class called RelatableTools. This class will only have static methods for use with Relatables. Add to RelatableTools a static method called .greatestOf(). This method should take two Relatables and return the greater of the two, as defined by .isGreaterThan(). (You can assume that if the first one isn't greater than the second, then the second one is greater.)
Next, add to RelatableTools a second definition of .greatestOf() (overloading the first) that takes an array of Relatables and returns the greatest one in the array.
Explanation / Answer
//interface Relatable
interface Relatable{
boolean isGreatThan(Relatable relatable);//abstract method
}
//class relatable tool
class RelatableTool{
//function to check and return the greater value
public static Relatable greatestOf(Relatable relatable1,Relatable relatable2){
if(!relatable1.isGreatThan(relatable2)){
return relatable2;
}
return relatable1;
}
//function to check and return the greatest value from the array
public static Relatable greatestOf(Relatable[]relatables){
Relatable greatest=relatables[0];
for(Relatable relatable:relatables){
if(!greatest.isGreatThan(relatable)){
greatest=relatable;
}
}
return greatest;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.