Photo AI
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. The user is... show full transcript
Step 1
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
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
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
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
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);
Report Improved Results
Recommend to friends
Students Supported
Questions answered