Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Refer to the Powerpoint diagram: Change the given code as follows: Add height, weight to Animal add these as doubles to class Animal ( where

Refer to the Powerpoint diagram: Change the given code as follows:
Add height, weight to Animal
add these as doubles to class Animal (where x,y are declared)
add appropriate assignments in Animal constructor for height and weight (like it does for x,y)
add height and weight to super() statement in Mammal constructor
add height and weight to super() statements in Cat and Kangaroo constructor
add height and weight parameters to animals (boots, whiskers..) created/declared in main(0
Add toString() to Animal
add a toString method to the Animal class that returns a string with the following:
name
height
weight
location (x,y formatted to 2 decimal places)
the toString method should look like:
@override
public string tostring(){
return somestring;
the string returned should look like:
Boots Weighs: 10.0 Height:1.0 is now at: (1.52,1.02)
The toString method should not print anything
For each species object in main((boots, whiskers, kanga) call its toString method:
once before the MoveAnAnimal for loop section in main():
once after the VocalizeAnAnimal section in main():
Using Cat or Kangaroo class as your template, add a (species) class Platypus, with the following:
reproduction type MONOTREME
when it vocalizes it "snuffles"
in its move() method it walks or swims
use a random number so that it walks half the time and swims the other half
it uses 4 feet whether walking or swimming
if it swims it moves half as far as it does walking
Create a Platypus class object in the AnimalKingdom class called "platty" and use it as the other
species have done:
Create it in main() as the others have (it should have name, height, weight..)
Call its toString() method where the others have
Call MoveAnAnimal() where the others have
Call VocalizeAnAnimal() where the others have
Call its toString() method again where the others have
AnimalKingdomObjectDiagram.ppt
Use the given code in:
AnimalKingdom.java
The Animal class is abstract and has Mammal class as a derived class.
The Mammal class is abstract and has Animal as its base (or super) class
The classes derived from Mammal are "species" classes such as Cat and Kangaroo. Note, there is no "species" class
in this assignment. The term "species" here is only used to refer to classes derived from the Mammal class.
The AnimalKingdom class instantiates and exercises the above classes
The MoveAnAnimal method takes an Animal object and moves it a random distance
The VocalizeAnAnimal method takes an Animal object and calls its vocalize method
There are 3 sections in main():
In the first section Cat and Kangaroo objects are created with name ("whiskers", "kanga")
The MoveAnAnimal section has a loop that moves each of the animals 3 times
The VocalizeAnAnimal section exercises the vocalize() method of each animal
You will add members and methods to the abstract class Animal as well as create another species and exercise it
accordingly. This program shows how abstract classes are used and how inheritance and polymorphism works. You
should develop this code incrementally changing one thing at a time and getting it to work and then move on to the
next thing. Do these changes in the order listed.
Java code all together ...
package com.animalkingdom;
import java.util.Random;
//------------- Animal class ---------------------------------------------
abstract class Animal {
protected String name;
protected double x;
protected double y;
// Animal constructor
public Animal(String name){
this.name = name;
this.x =0;
this.y =0;
}
abstract void vocalize();
abstract void move(double x, double y);
abstract void eat();
}
//---------------- Mammal class ---------------------------------------------
// Mammal class inherits from Animal
abstract class Mammal extends Animal {
enum REPRODUCTION_TYPE {MARSUPIAL, PLACENTAL, MONOTREME};
protected REPRODUCTION_TYPE reproductionType;
public Mammal(String name, REPRODUCTION_TYPE reproductionType){
super(name);
this.reproductionType = reproductionType;
}
@Override
public void vocalize(){
System.out.println("Mammal: "+ name +" is vocalizing.");
}
@Override
void move(double x, double y){
double oldx=this.x, oldy=this.y;
this.x += x;
this.y += y;
System.out.printf("Mammal: %s is moving was at: %02.2f %02.2f is now at: %02.2f %02.2f
", name, oldx, oldy, this.x ,this.y);
}
}
//------------------------------------------------------------------------
// a species class inherits from Mammal, this is a concrete class
class Cat extends Mammal {
public Cat(String name){
super(name, Mammal.REPRODUCTION_TYPE.PLACENTAL);
}
@Override
public void vocalize(){
System.out.println(name +" says meow!");
}
@Override
public void move(d
image text in transcribed

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

Databases On The Web Designing And Programming For Network Access

Authors: Patricia Ju

1st Edition

1558515100, 978-1558515109

More Books

Students also viewed these Databases questions

Question

=+5.14. Let f (x) be n2x or 2n -n2x or 0 according as 0 5x

Answered: 1 week ago