Question

In: Computer Science

In Java Create a class called "TestZoo" that holds your main method. Write the code in...

In Java

  1. Create a class called "TestZoo" that holds your main method.
  2. Write the code in main to create a number of instances of the objects. Create a number of animals and assign a cage and a diet to each. Use a string to specify the diet.
  3. Create a zoo object and populate it with your animals. Declare the Animal object in zoo as
    Animal[] animal = new Animal[3] and add the animals into this array. Note that this zoo holds only 3 animals.
  4. Use a "for" loop to print the following:
    I am Laila the lion and I live in cage 1. I eat fresh meat.
    I am Hahn the hawk and I live in cage 2. I eat rodents.
    I am Deepak the deer and I live in cage 2. I eat fresh grass.
  5. Create a UML diagram of this zoo using boxes to represent each class : Add the relevant instance variables for each class. Indicate your associations and whether they are aggregation or composition. Indicate the one-to-many, one-to-one, etc. relationships.
  6. Instance variables would include String animalType, String animalName, String cageType, and String animalFood,

Solutions

Expert Solution

Animal.java

public class Animal {
private String animalType, animalName, cageType, animalFood;
  
public Animal()
{
this.animalType = "";
this.animalName = "";
this.cageType = "";
this.animalFood = "";
}

public Animal(String animalType, String animalName, String cageType, String animalFood)
{
this.animalType = animalType;
this.animalName = animalName;
this.cageType = cageType;
this.animalFood = animalFood;
}

public String getAnimalType() {
return animalType;
}

public void setAnimalType(String animalType) {
this.animalType = animalType;
}

public String getAnimalName() {
return animalName;
}

public void setAnimalName(String animalName) {
this.animalName = animalName;
}

public String getCageType() {
return cageType;
}

public void setCageType(String cageType) {
this.cageType = cageType;
}

public String getAnimalFood() {
return animalFood;
}

public void setAnimalFood(String animalFood) {
this.animalFood = animalFood;
}
  
@Override
public String toString()
{
return ("I am " + this.animalName + " the " + this.animalType
+ " and I live in cage " + this.cageType + ". I eat " + this.animalFood + ".");
}
}

Zoo.java

public class Zoo {
private Animal animal[];
private int size;
private static int count = 0;
  
public Zoo()
{
this.size = 3;
this.animal = new Animal[size];
}
  
public void addAnimal(Animal animal)
{
if(count == size)
throw new ArrayIndexOutOfBoundsException("Cannot add more than 3 animals!");
else
this.animal[count++] = animal;
}
  
public void printAnimalDetails()
{
for(int i = 0; i < animal.length; i++)
{
System.out.println(animal[i].toString());
}
System.out.println();
}
}

TestZoo.java

public class TestZoo {
  
public static void main(String[] args)
{
Zoo zoo = new Zoo();
try
{
Animal lion = new Animal("lion", "Laila", "1", "fresh meat");
Animal hawk = new Animal("hawk", "Hahn", "2", "rodents");
Animal deer = new Animal("deer", "Deepak", "2", "fresh grass");
  
zoo.addAnimal(lion);
zoo.addAnimal(hawk);
zoo.addAnimal(deer);
  
zoo.printAnimalDetails();
  
}catch(ArrayIndexOutOfBoundsException aio){
System.out.println(aio.getMessage());
}
}
}

**********************************************************************************************************************************************

SCREENSHOT:

UML Diagram:

Explanation: The relation between the two classes is Composition; the Zoo class is composed of the Animal class. If the Zoo does not exist, the Animals will also cease to exist. In other words, the existence of the Animal class depends on the life cycle of the Zoo class.


Related Solutions

Write in Java * Create a new client class called Plants.java * Write code in the...
Write in Java * Create a new client class called Plants.java * Write code in the Plants class that solves problems 1,2,3 and 4 * Include the solutions of the different problems in different methods and call them from the main method * Use the BagInterface.java and ArrayBag.java, but do not add any code Problem 1: Create a bag plantsCart, which holds the following spring seedlings(represented by String) Rose, Daisy, Cabbage, Cucumber, Carrot, Cucumber, Daffodil, Daisy, Rose, Iris, Rose, Spinach....
Write the code in Java: 1. Create a method that displays your name in the console....
Write the code in Java: 1. Create a method that displays your name in the console. This method is void and takes no parameters. Make an app that runs the method in response to a button press. 2. Create a version of the method in #1 that takes the text (String) to be displayed as a parameter. Allow the user to enter the text in a dialog box or text field and display that text in the console. Be sure...
Create a Project and a Class called “FinalGrade” Write a Java program to compute your final...
Create a Project and a Class called “FinalGrade” Write a Java program to compute your final grade for the course. Assume the following (you can hard-code these values in your program). Assignments are worth 30% Labs are worth 40% Mid-term is worth 15% Final is worth 15% Ask for the grades in each category. Store them in a double variable. Print out the final percentage grade.           For example, Final Grade Calculation Enter percentage grades in the following order. Assignments,...
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called...
Write a class in Java called 'RandDate' containing a method called 'getRandomDate()' that can be called without instantiating the class and returns a random Date between Jan 1, 2000 and Dec 31, 2010.
Create a new class called Account with a main method that contains the following: • A...
Create a new class called Account with a main method that contains the following: • A static variable called numAccounts, initialized to 0. • A constructor method that will add 1 to the numAccounts variable each time a new Account object is created. • A static method called getNumAccounts(). It should return numAccounts. Test the functionality in the main method of Account by creating a few Account objects, then print out the number of accounts
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class,...
Java Language -Create a project and a class with a main method, TestCollectors. -Add new class, Collector, which has an int instance variable collected, to keep track of how many of something they collected, another available, for how many of that thing exist, and a boolean completist, which is true if we want to collect every item available, or false if we don't care about having the complete set. -Add a method addToCollection. In this method, add one to collected...
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of...
Create a PoemDriver.java class with a main method. In the main method, create an ArrayList of Poem objects, read in the information from PoemInfo.txt and create Poem objects to populate the ArrayList. After all data from the file is read in and the Poem objects added to the ArrayList- print the contents of the ArrayList. Paste your PoemDriver.java text (CtrlC to copy, CtrlV to paste) into the open space before. You should not change Poem.java or PoemInfo.txt. Watch your time...
IN JAVA: Build a class called ExamPractice from scratch. This class should include a main method...
IN JAVA: Build a class called ExamPractice from scratch. This class should include a main method that does the following: Prompt the user to enter a number of inches Read the number of inches entered from the console Convert the inches into an equivalent number of miles, yards, feet, and inches. Output the results to the console. For example, if a user entered 100000 inches, the program would output: 100000 inches is equivalent to: Miles: 1 Yards: 1017 Feet: 2...
In Java. Create a class called FileSumWrapper with a method that has the signature public static...
In Java. Create a class called FileSumWrapper with a method that has the signature public static void handle(String filename, int lowerBound) Make this method call FileSum.read and make your method catch all the errors. FileSum.read is a method that takes a filename and a lower bound, then sums up all the numbers in that file that are equal to or above the given lower bound. FileSum : import java.io.File; import java.rmi.UnexpectedException; import java.util.Scanner; public class FileSum { public static int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT