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

60.1K

Verified Solution

Question

Programming

I need this in Java please:

Behaviors. In the context of OOP, functions are called methodsor behaviors because they typically do something. Most often, theyread or change the values of one or more variables in the class.For example, you may have a weight variable in a class, and amethod called gainWeight( ) that increases the weight variable by acertain amount. For this part of the lab, create class KoalaBearthat has a weight attribute (in kilograms).

Create a constructor for the class that takes in (as aparameter) the initial weight of the koala bear. Then, write afunction called eat( ) that takes in the number of leaves the koalabear should eat. Each leaf weighs one gram, so you must increasethe weight of the koala bear by one gram. According to a sitededicated to saving Koala bears, they can eat between 200-500 gramsof leaves per day. To finish the class out, include a showWeight()method that prints out how much the koala weighs. In main, create akoala object starting at 100 kilos and show the koala’s weight.Then, make the koala eat 400, 300, and 650 leaves, showing theweight of the koala after each time it eats. NOTE: if you’re outputdoes not match exactly (because of the rounding of floating pointmath), that’s OK.

For example, Java will include some extra “precision”. Sampleoutput #1 This koala weighs 100.4 kilos This koala weighs 100.7kilos This koala weighs 101.35 kilos

Answer & Explanation Solved by verified expert
4.1 Ratings (793 Votes)
I have uploaded the Images of the code Typed code and Output ofthe Code I have provided explanation using comments read them forbetter understandingImages of the CodeNote If the below code is missing indentation please refercode ImagesTyped Code Creating a class called KoalaBearclass KoalaBear A    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students