Lab Description:
This purpose of this Lab was to create a temperature simulator for the four seasons. This lab covered concepts of ‘while’ loops, ‘for’ loops, conditional statements, specifically ‘if’ statements and ‘switch’ cases. Other concepts include, integer comparisons, string comparisons and basic input and output.
Code:
package Lab1; import java.util.Scanner; import java.util.Random; public class TempSimulator { public static void main(String[] args) { int seasonChoice, numSimValue, firstTemp = 0, lastTemp = 0, lowTemp, highTemp, sumTemp =0, randTempValue; int avgTemp=0; Scanner season = new Scanner(System.in); //Used for capturing user input Scanner numSim = new Scanner(System.in); //Used for capturing user input Random randTemp = new Random(); //Creates a random number generator object Scanner loopControl = new Scanner(System.in); String run = "yes"; //Display Menu of Season while(run.equals("yes")) { System.out.println("Select Season"); System.out.println("1 - Winter\n2 - Spring\n3 - Summer\n4 - Fall\n"); System.out.println("Choice: "); seasonChoice = season.nextInt(); System.out.println("Enter Number of Simulations to Run: "); numSimValue = numSim.nextInt(); lowTemp = 99999; // Initial Value of Low Temp highTemp = 0; // Initial Value of High Temp //Switch switch (seasonChoice) { //Winter case 1: for(int count = 0; count < numSimValue; count++){ //Generate a random number between 20-40 randTempValue = 20 + randTemp.nextInt(20); //captures the first iteration of the loop if (count == 0){ firstTemp=randTempValue; } //checks to see if the current random temperature is lower than the stored lowest. if (randTempValue = highTemp){ highTemp = randTempValue; } //Stores the last value in the iteration if (count == (numSimValue - 1)){ lastTemp = randTempValue; } //Adds all the Generated Temperatures sumTemp += randTempValue; } //Calculate the average of all the generated temperatures. avgTemp = sumTemp/numSimValue; break; case 2: //Spring for(int count = 0; count < numSimValue; count++){ randTempValue = 40 + randTemp.nextInt(30); if (count == 0){ firstTemp=randTempValue; } if (randTempValue = highTemp){ highTemp = randTempValue; } if (count == (numSimValue - 1)){ lastTemp = randTempValue; } sumTemp += randTempValue; } avgTemp = sumTemp/numSimValue; break; case 3: //Summer for(int count = 0; count < numSimValue; count++){ randTempValue = 70 + randTemp.nextInt(20); if (count == 0){ firstTemp=randTempValue; } if (randTempValue = highTemp){ highTemp = randTempValue; } if (count == (numSimValue - 1)){ lastTemp = randTempValue; } sumTemp += randTempValue; } avgTemp = sumTemp/numSimValue; break; case 4: //Fall for(int count = 0; count < numSimValue; count++){ randTempValue = 40 + randTemp.nextInt(20); if (count == 0){ firstTemp=randTempValue; } if (randTempValue = highTemp){ highTemp = randTempValue; } if (count == (numSimValue - 1)){ lastTemp = randTempValue; } sumTemp += randTempValue; } avgTemp = sumTemp/numSimValue; break; default: System.out.println("Invalid Season Choice.\n"); continue; } //Print Out to User System.out.printf("First Temperature: %d\n", firstTemp); System.out.printf("Last Temperature: %d\n", lastTemp); System.out.printf("Lowest Temperature: %d\n", lowTemp); System.out.printf("Highest Temperature: %d\n", highTemp); System.out.printf("Sum of Temperature: %d\n", sumTemp); System.out.printf("Average Temperature: %d\n", avgTemp); //asks user if they would like to rerun the simulation System.out.println("Would You Like to run Simulation Again? yes/no: "); run = loopControl.nextLine(); } } }
Screenshot: