Question

In: Computer Science

Classwork Java Program A zoo is developing an educational safari game with various animals. You will...

Classwork

Java Program

A zoo is developing an educational safari game with various animals. You will write classes representing Elephants, Camels, and Moose.  

Part A

Think through common characteristics of animals, and write a class ZooAnimal.

ZooAnimal should have at least two instance variables of different types (protected), representing characteristics that all animals have values for.  

It should also have at least two methods for behaviors that all animals do; these can simply print out a String stating what the animal is doing. (accessors and mutators are not behaviors).

Include at least one parameterized constructor.

Part B

Create classes Elephant, Camel, and Moose, all of which extend ZooAnimal. In each, include a default constructor that calls the parameterized superclass constructor.  

To one class (of Elephant, Camel, and Moose), add another instance variable specific to that type of animal. To another, add another behavior method. In a third, override a behavior from ZooAnimal.   

Part C

Create a test harness for the animal classes. In a main create at least one Elephant, Camel, and Moose, and show which behavior methods are usable for each.

Create another animal class inheriting from one of your three types (doesn't have to be zoologically accurate, but should make some sense), and either add an instance variable or add or override a method. This class should also be tested in the harness.

Solutions

Expert Solution

Here is the code for above problem statement along with outout screenshot. Please comment if need any further clarifications. Also do upvote if i answered your question.

-----------------------------------ZooAnimal----------------------------------

/**
* The Class ZooAnimal.
*/
public class ZooAnimal {
  
   /** The weight. */
   protected double weight;

   /** The category. */
   protected String category;

   /**
   * Instantiates a new zoo animal.
   *
   * @param weight the weight
   * @param category the category
   */
   public ZooAnimal(double weight, String category) {
       this.weight = weight;
       this.category = category;
   }

   /**
   * Eat.
   */
   public void eat() {
       System.out.println(this.category + " is eating");
   }

   /**
   * Walk.
   */
   public void walk() {
       System.out.println(this.category + " is walking");
   }

   /**
   * Gets the weight.
   *
   * @return the weight
   */
   public double getWeight() {
       return weight;
   }

   /**
   * Sets the weight.
   *
   * @param weight the new weight
   */
   public void setWeight(double weight) {
       this.weight = weight;
   }

   /**
   * Gets the category.
   *
   * @return the category
   */
   public String getCategory() {
       return category;
   }

   /**
   * Sets the category.
   *
   * @param category the new category
   */
   public void setCategory(String category) {
       this.category = category;
   }

}

-----------------------------------Elephant---------------------------------------

/**
* The Class Elephant.
*/
public class Elephant extends ZooAnimal {

   /**
   * Instantiates a new elephant.
   */
   public Elephant() {
       super(12, "Elephant");
   }
  
   public Elephant(String elephantType) {
       super(12, elephantType);
   }

   /**
   * Drink.
   */
   public void drink() {
       System.out.println("Elephant is drinking");
   }
}

------------------------------------Camel----------------------------------------

/**
* The Class Camel.
*/
public class Camel extends ZooAnimal {
  
   /** The camel type. */
   private String camelType;

   /**
   * Instantiates a new camel.
   */
   public Camel() {
       super(10, "Camel");
   }

   /**
   * Gets the camel type.
   *
   * @return the camel type
   */
   public String getCamelType() {
       return camelType;
   }

   /**
   * Sets the camel type.
   *
   * @param camelType the new camel type
   */
   public void setCamelType(String camelType) {
       this.camelType = camelType;
   }

}

-------------------------------------Moose--------------------------------------

/**
* The Class Moose.
*/
public class Moose extends ZooAnimal {
  
   /**
   * Instantiates a new moose.
   */
   public Moose() {
       super(15, "Moose");
   }

   /**
   * Walk.
   */
   public void walk() {
       System.out.println("Overridden : Moose is walking");
   }
}

----------------------------------AfricanElephant------------------------------

/**
* The Class AfricanElephant.
*/
public class AfricanElephant extends Elephant {
  
   public AfricanElephant() {
       super("African Elephant");
   }

   /**
   * Drink.
   */
   public void drink() {
       System.out.println("African Elephant is drinking");
   }
}

----------------------------------TestClassZoo------------------------------

/**
* The Class TestClassZoo.
*/
public class TestClassZoo {

   /**
   * The main method.
   *
   * @param args the arguments
   */
   public static void main(String[] args) {
       Elephant elephant = new Elephant();
       elephant.eat();
       elephant.walk();
       elephant.drink();
       AfricanElephant africanElephant = new AfricanElephant();
       africanElephant.eat();
       africanElephant.walk();
       africanElephant.drink();
       Camel camel = new Camel();
       camel.eat();
       camel.walk();
       Moose moose = new Moose();
       moose.eat();
       moose.walk();
   }

}

----------------------------------------------OUTPUT-------------------------------------------------


Related Solutions

Java Project Requirements: 1.Write a Java program that plays a word game with a user. The...
Java Project Requirements: 1.Write a Java program that plays a word game with a user. The program asks the user questions and then creates a paragraph using the user’s answers. 2.The program must perform the following: a.Uses a Scanner object to ask the user: (The program asks for no other information) i.Full Name (First and Last name only) - stores this Full Name in one String object variable. ii.Age – must be read in as an int. iii.Profession or expected...
Create a simple dice game in Java. Add screenshots and the program here.
Create a simple dice game in Java. Add screenshots and the program here.
write on eclipse java Write a program named lab5 that will play a game of Blackjack...
write on eclipse java Write a program named lab5 that will play a game of Blackjack between the user and the computer. Create a second class named Card with the following: Define the following private instance variables: cardValue (int) & cardSuite (String) Write a constructor with no parameters that will Set cardValue to a random number between 1 and 13 Generate a second random number between 0 and 3 and assign cardSuite a string based on its value (0 –...
One file java program that will simulate a game of Rock, Paper, Scissors. One of the...
One file java program that will simulate a game of Rock, Paper, Scissors. One of the two players will be the computer. The program will start by asking how many winning rounds are needed to win the game. Each round will consist of you asking the user to pick between rock, paper, and scissors. Internally you will get the computers choice by using a random number generator. Rock beats Scissors, Paper beats Rock, and Scissors beats Paper. You will report...
Write a Java program that lets the user play a game where they must guess a...
Write a Java program that lets the user play a game where they must guess a number between zero and one hundred (0-100). The program should generate a random number between 0 and 100 and then the user will try to guess the number. The program should help the user guess the number (see details below). The program must allow the user five attempts to guess the number. Further Instruction: (a) Declare a variable diff and assign to it the...
Create a java program. War is a card game for two players. A standard deck of...
Create a java program. War is a card game for two players. A standard deck of 52 cards is dealt so that both players have 26 cards. During each round of play (or "battle"), both players play a card from the top of their hand face up. The player who plays the card of the higher rank wins both cards and places them at the bottom of his stack of cards. If both cards played are of the same rank,...
Write a guessing game java program (basic program, we didn't study further than loops and files)...
Write a guessing game java program (basic program, we didn't study further than loops and files) where the user has to guess a secret number between 1 and 100. After every guess the program tells the user whether their number was too large or too small. At the end the number of tries needed should be printed. It counts only as one try if they input the same number multiple times consecutively.Example of running this program: Enter your secret number...
In this python program , you are to build a trivia game. The game should present...
In this python program , you are to build a trivia game. The game should present each question – either in order or randomly – to the player, and display up to four possible answers. The player is to input what they believe to be the correct answer.   The game will tell the player if they got it right or wrong and will display their score. If they got it right, their score will go up. If they got it...
Java program that uses binary tree and recursions to implement Tower of Hanoi game where there...
Java program that uses binary tree and recursions to implement Tower of Hanoi game where there can be any amount of disks and there are either 3,4, or 5 pegs to store the disks(# of pegs and disks is manually inputted by user), in Tower of Hanoi game, the object of the game is move all the pieces from the 1st peg to the right most peg or the opposite side. You can place disks on top of each other...
JAVA 1) You are provided an implementation of a text-based adventure game. Currently the game responds...
JAVA 1) You are provided an implementation of a text-based adventure game. Currently the game responds to directional movements that allow you to move your adventurer around a maze (that is read from a text file). Add the ability to save (and load) the game using object serialization. Write the serialization of the file out to a default filename SavedGame.dat. Make sure that you take care of any exceptions that may occur when reading or writing to a file (for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT