In this lab, the task is to take our previous lab, and create methods to execute our program as opposed to simple case statements. So I created 4 methods and then used a switch statement to call those methods when the user inputs his selection. I also was able to account for invalid inputs from the user. If the program asks for a number, and the user inputs a letter, it will ask the user to use a valid input for the program. This creates a program that does what its supposed to do, and remain as error-free as possible.
import java.util.Scanner; import java.util.Random; public class Temperaturesensor { @SuppressWarnings("finally") public static void main(String[] args) { boolean run = false; System.out.println("Welcome to the Temperature Sensor Simulator.\n"); while(!run){ try{ System.out.println("\n\nPlease select the season you wish to simulate: \n\n" + "1. Winter\n" + "2. Spring\n" + "3. Summer\n" + "4. Fall\n" + "5. Exit\n"); @SuppressWarnings("resource") Scanner input = new Scanner(System.in); int selection = input.nextInt(); switch(selection){ case 1: getwintertemp(); break; case 2: getspringtemp(); break; case 3: getsummertemp(); break; case 4: getfalltemp(); break; case 5: run = true; System.out.println("Thank you for using the temperature simulator!"); break; default: System.out.println("huh? Try Again. Enter a Value from (1-5): "); }//end of switch } catch(Exception e){ System.out.println("Huh? Please enter correct values.... "); } } //end of While loop } public static void getwintertemp(){ int degrees; int min = 100; //used 100 so that program can update lower values as they process int max = 0; int firsttemp = 0; int lasttemp = 0; int sum = 0; int average = 0; int simulations; Random sim = new Random(); System.out.println("How many simulations would you like to run?\n"); @SuppressWarnings("resource") Scanner input1 = new Scanner(System.in); simulations = input1.nextInt(); for(int counter=1;counter<=simulations;counter++){ degrees = 21+sim.nextInt(20); System.out.print(degrees + " "); if(degrees>max){ max=degrees; } else if(degrees<min){ min=degrees; } if(counter==1){ firsttemp = degrees; } else if(counter==simulations){ lasttemp = degrees; } sum+=degrees; average = sum/simulations; } System.out.println("\n\nFirst temperature generated: " + firsttemp); System.out.println("Last temperature generated: " + lasttemp); System.out.println("Lowest temperature generated: " + min); System.out.println("Highest temperature generated: " + max); System.out.println("Total sum of all temperatures generated " + sum); System.out.println("Average for the season: " + average); } public static void getspringtemp(){ int degrees; int min = 100; //used 100 so that program can update lower values as they process int max = 0; int firsttemp = 0; int lasttemp = 0; int sum = 0; int average = 0; int simulations; Random sim = new Random(); System.out.println("How many simulations would you like to run?\n"); @SuppressWarnings("resource") Scanner input2 = new Scanner(System.in); simulations = input2.nextInt(); for(int counter=1;counter<=simulations;counter++){ degrees = 41+sim.nextInt(30); System.out.print(degrees + " "); if(degrees>max){ max=degrees; } else if(degrees<min){ min=degrees; } if(counter==1){ firsttemp = degrees; } else if(counter==simulations){ lasttemp = degrees; } sum+=degrees; average = sum/simulations; } System.out.println("\nFirst temperature generated: " + firsttemp); System.out.println("Last temperature generated: " + lasttemp); System.out.println("Lowest temperature generated: " + min); System.out.println("Highest temperature generated: " + max); System.out.println("Total sum of all temperatures generated " + sum); System.out.println("Average for the season: " + average); } public static void getsummertemp(){ int degrees; int min = 100; //used 100 so that program can update lower values as they process int max = 0; int firsttemp = 0; int lasttemp = 0; int sum = 0; int average = 0; int simulations; Random sim = new Random(); System.out.println("How many simulations would you like to run?\n"); @SuppressWarnings("resource") Scanner input3 = new Scanner(System.in); simulations = input3.nextInt(); for(int counter=1;counter<=simulations;counter++){ degrees = 71+sim.nextInt(20); System.out.print(degrees + " "); if(degrees>max){ max=degrees; } else if(degrees<min){ min=degrees; } if(counter==1){ firsttemp = degrees; } else if(counter==simulations){ lasttemp = degrees; } sum+=degrees; average = sum/simulations; } System.out.println("\nFirst temperature generated: " + firsttemp); System.out.println("Last temperature generated: " + lasttemp); System.out.println("Lowest temperature generated: " + min); System.out.println("Highest temperature generated: " + max); System.out.println("Total sum of all temperatures generated " + sum); System.out.println("Average for the season: " + average); } public static void getfalltemp(){ int degrees; int min = 100; //used 100 so that program can update lower values as they process int max = 0; int firsttemp = 0; int lasttemp = 0; int sum = 0; int average = 0; int simulations; Random sim = new Random(); System.out.println("How many simulations would you like to run?\n"); @SuppressWarnings("resource") Scanner input4 = new Scanner(System.in); simulations = input4.nextInt(); for(int counter=1;counter<=simulations;counter++){ degrees = 41+sim.nextInt(20); System.out.print(degrees + " "); if(degrees>max){ max=degrees; } else if(degrees<min){ min=degrees; } if(counter==1){ firsttemp = degrees; } else if(counter==simulations){ lasttemp = degrees; } sum+=degrees; average = sum/simulations; } System.out.println("\nFirst temperature generated: " + firsttemp); System.out.println("Last temperature generated: " + lasttemp); System.out.println("Lowest temperature generated: " + min); System.out.println("Highest temperature generated: " + max); System.out.println("Total sum of all temperatures generated " + sum); System.out.println("Average for the season: " + average); } }