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

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
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...
I need a full java code. And I need it in GUI With the mathematics you...
I need a full java code. And I need it in GUI With the mathematics you have studied so far in your education you have worked with polynomials. Polynomials are used to describe curves of various types; people use them in the real world to graph curves. For example, roller coaster designers may use polynomials to describe the curves in their rides. Polynomials appear in many areas of mathematics and science. Write a program which finds an approximate solution to...
IN JAVA PLEASE ASAP !!! I just need the main and mergesort function Ask the user...
IN JAVA PLEASE ASAP !!! I just need the main and mergesort function Ask the user for the number of elements, not to exceed arraySize = 20 (put appropriate input validation) Ask the user for the type of data they will enter - EnglishGrade or MathGrade objects. Use your EnglishGrade and MathGrade classes Based on the input, create an appropriate array for the data to be entered. Write a helper function called recursionMergeSort such that: It is a standalone function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT