Photo AI

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 danger levels of the mountain passes - NSC Information Technology - Question 2 - 2016 - Paper 1

Question icon

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-danger-levels-of-the-mountain-passes-NSC Information Technology-Question 2-2016-Paper 1.png

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

Worked Solution & Example Answer: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 danger levels of the mountain passes - NSC Information Technology - Question 2 - 2016 - Paper 1

Step 1

2.1.1 Write a constructor that receives the following parameters:

96%

114 rated

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

2.1.2 Write a mutator method named setDangerLevel

99%

104 rated

Answer

The setDangerLevel method should be implemented as follows:

public void setDangerLevel(String dangerLevel) {
    this.dangerLevel = dangerLevel;
}

This method updates the dangerLevel attribute of the class with the new value provided.

Step 3

2.1.3 Write a method named calculateFine

96%

101 rated

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

2.1.4 Write a method named suggestedDangerLevel

98%

120 rated

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

2.2.1 Button [Question 2.2.1] Write code to do the following:

97%

117 rated

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

2.2.2 Button [Question 2.2.2] Write code to do the following:

97%

121 rated

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

2.2.3 Button [Question 2.2.3] Write code to do the following:

96%

114 rated

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

2.2.4 Button [Question 2.2.4] Write code to do the following:

99%

104 rated

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.

Join the NSC students using SimpleStudy...

97% of Students

Report Improved Results

98% of Students

Recommend to friends

100,000+

Students Supported

1 Million+

Questions answered

;