Write a Java program that contains a superclass of animal and the subclasses bir
ID: 3642203 • Letter: W
Question
Write a Java program that contains a superclass of animal and the subclasses bird, frog, and fish. Create a method called "move" in the superclass and then override this method in the three subclasses, so that the bird moves 10 feet, the frog moves 8 feet, and the fish moves 6 feet every second. Run the program and have each animal move for 10 seconds. Output the distance travelled, for each animal, each second.Modify the program you wrote for question 9 and add another animal called mouse. Override the move command so that the mouse can move 4 feet each second. Re-run the program as before, this time with four animals, for 10 seconds. Output the distance travelled, for each animal, each second.
Explanation / Answer
please rate package classesDemo; /** * The superclass animal * */ class Animal{ int total_distance = 0; public int move(){ return 0; } } class Bird extends Animal{ /** * Birds can move 10 feet in 1 second */ public int move(){ total_distance += 10; return total_distance; } } class Frog extends Animal{ /** * Frog can move 8 feet in 1 second */ public int move(){ total_distance += 8; return total_distance; } } class Fish extends Animal{ /** * Fish can move 6 feet in 1 second */ public int move(){ total_distance += 6; return total_distance; } } class Mouse extends Animal{ /** * Mouse can move 4 feet in 1 second */ public int move(){ total_distance += 4; return total_distance; } } public class MoveAnimals { public static void main(String args[]){ Animal myBird = new Bird(); Animal myFish = new Fish(); Animal myMouse = new Mouse(); Animal myFrog = new Frog(); System.out.println("Moving animals"); for(int i=0;iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.