Lab 2

Description:

This lab is basically the same as Lab1. However, in this lab, the simulating method has to be excluded from the main program. Therefore, I create another class called “TemperatureSensor” and put the method in. I also have to modify the original program so it can get data from the method. Also, “try-catch” statement is needed to be part of the program as well. I used that statement to verify if the input is valid.

Code:

/* CET3640 Lab2 Luis Hu
 * this program will simulate temperature according to the season entered.
 * it will print out the following:
 * 1. first generated data
 * 2. last generated data
 * 3. highest generated data
 * 4. lowest generated data
 * 5. sum of generated data
 * 6. average of generated data
 * 
 * the difference from lab1 is that this lab call other class to roll numbers.
 * 
 */

import java.util.Scanner;

public class TemperatureSensorStats {

	private static Scanner scanner = new Scanner(System.in);

	public static void main(String[] args) {

		int programLooping = 0;
		outer: while (programLooping == 0) {

			int rollTemp = 0; // initialize

			// Season selecting menu 
			System.out.println("Select a season you want to simulate:"); // Display the menu.
			System.out.println("1. Spring");
			System.out.println("2. Summer");
			System.out.println("3. Fall");
			System.out.println("4. Winter");
			System.out.println("5. Exit");
			System.out.print("Your choice is:");
			String inSeason = scanner.nextLine();

			switch (inSeason) {		//check if input is valid
			case ("1"):
			case ("2"):
			case ("3"):
			case ("4"):
				break;
			case ("5"):
				programLooping = 1;
				return;
			default:
				System.out.print("invalid input\n");
				continue outer;
			}

			// promote user to set up the number of trial
			System.out.println("Enter number of trials you want:");
			String StInTrial = scanner.nextLine();
			int intInTrial1;

			try {		//check if input is valid
				intInTrial1 = Integer.parseInt(StInTrial);
			} catch (NumberFormatException ex) {
				System.out.println("invalid input\n");
				continue outer;
			}

			// declare and initialize variables
			int preRollTempHigh = 0;
			int preRollTempLow = 100;
			int rollTempTotal = 0;

			// start rolling!!
			for (int Counter = 0; Counter < intInTrial1; Counter++) {
				switch (inSeason) {
				case ("1"):
					rollTemp = TemperatureSensor.getSpringTemp();
					break;
				case ("2"):
					rollTemp = TemperatureSensor.getSummerTemp();
					break;
				case ("3"):
					rollTemp = TemperatureSensor.getFallTemp();
					break;
				case ("4"):
					rollTemp = TemperatureSensor.getWinterTemp();
					break;
				}

				// System.out.println(Counter+ " "+ rollTemp ); //used to monitor rolled number

				if (Counter == 0) 				// print out first generated data
					System.out.println("The first data generated is :"
							+ rollTemp);

				if (Counter == intInTrial1 - 1) // print out last generated data
					System.out.println("The last data generated is :"
							+ rollTemp);

				if (preRollTempHigh  rollTemp) // if rolled number is smaller
												// than previous, save it to
												// preRollTempLow
					preRollTempLow = rollTemp;

				rollTempTotal += rollTemp; // sum rolled number up

			}

			// print out the rest of collected data .
			System.out.println("The highest data generated is :"
					+ preRollTempHigh);
			System.out.println("The lowest data generated is :"
					+ preRollTempLow);
			System.out.println("The sum of generated data is :"
					+ rollTempTotal);
			System.out.println("The avarage of generated data is :"
					+ rollTempTotal / intInTrial1);

		}

	}
}
public class TemperatureSensor {

  		static int rollTemp;	

	  	public static int getSpringTemp(){
  	  		rollTemp = (int) (Math.random() * 30 + 40);
  	  		return rollTemp;
	  	}
	  	public static int getSummerTemp(){
	  		rollTemp = (int) (Math.random() * 20 + 70);
  	  		return rollTemp;
	  	}
	  	public static int getFallTemp(){
	  		rollTemp = (int) (Math.random() * 20 + 40);
  	  		return rollTemp;
	  	}
	  	public static int getWinterTemp(){
	  		rollTemp = (int) (Math.random() * 20 + 20);
  	  		return rollTemp;
	  	}
}

Screenshot:

Capture

Leave a Reply

Your email address will not be published. Required fields are marked *