Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Object Oriented Programming (java) Wild Kingdom The program that you are to writ

ID: 3751939 • Letter: O

Question

Object Oriented Programming (java)

Wild Kingdom

The program that you are to write is designed to determine how long it takes for various animals to travel different distances. A number of classes will be used. The following diagram describes an inheritance hierarchy of animals. # indicates a protected field.

abstract Animal

# String kind

# double airSpeed

# double landSpeed

# boolean flies

+ abstract double FastTime (double distance)

+ void print ()

Mammal   Bird

+ Mammal ()   + Bird ()

+ Time FastTime (double distance) + Time FastTime (double distance)

Cow   Hawk

+ Cow () +Hawk ()

Bat

Bat   Penguin

+ Bat () + Penguin ()

+ Time FastTime (double distance) + Time FastTime (double distance)

Some comments about each class:

Animal (abstract class):

kind is the name of the animal

airSpeed and landSpeed are the speed in miles per hour that an animal can move through the air and on the groun

flies is true if the animal can fly

The FastTime is an abstract method that returns the amount of time in hours the animal will take to cover the given distance.

The print method prints the four protected data items, one per line, with labels. yes/no is indicated for flying.

Mammal:

The constructor notes that the animal cannot fly

The FastTime method is defined to give the time that the animal will take to make the trip on the ground

Bird:

The constructor notes that the animal can fly

The FastTime method is defined to give the time that the animal will take to make the trip through the air.

Cow:

The constructor indicates that the animal is a “Cow”, travels 5 mph on land and 0 mph through the air.

Hawk:

The constructor indicates that the animal is a “Hawk”, travels 2 mph on land and 40 mph through the air.

Bat:

The constructor indicates that the animal is a “Bat”, can fly, travels 1 mph on land and 22 mph through the air.

The FastTime method is overridden to give the time that the bat will take to make the trip through the air.

Penguin:

The constructor indicates that the animal is a “Penguin”, cannot fly, travels 1.5 mph on land and 0 mph through the air.

The FastTime method is defined to give the time that the penguin will take to make the trip on the ground.

Write a main application that repeatedly asks the user for an animal and a trip and, for each, prints the attributes of the animal and the length of time needed for the trip. You will declare an Animal object and use an appropriate constructor to “cast” it as a given animal. Travel time should be printed with two digits after the decimal. Here is a sample run (user input in bold):

Enter an animal (or quit): penguin

Enter a distance to travel: 5

Kind:       Penguin

Air Speed: 0.0

Land Speed: 1.5

Can Fly:    No

Time for trip: 3.33 hours

Enter an animal (or quit): cow

Enter a distance to travel: 7

Kind:       Cow

Air Speed: 0.0

Land Speed: 5.0

Can Fly:    No

Time for trip: 1.4 hours

Enter an animal (or quit): bat

Enter a distance to travel: 4

Kind:       Bat

Air Speed: 22.0

Land Speed: 1.0

Can Fly:    Yes

Time for trip: 0.18 hours

Enter an animal (or quit): hawk

Enter a distance to travel: 22

Kind:       Hawk

Air Speed: 40.0

Land Speed: 2.0

Can Fly:    Yes

Time for trip: 0.55 hours

Enter an animal (or quit): quit

Explanation / Answer

import java.text.DecimalFormat;
import java.util.Scanner;

abstract class Animal
{
protected String kind;
protected double airSpeed;
protected double landSpeed;
protected boolean flies;
  
public abstract double FastTime(double distance);
public void print()
{
System.out.println("Kind: " + kind);
System.out.println("Air Speed: " + airSpeed + " mph");
System.out.println("Land Speed: " + landSpeed + " mph");
if(flies)
System.out.println("Can Fly: Yes");
else
System.out.println("Can Fly: No");
}
}

class Mammal extends Animal
{
  
Mammal()
{
flies = false;
}
  
@Override
public double FastTime(double distance)
{
return (distance / landSpeed);
}
}

class Bird extends Animal
{
  
Bird()
{
flies = true;
}
  
@Override
public double FastTime(double distance)
{
return (distance / airSpeed);
}
}

class Cow extends Animal
{
Cow()
{
kind = "Cow";
airSpeed = 0;
landSpeed = 5;
flies = false;
}
@Override
public double FastTime(double distance)
{
return (distance / landSpeed);
}
}

class Hawk extends Animal
{
Hawk()
{
kind = "Hawk";
airSpeed = 40;
landSpeed = 2;
flies = true;
}
@Override
public double FastTime(double distance)
{
return (distance / (airSpeed + landSpeed));
}
}

class Bat extends Animal
{
Bat()
{
kind = "Bat";
airSpeed = 22;
landSpeed = 1;
flies = true;
}
@Override
public double FastTime(double distance)
{
return (distance / (airSpeed + landSpeed));
}
}

class Penguin extends Animal
{
Penguin()
{
kind = "Penguin";
airSpeed = 0;
landSpeed = 1.5;
flies = false;
}
@Override
public double FastTime(double distance)
{
return (distance / landSpeed);
}
}
  
public class WildKingdom {
  
public static void main(String[] args) {
String user_input = "";
double distance = 0.0D;
double timeTaken = 0.0D;
do
{
System.out.print("Enter an animal (or quit): ");
Scanner sc = new Scanner(System.in);
user_input = sc.next().toLowerCase();
if(user_input.equalsIgnoreCase("quit")) break;
System.out.print("Enter a distance to travel: ");
distance = sc.nextDouble();
DecimalFormat df = new DecimalFormat("#.##");
  
switch(user_input)
{
case "cow":
Cow cow = new Cow();
cow.print();
timeTaken = cow.FastTime(distance);
Math.floor(timeTaken);
System.out.println("Time for trip: " + df.format(timeTaken) + " hours ");
break;
case "hawk":
Hawk hawk = new Hawk();
hawk.print();
timeTaken = hawk.FastTime(distance);
Math.floor(timeTaken);
System.out.println("Time for trip: " + df.format(timeTaken) + " hours ");
break;
case "bat":
Bat bat = new Bat();
bat.print();
timeTaken = bat.FastTime(distance);
Math.floor(timeTaken);
System.out.println("Time for trip: " + df.format(timeTaken) + " hours ");
break;
case "penguin":
Penguin penguin = new Penguin();
penguin.print();
timeTaken = penguin.FastTime(distance);
Math.floor(timeTaken);
System.out.println("Time for trip: " + df.format(timeTaken) + " hours ");
break;
default:
System.out.println("Animal not found!");
break;
}
}while(true);   
}
  
}