2. The SpaceProbe class, which you will write, represents a robotic probe exploring a planet. SpaceProbe objects are created by calls to a constructor with two parameters.
SpaceProbe objects are created by calls to a constructor with two parameters. The constructor initializes the probe's current energy to the maximum energy and the total distance traveled to 0.
The first parameter is a String representing the name of the probe. (Precondition: Assume the name is not null.)
The second parameter is an int representing the maximum energy capacity of the probe. (Precondition: Assume this value is positive.)
explore
Attempts to move the probe a specified distance, consuming energy.
An int representing the distance to explore in kilometers. Assume this value is positive.
Moving requires 2 units of energy per kilometer.
If the probe has enough energy to cover the full distance, the move is successful: energy is reduced by the calculated cost, the total distance traveled is updated, and the method returns a string in the format "Success: explored N km".
If the probe does not have enough energy, the move fails: energy and total distance remain unchanged, and the method returns the string "Failure: not enough energy".
A String indicating the result of the exploration attempt.
recharge
Adds energy to the probe's battery.
An int representing the amount of energy to add. Assume this value is positive.
Increases the probe's current energy by the specified amount.
The energy cannot exceed the probe's maximum capacity. If the sum exceeds the maximum, the energy is set to the maximum.
Returns the value of the probe's energy after recharging.
An int representing the current energy level.
getStatus
Provides a summary of the probe's current state.
Returns a formatted string containing the name, total distance traveled, current energy, and maximum energy.
The format must be: "NAME: D km traveled, C/M energy", where NAME is the probe's name, D is total distance, C is current energy, and M is maximum energy.
A formatted String summary.
Statement | Return Value | Explanation |
|---|---|---|
SpaceProbe rover = new SpaceProbe("Curiosity", 50); | A new SpaceProbe named "Curiosity" is created with max energy 50. Current energy starts at 50. Total distance starts at 0. | |
rover.explore(10); | "Success: explored 10 km" | Cost is 10 * 2 = 20. 50 >= 20, so move succeeds. Energy becomes 30. Distance becomes 10. |
rover.explore(20); | "Failure: not enough energy" | Cost is 20 * 2 = 40. Current energy is 30. 30 < 40, so move fails. State unchanged. |
rover.recharge(15); | 45 | Adds 15 to energy. 30 + 15 = 45. Returns 45. |
rover.getStatus(); | "Curiosity: 10 km traveled, 45/50 energy" | Returns formatted status string. |
rover.recharge(100); | 50 | Adds 100 to energy. 45 + 100 = 145. Capped at max (50). Returns 50. |
Write the complete SpaceProbe class. Your implementation must meet all specifications and conform to the examples shown in the table.
00:00