Question
Class called Battery that serves as a blueprint for objects representing a battery for a cell phone or other similar device. Each Battery object keeps
Class called Battery that serves as a blueprint for objects representing a battery for a cell phone or other similar device. Each Battery object keeps track of (1) the percentage of charge remaining in the battery (an integer) and (2) whether the battery is being charged (true if it is, and false if it is not).
Write only the five components of the class that we have requested below. You should assume that all of the other methods of the class are being written by someone else. You should not write them, and you should not call them.
Make sure that your class employs proper encapsulation to prevent clients of the class from putting a Battery object into an invalid state. In particular, you should not allow a Battery to have a negative charge; rather, you should throw an IllegalArgument- Exception to prevent this from happening. You only need to worry about this in the context of the code that that you write below.
a) Define the fields of the object. Choose appropriate names.
b) Write a constructor that takes an initial value for the charge remaining and assumes that the battery is not being charged.
c) Write an accessor method for whether the battery is being charged. Choose an appropriate name.
Sulliva
d) Write a method called decrementCharge that decreases the charge remaining in the battery by 1. For example, if the charge remaining is 50, calling this method should reduce it to 49.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Java public class Battery a Fields private int charge...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started