Question: Language scala do comments for learning purpose thanks In a package named Lbgs, implement the following classes: class Battery with A constructor that takes a
Language scala
do comments for learning purpose thanks
In a package named Lbgs, implement the following classes:
class Battery with A constructor that takes a variable named "charge" of type Int.
class Flashlight with A constructor that takes no parameters.
When a new Flashlight is created, declare a state variable named "battery" of type Battery and set it to a new Battery with 5 charge (i.e. Batteries included).
A method named "use" that takes no parameters and returns Unit. This method will decrease the charge of the Flashlight's battery by 1. If the charge of the battery is 0, this method does nothing.
A method named "replaceBattery" that takes a Battery as a parameter and returns a Battery. This method swaps the input Battery with the Battery currently stored in this Flashlight's state variable. The returned Battery is the one that was in the state variable when the method is called.
Testing: In a package named tests" create a Scala class named "TestBatteries" as a test suite that tests all the functionality listed above. Be sure to check the references of batteries involved in both methods. When a battery is used, it is not enough to check that the charge ofthe battery in the flashlight has decreased by 1. You must check the reference of the battery in the flashlight to ensure that it is in fact the samebattery. To do this, you should store a reference to the battery created when the flashlight was created so you can compare it to the reference in the flashlight after it's used. The first thing you should do aftercreating a new flashlight is store a reference to its battery in a separate variable. val f1: Flashlight = new Flashlight() val f1Battery: Battery = f1.battery When replacing a battery, the batteries will be swapped by reference. Be sure to check for the proper reference of the battery in the flashlight and the battery that is returned by the method for proper references. Make sure the returned battery is the battery that was in the flashlight before the swap.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
