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)...
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...
import java.util.Scanner; class Factorial { public static int calc_factorial(int f) { int myint= 1; // put...
import java.util.Scanner; class Factorial { public static int calc_factorial(int f) { int myint= 1; // put code here return myint; } public int getInt() { int f = 0; // put code here return f; } } public class Assignment06 { public static void main(String args[]){ System.out.println("Assignmemnt 06 Written by Matt Weisfeld"); int myInt = 0; // create a Factorial object Factorial factorial = new Factorial(); // get a number from the console myInt = factorial.getInt(); System.out.println("Factorial of " +...
class A { public: //constructors // other members private: int a; int b; }; Give declatations...
class A { public: //constructors // other members private: int a; int b; }; Give declatations of operator functions for each of the following ways to overload operator + You must state where the declatation goes, whether within the class in the public or private section or outside the class. The operator + may be overloaded. a) as friend function b) as member function c) as non-friend, non-member function
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT