Photo AI

Distance of mountain pass in miles 145.87 Question 1.1 Many tourists are interested in the decrease in temperature while travelling up a mountain pass - NSC Information Technology - Question 1 - 2016 - Paper 1

Question icon

Question 1

Distance-of-mountain-pass-in-miles-145.87--Question-1.1--Many-tourists-are-interested-in-the-decrease-in-temperature-while-travelling-up-a-mountain-pass-NSC Information Technology-Question 1-2016-Paper 1.png

Distance of mountain pass in miles 145.87 Question 1.1 Many tourists are interested in the decrease in temperature while travelling up a mountain pass. The user is... show full transcript

Worked Solution & Example Answer:Distance of mountain pass in miles 145.87 Question 1.1 Many tourists are interested in the decrease in temperature while travelling up a mountain pass - NSC Information Technology - Question 1 - 2016 - Paper 1

Step 1

1.1 - Extract the distance from the text box as an integer

96%

114 rated

Answer

To extract the distance from the text box, use the following code snippet:

int distanceMiles = Integer.parseInt(textBox.getText());

Next, convert the distance into kilometers by multiplying the miles by 1.6:

double distanceKm = distanceMiles * 1.6;

Finally, display the calculated distance in the output text box with the appropriate labels:

outputTextBox.setText("Distance in kilometers: " + distanceKm);

Step 2

1.2 - Obtain the height of the mountain pass from the text box

99%

104 rated

Answer

To obtain the height of the mountain pass, execute the following code:

int heightMetres = Integer.parseInt(heightTextBox.getText());

Next, validate the height:

if (heightMetres < 1000) {
    messageBox.show("Please enter a height greater than or equal to 1000 m");
    heightTextBox.clear();
    heightTextBox.focus();
}

If valid, calculate the temperature for every 100 m above 1 000 m, updating as follows:

for (int i = 1000; i <= heightMetres; i += 100) {
    double temperature = 18 - ((i - 1000) / 100);
    outputTextBox.append("Height: " + i + " Temperature: " + temperature + "\n");
}

Step 3

1.3 - Set lowest to value at first index of arrays

96%

101 rated

Answer

To determine the lowest mountain pass, initialize the lowest variable and loop through the heights array:

int lowest = arrPassHeights[0];
String lowestName = arrPassNames[0];

for (int i = 1; i < arrPassHeights.length; i++) {
    if (arrPassHeights[i] < lowest) {
        lowest = arrPassHeights[i];
        lowestName = arrPassNames[i];
    }
}

Finally, display the name of the lowest mountain pass and its height:

outputTextBox.setText("Lowest mountain pass: " + lowestName + " with height: " + lowest);

Step 4

1.4 - Obtain type of accommodation from the combo box

98%

120 rated

Answer

To obtain the type of accommodation, use:

String accommodationType = comboBox.getSelectedItem().toString();

Then, get the number of people for the booking:

int numberOfPeople = Integer.parseInt(numberOfPeopleTextBox.getText());

Calculate the total cost based on the accommodation selected:

double costPerPerson;

switch (accommodationType) {
    case "Hotel":
        costPerPerson = 1700;
        break;
    case "Bed-and-steakfast":
        costPerPerson = 1200;
        break;
    case "Self-catering unit":
        costPerPerson = 750;
        break;
    case "Camping":
        costPerPerson = 300;
        break;
}

double totalCost = costPerPerson * numberOfPeople;

Check for Wi-Fi selection and update total cost accordingly:

if (wifiCheckBox.isSelected()) {
    totalCost += 150;
}

Step 5

1.5 - Extract transaction number from text box

97%

117 rated

Answer

To extract the transaction number, use:

int transactionNumber = Integer.parseInt(transactionNumberTextBox.getText());

Next, perform a prime number check with a loop:

boolean isPrime = true;
for (int i = 2; i <= transactionNumber / 2; i++) {
    if (transactionNumber % i == 0) {
        isPrime = false;
        break;
    }
}

Generate a random number based on whether it is prime:

int randomNumber;
if (isPrime) {
    randomNumber = (int) (Math.random() * 4) + 1;
} else {
    randomNumber = (int) (Math.random() * 3) + 1;
}

Finally, display the random number in the output box:

outputTextBox.setText("Random Number: " + randomNumber);

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

;