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;...
public class Clock { private int hr; private int min; private int sec; public Clock() {...
public class Clock { private int hr; private int min; private int sec; public Clock() { setTime(0, 0, 0); } public Clock(int hours, int minutes, int seconds) { setTime(hours, minutes, seconds); } public void setTime(int hours, int minutes, int seconds) { if (0 <= hours && hours < 24) hr = hours; else hr = 0; if (0 <= minutes && minutes < 60) min = minutes; else min = 0; if(0 <= seconds && seconds < 60) sec =...
class Counter{   private int count = 0;   public void inc(){    count++;     }   public int get(){     return...
class Counter{   private int count = 0;   public void inc(){    count++;     }   public int get(){     return count;   } } class Worker extends Thread{ Counter count;   Worker(Counter count){     this.count = count;   }   public void run(){     for (int i = 0; i < 1000;i++){       synchronized(this){         count.inc();       }}   } } public class Test {     public static void main(String args[]) throws InterruptedException   {     Counter c = new Counter();     Worker w1 = new Worker(c);     Worker w2 = new Worker(c);     w1.start();     w2.start();     w1.join();     w2.join();     System.out.println(c.get());      ...
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.awt.*; import javax.swing.JButton; import javax.swing.JFrame; public class GridBagLayoutDemo { final static boolean shouldFill = true;...
import java.awt.*; import javax.swing.JButton; import javax.swing.JFrame; public class GridBagLayoutDemo { final static boolean shouldFill = true; final static boolean shouldWeightX = true; final static boolean RIGHT_TO_LEFT = false; public static void addComponentsToPane(Container pane) { if (RIGHT_TO_LEFT) { pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); } JButton button; pane.setLayout(new GridBagLayout()); GridBagConstraints c = new GridBagConstraints(); if (shouldFill) { //natural height, maximum width c.fill = GridBagConstraints.HORIZONTAL; } button = new JButton("Button 1"); if (shouldWeightX) { c.weightx = 0.5; } c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridy = 0;...
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    }       /**    *...
public class IntNode               {            private int data;            pri
public class IntNode               {            private int data;            private IntNode link;            public IntNode(int data, IntNode link){.. }            public int     getData( )          {.. }            public IntNode getLink( )           {.. }            public void    setData(int data)     {.. }            public void    setLink(IntNode link) {.. }         } All questions are based on the above class, and the following declaration.   // Declare an empty, singly linked list with a head and a tail reference. // you need to make sure that head always points to...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int stNo,String name) {         student_Number=stNo;         student_Name=name;      }     public String getName() {       return student_Name;     }      public int getNumber() {       return student_Number;      }     public void setName(String st_name) {       student_Name = st_name;     } } Write a Tester class named StudentTester which contains the following instruction: Use the contractor to create a student object where student_Number =12567, student_Name = “Ali”. Use the setName method to change the name of...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT