Lab Description:
This lab demonstrates the temperature generator program. This program based on the user decision, generates a select number of random temperatures depending on the season. If the season winter is chosen, a temperature is generated between 20-40 degrees, if spring is chosen, a temperature is generated between 40-70 degrees, if summer is chosen, a temperature is generated between 70-90 degrees, if fall is chosen, a temperature is generated between between 40-60 degrees. The program calculates the maximum, minimum, first, last, the sum, and average of the temperatures generated. The way the program operates is through its entry in the do-while loop. Within the do-while loop, the using System.out.println statements to speak to user and using Scanner to receive the user’s input. Whatever season they respond with, the program enters the switch statement and according to there response it enters one the following seasons in the switch. Within in the season section, the program ask the user how many simulations they would want to do, then after the program will create a new object from the Random class that will generate the random temperatures. The for loop containing the random object will stop until it reaches the desired amount of simulations and output desired amount of random numbers. After the simulation is done, the program will ask the user if they would like to continue. The program will then enter an if-else statement, which will set a boolean variable to true or false that will continue the program. The do-while continues if value equals true.
Code:
import java.util.Random; import java.util.Scanner; import java.lang.Math; public class Main { public static void main(String main[]){ boolean value; int temp; //Create input for the question for temperature generator. Scanner userInput = new Scanner(System.in); Random r = new Random(); //The variables used for the max, min, total sum, average, and last number values. int max = 0, min = 0, maxtemp, mintemp, total = 0, sum = 0, average = 0, count, lastnum, finalnum = 0; /*seasons climate; climate = new seasons(); */ //do while, runs if bool value is true do{ System.out.println("What season would you like to simulate?, Type in the season in lower case letters or according number menu number"); System.out.println("Enter winter, spring, summer, fall, or exit to exit the program"); String decision = userInput.next(); //Location of temperature generator for all the seasons switch(decision){ case "winter": //temperatures generated are 20-40 //Ask user how many simulations they would like to do System.out.println("How many tempuratures would you like to simulate"); //Get users input temp = userInput.nextInt(); //Using a for loop generate random temperatures up to users requested amount of simulations mintemp = 40; maxtemp = 20; for(count = 0; count 0){ max = Math.max(max, randomNum); min = Math.min(min, randomNum); } sum += randomNum; finalnum = randomNum; //final number outputted } lastnum = finalnum; total += sum; // The sum of all the temperatures generated. average += (total / temp); // average = total / 'number of temperatures generated' //Outputs last number generated, Max, Min, Total sum, Average values if (count == temp){ System.out.println("The last temperature generated is: " + lastnum); System.out.println(); //Create space from the other values going to be displayed. System.out.println("The max temperature is: " + max); System.out.println("The minimum temperature is: " + min); System.out.println("The sum of temperatures is: " + total); System.out.println("The average temperature is: " + average); } break; case "spring": //temperatures generated are 40-70 //Ask user how many simulations they would like to do System.out.println("How many tempuratures would you like to simulate"); //Get users input temp = userInput.nextInt(); //Using a for loop generate random temperatures up to users requested amount of simulations maxtemp = 40; mintemp = 70; for(count = 0; count 0){ max = Math.max(max, randomNum); min = Math.min(min, randomNum); } sum += randomNum; finalnum = randomNum; } lastnum = finalnum; total += sum; // The sum of all the temperatures generated. average += (total / temp); // average = total / 'number of temperatures generated' //Outputs last number generated, Max, Min, Total sum, Average values if (count == temp){ System.out.println("The last temperature generated is: " + lastnum); System.out.println(); //Create space from the other values going to be displayed. System.out.println("The max temperature is: " + max); System.out.println("The minimum temperature is: " + min); System.out.println("The sum of temperatures is: " + total); System.out.println("The average temperature is: " + average); } break; case "summer": //temperatures generated are 70-90 //Ask user how many simulations they would like to do System.out.println("How many tempuratures would you like to simulate"); //Get users input temp = userInput.nextInt(); //Using a for loop generate random temperatures up to users requested amount of simulations maxtemp = 70; mintemp = 90; for(count = 0; count 0){ max = Math.max(max, randomNum); min = Math.min(min, randomNum); } sum += randomNum; finalnum = randomNum; } lastnum = finalnum; total += sum; // The sum of all the temperatures generated. average += (total / temp); // average = total / 'number of temperatures generated' //Outputs last number generated, Max, Min, Total sum, Average values if (count == temp){ System.out.println("The last temperature generated is: " + lastnum); System.out.println(); //Create space from the other values going to be displayed. System.out.println("The max temperature is: " + max); System.out.println("The minimum temperature is: " + min); System.out.println("The sum of temperatures is: " + total); System.out.println("The average temperature is: " + average); } break; case "fall": //temperatures generated are 40-60 //Ask user how many simulations they would like to do System.out.println("How many tempuratures would you like to simulate"); //Get users input temp = userInput.nextInt(); //Using a for loop generate random temperatures up to users requested amount of simulations maxtemp = 40; mintemp = 60; for(count = 0; count 0){ max = Math.max(max, randomNum); min = Math.min(min, randomNum); } sum += randomNum; finalnum = randomNum; } lastnum = finalnum; total += sum; // The sum of all the temperatures generated. average += (total / temp); // average = total / 'number of temperatures generated' //Outputs last number generated, Max, Min, Total sum, Average values if (count == temp){ System.out.println("The last temperature generated is: " + lastnum); System.out.println(); //Create space from the other values going to be displayed. System.out.println("The max temperature is: " + max); System.out.println("The minimum temperature is: " + min); System.out.println("The sum of temperatures is: " + total); System.out.println("The average temperature is: " + average); } break; default: System.out.println("Exiting program now"); break; } //Ask the user if they would like to continue, and their decision will set value to true or false. After value is set then the do-while will continue depending on what value equals to. System.out.println("Do you want to continue the program? Enter 1 for yes, 2 for no"); int in = userInput.nextInt(); if (in == 1){ value = true; } else{ value = false; } }while (value); }