In: Computer Science
I need this in Java please:
Behaviors. In the context of OOP, functions are called methods or behaviors because they typically do something. Most often, they read or change the values of one or more variables in the class. For example, you may have a weight variable in a class, and a method called gainWeight( ) that increases the weight variable by a certain amount. For this part of the lab, create class KoalaBear that has a weight attribute (in kilograms).
Create a constructor for the class that takes in (as a parameter) the initial weight of the koala bear. Then, write a function called eat( ) that takes in the number of leaves the koala bear should eat. Each leaf weighs one gram, so you must increase the weight of the koala bear by one gram. According to a site dedicated to saving Koala bears, 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 koala weighs. In main, create a koala object starting at 100 kilos and show the koala’s weight. Then, make the koala eat 400, 300, and 650 leaves, showing the weight of the koala 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 #1 This koala weighs 100.4 kilos This koala weighs 100.7 kilos This koala weighs 101.35 kilos
I have uploaded the Images of the code, Typed code and Output of the Code. I have provided explanation using comments (read them for better understanding).
Images of the Code:
Note: If the below code is missing indentation please refer
code Images
Typed Code:
// Creating a class called KoalaBear
class KoalaBear
{
// A class attribute or variable named weight
double weight;
// constructor with parameter initial_weight
KoalaBear(int initial_weight)
{
// assigning initial_weight to class variable weight
weight = initial_weight;
}
// A method called eat with parameter leaves
void eat(int leaves)
{
// as each leaf weighs one gram which is equal to 0.001 kilos
// calculating total leaves weight in kilos as the bear weighs in
kilos
double leaves_weight = leaves * 0.001;
// incrementing bear weight by adding leaves_weight
weight = weight + leaves_weight;
}
// A method named showWeight()
void showWeight()
{
// Printing the bear weight
System.out.println("This koala weighs "+ weight +" kilos");
}
}
// Main class
public class Main
{
// main method
public static void main(String[] args)
{
// Creating a KoalaBear Object with initial weight
100
KoalaBear koala = new KoalaBear(100);
// calling showWeight method
koala.showWeight();
// making KoalaBear to eat 400 leaves using method
eat()
koala.eat(400);
// calling showWeight method
koala.showWeight();
// making KoalaBear to eat 300 leaves using method
eat()
koala.eat(300);
// calling showWeight method
koala.showWeight();
// making KoalaBear to eat 650 leaves using method
eat()
koala.eat(650);
// calling showWeight method
koala.showWeight();
}
}
//code ended here
Output:
If You Have Any Doubts. Please Ask Using Comments.
Have A Great Day!