```java
public class ChargingHub {
/**
* The amount of energy, in kilowatt-hours (kWh), currently available at the hub;
* initialized in the constructor and always greater than or equal to 0.
*/
private int availableEnergy;
/**
* Simulates one charging session with numVehicles vehicles charging or an emergency shutdown,
* as described in part (a).
* Precondition: numVehicles > 0
/
public void simulateOneSession(int numVehicles) {
/ to be implemented in part (a) */
}
/**
* Returns the number of sessions in which vehicles were able to draw energy from the hub,
* as described in part (b).
* Preconditions: numVehicles > 0, maxSessions > 0
/
public int simulateMultipleSessions(int numVehicles, int maxSessions) {
/ to be implemented in part (b) /
}
// There may be instance variables, constructors, or methods that are not shown.
}
```
(a) Write the `simulateOneSession` method, which simulates `numVehicles` vehicles charging at the hub during a single charging cycle. The method determines the total energy drawn from the hub during the session and updates the `availableEnergy` instance variable.
The simulation accounts for normal operating conditions, which occur 85% of the time, and emergency grid disconnects (abnormal conditions), which occur 15% of the time.
Under normal conditions, all vehicles successfully charge, and each vehicle draws the exact same amount of energy. The energy drawn by each vehicle is a randomly selected integer from 25 to 65 kWh, inclusive, with each integer having an equal probability of being chosen. For example, if each vehicle draws 30 kWh and 4 vehicles charge, a total of 120 kWh is demanded.
* If the total energy demanded by the vehicles is greater than `availableEnergy`, all remaining energy is consumed and `availableEnergy` becomes 0.
* Otherwise, `availableEnergy` is decreased by the total energy demanded.
* Under abnormal conditions, an emergency disconnect empties/dissipates all remaining stored energy, setting `availableEnergy` to 0.
Complete the `simulateOneSession` method below:
```java
/**
* Simulates one charging session with numVehicles vehicles charging or an emergency shutdown,
* as described in part (a).
* Precondition: numVehicles > 0
/
public void simulateOneSession(int numVehicles)
```
---
(b) Write the `simulateMultipleSessions` method. The method repeatedly calls `simulateOneSession` to simulate charging sessions for up to `maxSessions` consecutive cycles. The simulation returns the number of sessions during which vehicles were able to obtain power (that is, sessions that began with a non-zero amount of `availableEnergy`).
Consider the following examples:
Example 1: If `availableEnergy` is initially 1,500 kWh, calling `simulateMultipleSessions(8, 4)` might run 4 consecutive sessions, ending with 320 kWh of energy remaining. The method returns `4` because energy was available at the beginning of all 4 sessions.
* Example 2: If `availableEnergy` is initially 200 kWh, calling `simulateMultipleSessions(10, 5)` might deplete all energy during session 1, leaving 0 kWh. Before session 2 can run, `availableEnergy` is 0 (or reaches 0 during session 2). The method returns `2` (since energy was found on sessions 1 and 2, but no further sessions can occur).
* Example 3: If `availableEnergy` is initially 0 kWh, calling `simulateMultipleSessions(5, 10)` immediately returns `0` without running any sessions.
Assume that `simulateOneSession` works as intended, regardless of what you wrote in part (a). You must use `simulateOneSession` appropriately to receive full credit.
Complete the `simulateMultipleSessions` method below:
```java
/**
* Returns the number of sessions in which vehicles were able to draw energy from the hub,
* as described in part (b).
* Preconditions: numVehicles > 0, maxSessions > 0
*/
public int simulateMultipleSessions(int numVehicles, int maxSessions)
```
查看答案详解收起答案详解
解题
To simulate one session:
1. Generate a pseudo-random floating-point value using `Math.random()`, which returns a value in the range \([0.0, 1.0)\).
2. Compare this value to `0.15` to represent the 15% probability of an emergency grid disconnect (abnormal condition). If triggered, set `availableEnergy = 0`.
3. Otherwise (under normal conditions with 85% probability), generate a random integer in the inclusive range \([25, 65]\). The number of possible outcomes is \(65 - 25 + 1 = 41\), so we compute `(int)(Math.random() * 41) + 25`.
4. Multiply this amount by `numVehicles` to find `totalDemand`.
5. If `totalDemand >= availableEnergy`, set `availableEnergy = 0`; otherwise, subtract `totalDemand` from `availableEnergy`.
```java
public void simulateOneSession(int numVehicles) {
if (Math.random() < 0.15) {
availableEnergy = 0;
} else {
int perVehicle = (int) (Math.random() * 41) + 25;
int totalDemand = numVehicles * perVehicle;
if (totalDemand > availableEnergy) {
availableEnergy = 0;
} else {
availableEnergy -= totalDemand;
}
}
}
```
---
### Part (b) Implementation
To simulate multiple sessions:
1. Maintain a counter for the number of sessions that successfully had power available.
2. Iterate while `count < maxSessions` and `availableEnergy > 0`.
3. In each iteration, call `simulateOneSession(numVehicles)` and increment `count` by 1.
4. Return the counter once the loop finishes.
```java
public int simulateMultipleSessions(int numVehicles, int maxSessions) {
int count = 0;
while (count < maxSessions && availableEnergy > 0) {
simulateOneSession(numVehicles);
count++;
}
return count;
}
```
评分标准
- Point 1: Generates a random value using `Math.random()` (or equivalent random generator).
- Point 2: Correctly creates a conditional branch implementing a 15% vs 85% probability.
- Point 3: Generates a uniformly distributed random integer in the range `[25, 65]` (e.g., `(int)(Math.random() * 41) + 25`).
- Point 4 (Algorithm): Scales the per-vehicle consumption by `numVehicles`, updates `availableEnergy` appropriately, and ensures `availableEnergy` does not become negative.
### Part (b): `simulateMultipleSessions` (5 points)
- Point 5: Calls `simulateOneSession` with the argument `numVehicles`.
- Point 6: Employs a loop that executes at most `maxSessions` times.
- Point 7: Checks that `availableEnergy > 0` before executing each session (or exits when `availableEnergy == 0`).
- Point 8 (Algorithm): Accurately tracks and counts only the sessions started with available energy.
- Point 9: Returns an `int` representing the total count of completed sessions across all execution paths.