Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Outlook 0010:30 PM GitHub, Inc. Animal Consider the Animal class provided. It is

ID: 3859839 • Letter: O

Question

Outlook 0010:30 PM GitHub, Inc. Animal Consider the Animal class provided. It is an abstract class to model animals. Read the class and see what it provides. Override Object's tostring) method in the Animal class so that it prints out the animals name and age. Note: assuming your computer's clock is correct, we can get the current year in Java using Java.util.calendar now = java . util.Calenda. int year - now.get (java.util.calendar.YEAR Cat and Dog The cat and Dog classes should extend the animal class. Add any needed constructors and methods to make these work Note: The noise that a cat makes should be meow or prrr (randomly chosen each time it's noise method is called), and the noise a dog makes should be woof or arrrr (randomly chosen each

Explanation / Answer

Below is your code: -

Animal.java

import java.util.Calendar;

public abstract class Animal {

protected String name;

protected int birthYear;

public String getName() {

return name;

}

public int getBirthYear() {

return birthYear;

}

public Animal(String name, int birthYear) {

this.name = name;

this.birthYear = birthYear;

}

public abstract String noise();

@Override

public String toString() {

Calendar now = Calendar.getInstance();

return getClass().getSimpleName()+" [name=" + name + ", age=" + (now.get(Calendar.YEAR) - birthYear) + "]";

}

}

Cat.java

import java.util.Random;

public class Cat extends Animal {

public Cat(String name, int birthYear) {

super(name, birthYear);

}

@Override

public String noise() {

int choice = new Random().nextInt(2);

if(choice == 0) {

return "meow";

}

return "prrr";

}

}

Dog.java

import java.util.Random;

public class Dog extends Animal {

public Dog(String name, int birthYear) {

super(name, birthYear);

}

@Override

public String noise() {

int choice = new Random().nextInt(2);

if(choice == 0) {

return "woof";

}

return "grrrr";

}

}

AnimalDriver.java

public class AnimalDriver {

public static void main(String[] args) {

Cat cat = new Cat("Billy", 2014);

Dog doggy = new Dog("Rusty", 2010);

System.out.println(cat);

System.out.println(cat.noise());

System.out.println(cat.noise());

System.out.println(doggy);

System.out.println(doggy.noise());

System.out.println(doggy.noise());

}

}

Sample run: -

Cat [name=Billy, age=3]
prrr
meow
Dog [name=Rusty, age=7]
grrrr
grrrr

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote