Question: [SCALA] Write a program with: 1. A class named Battery that: - Has a constructor that takes a variable named charge of type Int 2.

[SCALA]

Write a program with:

1. A class named "Battery" that:

- Has a constructor that takes a variable named "charge" of type Int

2. A class named Flashlight that:

- Has 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 (ie. Batteries included)

- A method named "use" that takes no parameters and returns Unit

--- This method(use) will decrease the charge of the FlashLight's battery by 1 - If the charge is 0, this method does nothing

- A method named "replaceBattery" that takes a Battery as a parameter and returns a Battery

--- This method(replaceBattery) 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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!