Question

In: Computer Science

I need this in Java please: Behaviors. In the context of OOP, functions are called methods...

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

Solutions

Expert Solution

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!


Related Solutions

JAVA: Lab11C:Behaviors.In the context of OOP, functions are called methods or behaviors because they typically do...
JAVA: Lab11C: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 weightattribute(in kilograms). Create a constructor for the class that...
i need an outline for my paper called the human brain and it functions
i need an outline for my paper called the human brain and it functions
I need this written in Java, it is a Linked List and each of it's Methods....
I need this written in Java, it is a Linked List and each of it's Methods. I am having trouble and would appreciate code written to specifications and shown how to check if each method is working with an example of one method being checked. Thank you. public interface Sequence <T> { /** * Inserts the given element at the specified index position within the sequence. The element currently at that * index position (and all subsequent elements) are shifted...
Please I need this to be done in Java, can I have it answered by anonymous...
Please I need this to be done in Java, can I have it answered by anonymous who answered my last question regarding java? Thank you You will need to implement a specific algorithm implementation to match the class definition AND implement a unit test using JUnit that conforms the specific naming convention. Algorithm: Inputs: integers m and n , assumes m and n are >= 1 Order is not important for this algorithm Local variables integers: remainder Initialize: No initialization...
Create a Java project called (clsCashRegister.java) The context of this project is a cash register at...
Create a Java project called (clsCashRegister.java) The context of this project is a cash register at a shop. The cash register should display a list of items for sale with the price for each item. The cash register should calculate the total cost of the user’s purchase, receive a payment from the user, add tax to the cost, deduct the cost from the payment, and return the exchange back to the user. Display a welcome message: “Welcome to Cash Mart”...
Java OOP - how do I avoid using getter and setter method in player class for...
Java OOP - how do I avoid using getter and setter method in player class for better privacy? I am told to use regular method instead. but I dont know how Thank you ----------------------------------------------------------------------------------------------------------- package MainPackage; import java.util.ArrayList; public class SoccerTeam { private ArrayList<Player> list; public SoccerTeam(int maxSubmission) { list = new ArrayList<>(maxSubmission); } public boolean addPlayer(String newPlayer) { if(newPlayer == null || newPlayer.isEmpty() == true) { return false; } for(Player player : list) { if(player.getName() == newPlayer) { return...
IN JAVA. I have the following code (please also implement the Tester to test the methods...
IN JAVA. I have the following code (please also implement the Tester to test the methods ) And I need to add a method called public int remove() that should remove the first integer of the array and return it at the dame time saving the first integer and bring down all other elements. After it should decrease the size of the array, and after return and save the integer. package ourVector; import java.util.Scanner; public class ourVector { private int[]...
B has to be matched with A so please I need both in Java. the previous...
B has to be matched with A so please I need both in Java. the previous project mean A A. Write a class that maintains the top ten scores for a game application, implementing the add and remove methods but using a singly linked list instead of an array. B. Perform the previous project, but use a doubly linked list. Moreover, your implementation of remove(i) should make the fewest number of pointer hops to get to the game entry at...
I need this in Java please: Lab11B: Understanding the constructor. Remember that the constructor is just...
I need this in Java please: Lab11B: Understanding the constructor. Remember that the constructor is just a special method (with no return type) that has the same name as the class name. Its job is to initialize all of the attributes. You can actually have more than one constructor, so long as the parameters are different. Create a class called Turtle that has two attributes: 1) speed and 2) color. Then, create a constructor that has no parameters, setting the...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is...
JAVA JAVA JAVA Hey i need to find a java code for my homework, this is my first java homework so for you i don't think it will be hard for you. (basic stuff) the problem: Write a complete Java program The transport Company in which you are the engineer responsible of operations for the optimization of the autonomous transport of liquid bulk goods, got a design contract for an automated intelligent transport management system that are autonomous trucks which...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT