Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Java. Animal class import java.util.Calendar; // used for computing current year /** Basic animal class for COMP1006/1406 tutorial. Example abstract class for tutorials in COMP

Java. image text in transcribed
Animal class
import java.util.Calendar; // used for computing current year
/**
Basic animal class for COMP1006/1406 tutorial.

Example abstract class for tutorials in COMP 1006/1406.
Students will complete the toString() class (overriding
the method inherited from Object) and write subclasses that define the abstract method noise().
@author m. jason hinek
@version 1.0
*/
public abstract class Animal{
/** internal storage of animal's name

private/protected attributes and methods may or may not have javadoc
documentation with them. If the javadoc is the API (interface) for the
class, then users of the class should not know about these attributes and
methods. It goes against the notion of information hiding
if we make them visible like this.
*/
protected String name;
/** internal storage of the animal's birth year */
protected int birthYear;
/** Getter for an animal's name
@return the name of this animal
*/
public String getName(){ return this.name; }
/** Getter for an animal's year of birth
@return the birth year of this animal
*/
public int getBirthyear(){ return this.birthYear; }
/**
Initializes an animal with the given name and birth year.
@param name is the animal's name
@param year is the animal's year of birth
*/
public Animal(String name, int year){
this.name = name;
this.birthYear = year;
}
/**
A string representation of the noise an animal makes.

The method must be overriden by child classes.
@return a string representation of the noise an amimal makes
*/
public abstract String noise();
/* override Object's toString method here */
@Override
public String toString(){
return "empty";
}
}
Cat class
public class Cat extends Animal{
}
Dog class
public class Dog extends Animal{
}
Animal App class
import java.util.Random;
import java.util.Calendar;
/** COMP 1006/1406 Tutorial Material

Testing program for the Animal, Cat and Dog classes.
@version 1.1
*/
public class AnimalApp{
/** number of animals in collection */
public static final int SIZE = 5;
/** a collection of names for generating random animals */
public static String[] names = {"Fluffy", "Tiger", "Spot",
"Bubbles", "Dodger", "Ace", "Flower", "Tiny", "Pip"};
public static void main(String[] args){
Random rnd = new Random();
/* get the current year based on computer's clock */
Calendar now = Calendar.getInstance();
int year = now.get(Calendar.YEAR);
System.out.println("The year is " + year);
/* some animals */
Animal[] animals = new Animal[SIZE];
/* randonly pick a cat or dog */
for(int i=0; i
if(Math.random()
animals[i] = new Cat( names[rnd.nextInt(names.length)], rnd.nextInt(12) + 2);
}else{
animals[i] = new Dog( names[rnd.nextInt(names.length)], rnd.nextInt(14) + 2);
}
}
for(Animal animal: animals){
System.out.print(animal + " ... ");
System.out.println(animal.noise());
}
}
}
Animal Consider the Animal class provided. t is an abstract class to model animals. Read the class and see w hat it provides. Override Object's tostring) method in the Aninal 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.Calendar.getInstance) int year now.get (java.util.Calendar.YEAR): Cat and Dog The cat and og 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 either "meow" or "pri (randomly chosen each time the noise method is called), and the noise a dog makes should be either 'w oof or "grr (randomty chosen each time it's noise method is called). AnimalApp The AntmalApp program generates an array of animals and calls the noise method of each. You can use this to test your Anima Cat and Dog classes. Owl Create an oul class that extends the Aninal class. The class should have a three-argument constructor that takes a string (name), an integer (birth year) and a boolean (that determines if it is a wis t or not). The string representation of an owl should include its name, age and w hether or not i is wise or not. An ow ts noise is hooo0. Modify the AninalApp program so that some owl objects are also created

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 24th International Conference Dexa 2013 Prague Czech Republic August 2013 Proceedings Part 2 Lncs 8056

Authors: Hendrik Decker ,Lenka Lhotska ,Sebastian Link ,Josef Basl ,A Min Tjoa

2013th Edition

3642401724, 978-3642401725

More Books

Students also viewed these Databases questions

Question

=+4. How have regulators helped you within the last three years?

Answered: 1 week ago

Question

Draw the structure of 2-methylbutane

Answered: 1 week ago