In: Computer Science
import javax.swing.JOptionPane;
public class Animal {
private int numTeeth = 0;
private boolean spots = false;
public int weight = 0;
public Animal(int numTeeth, boolean spots, int
weight){
this.numTeeth =numTeeth;
this.spots = spots;
this.weight =weight;
}
public int getNumTeeth(){
return numTeeth;
}
public void setNumTeeth(int numTeeth) {
this.numTeeth = numTeeth;
}
public boolean getSpots() {
return spots;
}
public void setSpots(boolean spots) {
this.spots = spots;
}
public int getWeight() {
weight =
Integer.parseInt(JOptionPane.showInputDialog("Enter the lion's
weight "));
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
----
Follow these steps:
● Modify the existing Animal.java file for this task.
● Using the Lion class template as shown in the pdf file, expand the class to have features specific to a lion.
● Add a field for lion type. ● Add a method in this class which sets the lion type based on it’s weight (note that the weight is a derived field from the superclass).
● If they weight is less than 80kg, it’s type should be a cub. If less than 120kg, it should be female. And if greater than 120kg.
● Print out the type of lion.
● Compile, save and run your file.
● Modify the existing Animal.java file for this task.
● Create a class called ‘Cheetah’ that:
○ Inherits from the Animal class.
○ Makes use of at least one static field which needs to have a static setter and getter.
○ Contains a constructor.
○ Contains a toString() method.
○ Has an array as one of it’s fields.
● Create an application class, and within it create a Cheetah object and print it out with the main method.
● Compile, save and run your file.
Animal.java
public class Animal {
// instance variables
private int numTeeth = 0;
private boolean spots = false;
private int weight = 0;
private String lionType;
// parameterized constructor
public Animal(int numTeeth, boolean spots, int weight)
{
this.numTeeth = numTeeth;
this.spots = spots;
this.weight = weight;
if(this.weight<80)
lionType="Cub";
else
if(this.weight<120)
lionType="Female";
else
lionType="Male";
}
// setter and getter methods
public String getLionType() {
return lionType;
}
public void setLionType() {
if(weight<80)
lionType="Cub";
else
if(weight<120)
lionType="Female";
else
lionType="Male";
}
public int getNumTeeth(){
return numTeeth;
}
public void setNumTeeth(int numTeeth) {
this.numTeeth = numTeeth;
}
public boolean getSpots() {
return spots;
}
public void setSpots(boolean spots) {
this.spots = spots;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
}
Cheetah.java
public class Cheetah extends Animal {
// static variable
private static int speedMPH;
// parameterized constructor
public Cheetah(int numTeeth, boolean spots, int
weight,int speed) {
// calls super class
constructor
super(numTeeth, spots,
weight);
speedMPH=speed;
}
// setter and getter methods
public static int getSpeedMPH() {
return speedMPH;
}
public static void setSpeedMPH(int speedMPH)
{
Cheetah.speedMPH = speedMPH;
}
@Override
public String toString() {
return "*** Cheetah ***"
+"\nNumber of teeth: "+getNumTeeth()
+"\nSpots: "+getSpots()
+"\nWeight: "+getWeight()
+"\nLion type: "+getLionType()
+"\nSpeed: "+getSpeedMPH()+"mph";
}
}
Application.java
public class Application {
public static void main(String[] args) {
// create Cheetah class
object
Cheetah cheetah=new Cheetah(24,
true, 145, 240);
// print object using toString()
method
System.out.println(cheetah);
}
}
Output