Java: An interface Terrestrial. It contains - String walk An interface class Aqu
ID: 3711689 • Letter: J
Question
Java:
An interface Terrestrial. It contains - String walk An interface class Aquatic. It contains: - String swim) An abstract class Animal. It contains public String sound I/set the default sounds to "No Sound" A concrete class Cougar extends Animal implements Terrestrial -Override walk) -Override sound ) Override toString O://call the walk) and sound ) methods A concrete class Fish extends Animal implements Aquatic Uses default sound. Override swim) Override toString //call the swim) methods A concrete class Turtle extends Animal implements Terrestrial, Aquatic -Override walk) Override swim) -Override sound ) Override toString //call the walk),swim), and sound) methods Driver program ArrayList of Animals: Cougar, Fish, and Turtle - Print all of the objects using the toString) methodExplanation / Answer
Terrestrial.java
package animal.details;
public interface Terrestrial { //Terrestrial interface according to the given description
String walk(); //incomplete method walk() which will be completed in the class that will inherit it.
}
Aquatic.java
package animal.details;
public interface Aquatic { //Aquatic interface according to the given description
String swim(); //incomplete method swim() which will be completed in the class that will inherit it.
}
Animal.java
package animal.details;
public abstract class Animal { //Abstract class according to the given description
public String sound(){ //Sound() method which returns default value
return null;
}
}
Cougar.java
package animal.details;
public class Cougar extends Animal implements Terrestrial{
@Override
public String walk() {
return "Walks..."; //returns that Cougar can walk
// TODO Auto-generated method stub
//return null;
}
@Override
public String sound() {
return "Rawr!"; //returns the sound of Cougar
// TODO Auto-generated method stub
//return null;
}
@Override
public String toString() {
String w=walk(); //Reading the values from overridden walk() method
String s=sound(); //Reading the values from overridden sound() method
return " Cougar "+w+" "+"Sound:"+s; //Formatting the read values into proper format
// TODO Auto-generated method stub
//return null;
}
}
Turtle.java
package animal.details;
public class Turtle extends Animal implements Terrestrial, Aquatic{
@Override //Annotation is used to check if the method is getting overridden or not
public String swim() {
return "swims..."; //returns that Turtle can swim
// TODO Auto-generated method stub
//return null;
}
@Override
public String walk() {
return "walks..."; //returns that Turtle can walk
// TODO Auto-generated method stub
//return null;
}
@Override
public String sound() {
return "ehhhhhh..."; //returns the sound of Turtle
// TODO Auto-generated method stub
//return null;
}
@Override
public String toString() {
String w=walk(); //Reading the values from overridden walk() method
String s=swim(); //Reading the values from overridden swim() method
String so=sound(); //Reading the values from overridden sound() method
return " Turtle "+s+" Turtle "+w+" Sound: "+so; //Formatting the read values into proper format
// TODO Auto-generated method stub
//return null;
}
}
Fish.java
package animal.details;
public class Fish extends Animal implements Aquatic{
@Override //Annotation is used to check if the method is getting overridden or not
public String swim() {
return "Swims..."; //returns that Fish can swim
// TODO Auto-generated method stub
//return null;
}
@Override
public String toString() {
String s=swim(); //Reading the values from overridden swim() method
String so=sound(); //Reading the values from overridden sound() method
if(so==null){ //if sound is null then print No Sound
so="No Sound";
}
return " Fish "+s+" "+"Sound:"+so; //Formatting the read values into proper format
// TODO Auto-generated method stub
//return null;
}
}
DriverProg.java
package animal.details;
import java.util.ArrayList;
public class driverProg {
public static void main(String[] args) { //execution starts from main() method
ArrayList<String> a=new ArrayList<String>(); //creating an ArrayList of type String
a.add("Cougar"); //Adding elements into the ArrayList using the add() method of ArrayList
a.add("Fish");
a.add("Turtle");
Turtle t=new Turtle(); //creating reference variable for class Turtle
System.out.println(t.toString()); //calling to.String of Turtle class and displaying its contents
Fish f=new Fish(); //creating reference variable for class Fish
System.out.println(f.toString()); //calling to.String of Fish class and displaying its contents
Cougar c=new Cougar(); //creating reference variable for class Cougar
System.out.println(c.toString()); //calling to.String of Cougar class and displaying its contents
} //end of main()
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.