Outlook 0010:30 PM GitHub, Inc. Animal Consider the Animal class provided. It is
ID: 3859840 • 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 eachExplanation / Answer
public abstract class Animal {
protected String name;
protected int birthYear;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @return the birthYear
*/
public int getBirthYear() {
return birthYear;
}
/**
* @param name
* @param birthYear
*/
public Animal(String name, int birthYear) {
this.name = name;
this.birthYear = birthYear;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "empty";
}
public abstract String noise();
}
public class Cat extends Animal {
public Cat(String name, int birthYear) {
super(name, birthYear);
// TODO Auto-generated constructor stub
}
@Override
public String noise() {
// TODO Auto-generated method stub
int r = (int) (Math.random() * 2);
if (r == 0)
return "meow";
else
return "prr";
}
/*
* (non-Javadoc)
*
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Cat [Name=" + getName() + ", Year=" + getBirthYear() + "]";
}
}
public class Dog extends Animal {
public Dog(String name, int birthYear) {
super(name, birthYear);
// TODO Auto-generated constructor stub
}
@Override
public String noise() {
// TODO Auto-generated method stub
int r = (int) (Math.random() * 2);
if (r == 0)
return "woof";
else
return "grrrr";
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "Dog [Name=" + getName() + ", Year=" + getBirthYear() + "]";
}
}
import java.util.Calendar;
import java.util.Random;
public class AnimalApp {
public static final int SIZE = 5;
public static String[] names = { "Fluffy", "Tiger", "Spot", "Bubbles",
"Dodger", "Ace", "Flower", "Tiny", "Pip" };
public static void main(String[] args) {
Random rnd = new Random();
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
System.out.println("The Year is " + year);
Animal[] animals = new Animal[SIZE];
for (int i = 0; i < SIZE; i++) {
if (Math.random() < 0.5) {
animals[i] = new Cat(names[rnd.nextInt(names.length)],
rnd.nextInt(12) + 2000);
} else {
animals[i] = new Dog(names[rnd.nextInt(names.length)],
rnd.nextInt(14) + 4000);
}
}
for (Animal animal : animals) {
System.out.print(animal + "....");
System.out.println(animal.noise());
}
}
}
OUTPUT:
The Year is 2017
Cat [Name=Bubbles, Year=2007]....prr
Cat [Name=Fluffy, Year=2010]....prr
Cat [Name=Fluffy, Year=2004]....meow
Dog [Name=Tiger, Year=4010]....grrrr
Dog [Name=Flower, Year=4000]....grrrr
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.