In: Computer Science
(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
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!