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

*in Java 1.Create a card class with two attributes, suit and rank. 2.In the card class,...
*in Java 1.Create a card class with two attributes, suit and rank. 2.In the card class, create several methods: a. createDeck – input what type of deck (bridge or pinochle) is to be created and output an array of either 52 or 48 cards. b.shuffleDeck – input an unshuffled deck array and return a shuffled one. + Create a swap method to help shuffle the deck c. countBridgePoints – inputs a 13-card bridge ‘hand’-array and returns the number of high-card...
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...
JAVA Program Create a class called SoccerPlayer Create 4 private attributes: First Name, Last Name, Games,...
JAVA Program Create a class called SoccerPlayer Create 4 private attributes: First Name, Last Name, Games, and Goals Have two constructors Constructor 1 – default constructor; all values to "NONE" or zero Constructor 2 – accepts input of first name, last name, games and goals. Create get and set methods for each of the four attributes Create a method the returns a double that calculates the average goals per game This method checks for zero games played: If there are...
Please Code Using Java Create a class called SoccerPlayer Create 4 private attributes: First Name, Last...
Please Code Using Java Create a class called SoccerPlayer Create 4 private attributes: First Name, Last Name, Games, and Goals Have two constructors Constructor 1 – default constructor; all values to "NONE" or zero Constructor 2 – accepts input of first name, last name, games and goals. Create get and set methods for each of the four attributes Create a method the returns a double that calculates the average goals per game This method checks for zero games played: If...
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#
Create a car class with three attributes: year, mpg, speed and list of owners. The class...
Create a car class with three attributes: year, mpg, speed and list of owners. The class also should have 2 methods called accelerate and brake. Whenever the car accelerates, its speed is increased by 30 and whenever the car brakes, its speed is reduced by 60. add a constructor to the class, which takes year, mpg, and speed as input implement a magic method that prints the car information including year, speed and mpg. That is, for a car object,...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class called aDLLNode. class aDLLNode { aDLLNode prev;    char data;    aDLLNode next; aDLLNode(char mydata) { // Constructor data = mydata; next = null;    prev = null;    } }; Step 3: In the main() function of the driver class (Lab5.5), instantiate an object of type aDLLNode and print the content of its class public static void main(String[] args) { System.out.println("-----------------------------------------");    System.out.println("--------Create...
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...
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):...
1. Create a new class called Card that contains the following attributes: [3 K] Suit (Clubs,...
1. Create a new class called Card that contains the following attributes: [3 K] Suit (Clubs, Diamonds, Spades, Hearts) Value (do not use Jack, Queen, King, Ace, instead, the value of your cards should be 1-13 where 11 = jack, 12 = Queen, 13 = King, and 1 = Ace) Colour (Red or Black) 2. Make two constructions for your card: [2 A] A default constructor for when you create an object without any arguments A constructor that has 3...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT