Question

In: Computer Science

(JAVA) 1.) Create a class called Rabbit that with 2 attributes: 1) speed and 2) color....

(JAVA)

1.) Create a class called Rabbit that with 2 attributes: 1) speed and 2) color. Then, create a constructor that has no parameters, setting the default speed to 0 and the color to “white”; this is called a default constructor. Next, create a second constructor that takes in two parameters. The second constructor should assign those parameters to the attributes. Then, in main, create two Rabbit objects. For the first Rabbit object, call the first constructor. For the second Rabbit object, call the second constructor (passing it a speed of 5 and a color of “purple”). Using the dot ‘.’ Operator, print out the first rabbit's speed and the second rabbit's color. This is a test to see if you can design constructors inside your class and initialize attributes.

Sample output

0

purple

(JAVA)

2.) Create a class called Sloth that has a weight attribute (in kg). Create a constructor for the class that takes in (as a parameter) the initial weight of the Sloth. Then, write a function called eat( ) that takes in the number of leaves the Sloth should eat. Each leaf weighs one gram, so you must increase the weight of the Sloth by one gram. According to a site dedicated to saving Sloths, they can eat between 200-500 grams of leaves per day. To finish the class out, include a showWeight() method that prints out how much the sloth weighs. In main, create a sloth object starting at 100 kilos and show the sloth's weight. Then, make the sloth eat 400, 300, and 650 leaves, showing the weight of the sloth after each time it eats. NOTE: if you’re output does not match exactly (because of the rounding of floating point math), that’s OK. For example, Java will include some extra “precision”.

Sample output

This sloth weighs 100.4 kilos

This sloth weighs 100.7 kilos

This sloth weighs 101.35 kilos

Solutions

Expert Solution

Answer:-

Hello, I have written both the Java programs. Please find it below.

1)

JAVA CODE:-(Rabbit class)

class Rabbit
{
   /* Attribute of Rabbit class*/
   int speed;
   String color;
   /* Constructors of Rabbit class*/
   Rabbit()
   {
       speed = 0;
       color = "white";
   }
   Rabbit(int speed, String color)
   {
       this.speed = speed;
       this.color = color;
   }
/* Main function starts here*/
   public static void main(String [] args)
   {
       /* Declaration of two Rabbit objects*/
       Rabbit r1 = new Rabbit();
       Rabbit r2 = new Rabbit(5,"purple");
       /* Printing the required output using dot operator*/
       System.out.println(r1.speed);
       System.out.println(r2.color);
   }
}

Screenshot of Code:-

Please refer to the screenshot of the code for properly understanding the indentation of the code.

OUTPUT:-

2)

JAVA CODE:- (Sloth class)

class Sloth
{
   /* Attribute of Sloth class*/
   double weight;
   /* Constructor of Sloth class*/
   Sloth(double weight)
   {
       this.weight = weight;
   }
   /* Implementation of eat function*/
   void eat(int leaves)
   {
weight = weight + (leaves*0.001);
   }
/* Implementation of showWeight function*/
   void showWeight()
   {
       System.out.println("This sloth weighs "+weight+" kilos");
   }
/* Main function starts here*/
   public static void main(String [] args)
   {
       /* Declaration of a Sloth object*/
       Sloth s1 = new Sloth(100);
       /* Calling eat method for different number of leaves*/
       s1.eat(400);
       s1.showWeight();
       s1.eat(300);
       s1.showWeight();
       s1.eat(650);
       s1.showWeight();
   }
}

Screenshot of Code:-

Please refer to the screenshot of the code for properly understanding the indentation of the code.

OUTPUT:-

I hope it would help.

Thanks!


Related Solutions

1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
Create a class called Dishwash with a double called CubicFeet, a string called color, and a...
Create a class called Dishwash with a double called CubicFeet, a string called color, and a method called Washing. The Washing method should return a string to the main class with the text "Now washing dishes!" In the main class create an array of DishWasher size 3. Give each DishWasher a different color and CubicFeet. Use a foreach loop to display that info, call the Washing method, and display the text returned from the Washing method call. c#
using Java Implement a class called Binomial_Coefficient o Your class has 2 int attributes, namely K...
using Java Implement a class called Binomial_Coefficient o Your class has 2 int attributes, namely K and n o Create an overloaded constructor to initialize the variables into any positive integers such that n > k > 0o Create a method that calculates the value of binomial coefficient, C(n,k) , using the following rule: ▪ For an array of size (n+1) X (k+1) ▪ Array [n][k] = Array[n-1][ k-1]+ Array[n-1] [k]▪ Array[n][0]= Array[n][n] = 1 ▪ Hence, C(n,k) = array...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Code in Java 1. Create a class Flower with data: Name, Price, Color and properly methods....
Code in Java 1. Create a class Flower with data: Name, Price, Color and properly methods. 2. Create another class named ListFlower. This class manages a collection of Flower (may be LinkedList) named a. Implementing some methods for ListFlower: Add: add new item of Flower to a Display: display all items of a sort(): sort as descending by Price and display all items of a search(Flower f): check and return whether f is exists in a or not. delete(int pos):...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the...
JAVA Programming ECLIPSE IDE 1. Create an abstract class called Book. The class should declare the following variables: an instance variable that describes the title - String an instance variable that describes the ISBN - String an instance variable that describes the publisher - String an instance variable that describes the price - double an instance variable that describes the year – integer Provide a toString() method that returns the information stored in the above variables. Create the getter and...
Please use java to answer the below question. (i) Create a class Pencil with the attributes...
Please use java to answer the below question. (i) Create a class Pencil with the attributes brand (which is a string) and length (an integer) which are used to store the brand and length of the ruler respectively. Write a constructor Pencil (String brand, int length) which assigns the brand and length appropriately. Write also the getter/setter methods of the two attributes. Save the content under an appropriate file name. Copy the content of the file as the answers. (ii)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT