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...
Write this in java please. I use Eclipse Write the following two functions. main doesnt need...
Write this in java please. I use Eclipse Write the following two functions. main doesnt need to be filled up. Function 1: Write a function called howMany that takes two arguments: an array of integers called arr, and an integer called iTarget. The function should return an integer result. Inside the function write a loop to go through the array and count the number of elements in the array that have the same value as iTarget. At the end it...
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...
I am working java project. I need to write methods for text game. I'm working on...
I am working java project. I need to write methods for text game. I'm working on chest class. Description : You should start your work by implementing the Chest class (a skeleton is provided for you). It needs to store whether it is locked or not, a String describing the contents, and which key it was locked with. The method stubs for this class are provided with comments, be sure to implement them the way they are described. You should...
I need this before the end of the day and in java please :) 10.12 Lab...
I need this before the end of the day and in java please :) 10.12 Lab 9 In BlueJ, create a project called Lab9 A car dealership pays their salesmen a base salary every month. However, based on how many cars they sold (qtySold) they get a percentage of their salary as a bonus. To determine the percent bonus, the company uses the following chart. qtySold %Bonus 5 or less 0% More than 5 but 10 or less 5% More...
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”...
I need know how to Write a java program called character length that prompts the user...
I need know how to Write a java program called character length that prompts the user to enter a string and displays: The length of the string is: xxxxx The strings first character is: x    (where xxxxx is the length of the string and x is the first character.)
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT