Photo AI
Question 2
A group of mountain passes has been selected to determine specific statistics, such as the number of travellers, fines received by travellers and details about the d... show full transcript
Step 1
Answer
The constructor for the TMountainPass
class should be defined as follows:
public TMountainPass(String dangerLevel, int dangerLevel, int speedLimit, int distance) {
this.dangerLevel = dangerLevel;
this.dangerLevel = dangerLevel;
this.speedLimit = speedLimit;
this.distance = distance;
}
This constructor initializes the attributes of the class using the parameters received.
Step 2
Step 3
Answer
The calculateFine
method should be implemented like this:
public double calculateFine(int travelTime) {
double speed = (distance / (travelTime / 60.0));
if (speed <= speedLimit + 10) {
return 0.0;
} else {
double fine = 1000 + Math.ceil((speed - speedLimit - 10) / 3) * 100;
return fine;
}
}
This method calculates the average speed, checks if it exceeds the limit, and computes the fine if necessary.
Step 4
Answer
The suggestedDangerLevel
method can be written as follows:
public String suggestedDangerLevel(double averageRainfall) {
if (dangerLevel.equals("Low") && averageRainfall <= 10) {
return "Low";
} else if (dangerLevel.equals("Medium") && averageRainfall <= 20) {
return "Medium";
} else if (dangerLevel.equals("High")) {
return "High";
} else {
return "High";
}
}
This method evaluates the current danger level against the average rainfall and returns the suggested danger level accordingly.
Step 5
Answer
The code for this button should look like this:
String selectedPass = // code to get selected radio button;
int distance = Integer.parseInt(JOptionPane.showInputDialog("Enter distance:"));
String dangerLevelInput = JOptionPane.showInputDialog("Enter danger level:");
String gradient = JOptionPane.showInputDialog("Enter gradient:");
mountainPass = new TMountainPass(dangerLevelInput, dangerLevel, speedLimit, distance);
JOptionPane.showMessageDialog(null, "Mountain pass object created.");
This code gathers user input, instantiates the TMountainPass
object, and confirms the creation.
Step 6
Answer
Here is the implementation for displaying the mountain pass details:
String info = mountainPass.getDetails(); // assuming getter exists
JOptionPane.showMessageDialog(null, info);
String mapFileName = selectedPass + ".jpg";
File mapFile = new File(mapFileName);
if (mapFile.exists()) {
// code to replace <No map found> with the map
} else {
// Display <No map found> in the component;
}
This code checks for the existence of the map file and updates the display accordingly.
Step 7
Answer
The following implementation is for this button:
int speedLimit = Integer.parseInt(speedLimitTextBox.getText());
int travelTime = Integer.parseInt(travelTimeTextBox.getText());
double fine = mountainPass.calculateFine(travelTime);
String formattedFine = String.format("%.2f", fine);
fineOutputLabel.setText(formattedFine);
The code retrieves input from text boxes, calculates the fine, and displays it formatted to two decimal places.
Step 8
Answer
The code for processing the arrRain data should look something like this:
int rowIndex = selectedPassIndex; // based on user selection
int[] rainfallData = arrRain[rowIndex];
double averageRainfall = Arrays.stream(rainfallData).average().orElse(0);
String suggestedLevel = mountainPass.suggestedDangerLevel(averageRainfall);
outputArea.setText(mountainPass.toString());
if (!suggestedLevel.equals(mountainPass.getDangerLevel())) {
mountainPass.setDangerLevel(suggestedLevel);
outputArea.append("Danger level updated to: " + suggestedLevel);
} else {
outputArea.append("No change suggested, current level is: " + mountainPass.getDangerLevel());
}
This code calculates average rainfall and updates the danger level based on conditions.
Report Improved Results
Recommend to friends
Students Supported
Questions answered