Question

In: Computer Science

import javax.swing.JOptionPane; public class Animal {    private int numTeeth = 0;    private boolean spots...

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.

Solutions

Expert Solution

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


Related Solutions

With the code that is being tested is: import java.util.Random; public class GVdate { private int...
With the code that is being tested is: import java.util.Random; public class GVdate { private int month; private int day; private int year; private final int MONTH = 1; private final int DAY = 9; private static Random rand = new Random(); /** * Constructor for objects of class GVDate */ public GVdate() { this.month = rand.nextInt ( MONTH) + 1; this.day = rand.nextInt ( DAY );    } public int getMonth() {return this.month; } public int getDay() {return this.day;...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first...
import java.util.Scanner; public class CompareNums { private static String comparison( int first, int second){ if (first < second) return "less than"; else if (first == second) return "equal to"; else return "greater than";       }    // DO NOT MODIFY main! public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter first integer: "); int first = input.nextInt(); System.out.print("Enter second integer: "); int second = input.nextInt(); System.out.println("The first integer is " + comparison(first, second) + " the...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
import java.util.NoSuchElementException; public class ArrayBasedDeque<AnyType> implements deque<AnyType> {       private static int MAX_SIZE = 5;...
import java.util.NoSuchElementException; public class ArrayBasedDeque<AnyType> implements deque<AnyType> {       private static int MAX_SIZE = 5; // initial array size    //DO NOT CHANGE this, it is set to 5 to make sure your code    //will pass all the tests and works with no issue.       // add all data fields which are required    /**    * ArrayBasedDeque() constructs an empty deque.    */    public ArrayBasedDeque(){        //complete    }       /**    *...
import java.util.*; class A { int i, j, k; public A(int i, int j, int k)...
import java.util.*; class A { int i, j, k; public A(int i, int j, int k) { this.i=i; this.j=j; this.k=k; } public String toString() { return "A("+i+","+j+","+k+")"; } } class Main { public static void main(String[] args) { ArrayList<A> aL=new ArrayList<A>(); Random rand= new Random(1000); //1000 is a seed value for (int p=0; p<10; p++) { int i = rand.nextInt(100); int j = rand.nextInt(200); int k = rand.nextInt(300); aL.add(new A(i, j, k)); } System.out.println("----- Original arraylist------"); for (A a: aL)...
In the following class: public class Truth { private boolean yes_no; ... } Create constructor, setter...
In the following class: public class Truth { private boolean yes_no; ... } Create constructor, setter and getter methods, and toString method.
public class Date { private int dMonth; //variable to store the month private int dDay; //variable...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable to store the day private int dYear; //variable to store the year //Default constructor //Data members dMonth, dDay, and dYear are set to //the default values //Postcondition: dMonth = 1; dDay = 1; dYear = 1900; public Date() { dMonth = 1; dDay = 1; dYear = 1900; } //Constructor to set the date //Data members dMonth, dDay, and dYear are set //according to...
needed asap !!! 1. import java.utility.Random; 2.   3. public class Sequen 4.   5. private int stand  ...
needed asap !!! 1. import java.utility.Random; 2.   3. public class Sequen 4.   5. private int stand   6. private int calc;   7.   8. public Sequen(int numStand) 9. { 10.         stand = numStand; 11.         skip; 12.         } 13.           14.         public void skip() 15.         { 16.         Random sit = new Random(); 17.         calc = sit.nextInt(stand) + 1; 18.         } 19.           20.         public getStand() 21.         { 22.         return stand; 23.         } 24.           25.         int getCalc() 26.         { 27.         return calc; 28.         } 29.         } One error is already identified for you as shown below: Line Number: 5 Error description: Missing semicolon...
package applications; public class Matrix { private int[][] m; public Matrix(int x, int y) { m...
package applications; public class Matrix { private int[][] m; public Matrix(int x, int y) { m = new int[x][y]; } public Matrix(int x, int y, int z) { m = new int[x][y]; for(int i = 0; i < x; i++) { for(int j = 0; j < y; j++) { m[i][j] = z; } } } public int rowsum(int i) throws IndexOutOfBoundsException { if (i < 0 || i > m.length-1) { throw new IndexOutOfBoundsException("Invalid Row"); } int sum =...
Stack2540Array   import java .io .*; import java . util .*; public class Stack2540Array { int CAPACITY...
Stack2540Array   import java .io .*; import java . util .*; public class Stack2540Array { int CAPACITY = 128; int top ; String [] stack ; public Stack2540Array () { stack = new String [ CAPACITY ]; top = -1; } 1 public int size () { return top + 1; } public boolean isEmpty () { return (top == -1); } public String top () { if ( top == -1) return null ; return stack [ top ]; }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT