Lab Description:
In this lab we are required to re-do Lab#1 but with a few modifications which include utilizing specific methods; getWinterTemp, getSpringTemp, getSummerTemp, and get Fall Temp; which will return a random number within the specified ranges dictated by Lab #1. We are also asked to error check user inputs to eliminate non-integer values being able to be entered by the user.
As in Lab#1 we are also supposed to keep track of the same stats.
1- First temperature generated
2- Last temperature generated
3- Lowest temperature generated
4- Highest temperature generated
5- Total sum of all temperatures generated
6- Average for the season
Code:
//Yevgeniy Babkin CET - 3640 Lab #2 //Prof: J.Reyes Alamo //date: 2/28/14 //first the random generator utility/package needs to be imported import java.io.IOException; import java.util.InputMismatchException; import java.util.Random; // secondly we need to import the Scanner in order to get input from user import java.util.Scanner; import javax.swing.JOptionPane; // Then we start by creating a class public class TemperatureSensor { public static int getWinterTemp(int winternum) { winternum = (int) (20 + Math.random() * ((40-20)+1)); return winternum; } public static int getSpringTemp(int springnum) { springnum = (int) (40 + Math.random() * ((60-40)+1)); return springnum; } public static int getSummerTemp(int summernum) { summernum = (int) (70 + Math.random() * ((90-70)+1)); return summernum; } public static int getFallTemp(int fallnum) { fallnum = (int) (40 + Math.random() * ((60-40)+1)); return fallnum; } //this refers to the method or (function) "simulate" which will set the Seasonal range and //generate our random values according to users input public static void simulate(int numofsim,int range, int end) { //"ran" will be used to generate random values Random ran = new Random(); // here we create and initialize the variables we need for our statistics int firstTemp; int lastTemp; int lowestTemp; int highestTemp; int sumTemp = 0; int averageTemp = 0; int temp; // generating random values for (first temp) according to user selected choice //as well as setting a random ref.value firstTemp = ran.nextInt(end) + range; //here we set our reference as the first temp generated sumTemp = firstTemp; highestTemp = firstTemp; lowestTemp = firstTemp; // long for loop with if statements //to generate the user requested amount of values //and properly assign them for(int i=0; i<numofsim-2;i++) { // we use a method to generate random value "[name].nextInt(int n);" // generate a bunch of random values and double check against ref.value "temp" values temp = ran.nextInt(end)+range; if(temp > highestTemp) highestTemp = temp; if(temp < lowestTemp) lowestTemp = temp; // add to sum sumTemp += temp; } lastTemp = ran.nextInt(end) + range; // add to sum sumTemp += lastTemp; if(lastTemp > highestTemp) highestTemp = lastTemp; if(lastTemp < lowestTemp) lowestTemp = lastTemp; //we get the sum and divide by the number of simulations the user requested to get the average averageTemp = sumTemp/numofsim; //Print out all the required statistics of the random values System.out.println( "Seasonal Statistics \n\n" + "1- First temperature generated: " +firstTemp +"\n" + "2- Last temperature generated: " +lastTemp +"\n" + "3- Lowest temperature generated: " +lowestTemp +"\n" + "4- Highest temperature generated: " +highestTemp +"\n" + "5- Total sum of all temperatures generated: " + sumTemp+"\n" + "6- Average for the season: " + averageTemp+"\n" ); } //----------------------------------------- // program //----------------------------------------- static class TemperatureSensorStats {} public static void main (String [] args) //public class TemperatureSensorStats { Scanner scan = new Scanner(System.in); // declare and initialize the couple of variables that will be used in this scope int choice=0; int NumberOfSimulations = 0; int x =0; System.out.println("What season would you like to simulate???"); System.out.println("\nEnter 1 for Winter \nEnter 2 for Spring \nEnter 3 for Summer \nEnter 4 for Fall \nEnter 5 to Exit "); try{ choice = scan.nextInt(); } catch(Exception e){ System.out.println("That is not a valid selection please try again\n\n"); x=1; } while(choice!=5) { //System.out.println("How many temperature simulations would you like to generate?"); //NumberOfSimulations = scan.nextInt(); if (choice == 1) { System.out.println("How many temeprature simulations would you like to generate?"); try{ NumberOfSimulations = scan.nextInt(); } catch(Exception e) { System.out.println("Invalid Entry Please enter an Integer value"); } System.out.println("One specific Winter temperature simulation is: " +getWinterTemp(1) +"\n"); simulate(NumberOfSimulations,20,20); } else if(choice == 2) { System.out.println("How many temeprature simulations would you like to generate?"); NumberOfSimulations = scan.nextInt(); System.out.println("One specific Spring temperature simulation is: " +getSpringTemp(1) +"\n"); simulate(NumberOfSimulations,40,30); } else if(choice == 3) { System.out.println("How many temeprature simulations would you like to generate?"); NumberOfSimulations = scan.nextInt(); System.out.println("One specific Summer temperature simulation is: " +getSummerTemp(1) +"\n"); simulate(NumberOfSimulations,70,20); } else if(choice == 4) { System.out.println("How many temeprature simulations would you like to generate?"); NumberOfSimulations = scan.nextInt(); System.out.println("One specific Fall temperature simulation is: " +getFallTemp(1) +"\n"); simulate(NumberOfSimulations,40,20); } else { System.out.println("That is not a valid selection please try again\n\n"); } //else if (choice ==5) //{ // System.out.println("Thanks for using this Temperature Simulator\n\n" +"\n"+ "goodbye"); //} //else if (choice < 1 ) //{ // System.out.println("That is not a valid selection please try again\n\n"); //} //else if (choice >5) //{ // System.out.println("That is not a valid selection please try again\n\n"); //} //the 1st user menu which will repeat after the program reaches this stage System.out.println("Enter 1 for Winter\nEnter 2 for Spring\nEnter 3 for Summer\nEnter 4 for Fall\nEnter 5 to Exit"); choice = scan.nextInt(); } //Exit message if the above scan gets a 5 System.out.println("Thanks for using this Temperature Simulator\n\n" +"\n"+ "goodbye"); } }