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

Problem specifications: You are tasked with developing a complete temperature conversion program in Java. Your program...
Problem specifications: You are tasked with developing a complete temperature conversion program in Java. Your program should: -prompt the user with the following menu: -1-Convert Fahrenheit to Celcius -2-Convert Celcius to Fahrenheit -3-Quit Program The menu must be coded in its own method. -If user select option 1 then call a method that prompts the user to enter a value in degrees Fahrenheit and return it back to main. -call a method that converts from Fahrenheit to Celcius -call a...
Develop a Java program for the question below: A running race among animals: In a race...
Develop a Java program for the question below: A running race among animals: In a race among animals different types of animals are differenciated wih their running styles. In each iteration of the race, each participating animal makes a move acording to its style of running. Total length of the racecourt is NTotal (store this variable properly in the test class). Determine the winner animal and completion time of the race using polymorphism. Each of these animal classes extends from...
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...
Write a Java program that plays the game Rock, Paper, Scissors. The program should generate a...
Write a Java program that plays the game Rock, Paper, Scissors. The program should generate a random choice (Rock, Paper or Scissors) then ask the user to choose Rock, Paper or Scissors. After that the program will display its choice and a message showing if the player won, lost or tied. Next, the program should prompt the user to play again or not. Once the player selects to stop playing the game, the program should print the number of wins,...
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.
JAVA PROGRAM, Create the game "Rock, Scissor, Paper": Make the AI pick randomly. You need to...
JAVA PROGRAM, Create the game "Rock, Scissor, Paper": Make the AI pick randomly. You need to look up Random numbers in Java. First ask the user what language they want to play in. Choice 1 is English, but choice 2 can be whatever real language you want. Your first loop is making sure they enter good input. If there is a tie you don't give them the option to play again. They have to. Outer loop runs until they say...
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,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT