lab 2

Description: In this lab we had to create two different classes, one call TemperatureSensorStats and the other called TemperatureSensor. Basically how this work is that when you run TemperatureSensorStats it will, in turn call the second class which is TemperatureSensor and process it. Also you have to include the methods in the TemperatureSensor class which are called, getWinterTemp, getSpringTemp, getSummerTemp, getFallTemp. Each one of these methods should have a minimum and maximum value. For example, the Method getWinterTemp should return a number between 20-40, getSpringTemp between 40-70, getSummerTemp between 70-90, and getFallTemp between 40-60. Each stimulation will call the correct methods from the TemperatureSensor class and process it base on which season the user selected from the menu. Additionally, we had to implement the try/catch statement which basically is, every time the user select a season from the menu, the try/catch make sure it is a valid input that is being selected from the menu. For example, if the user chooses an option from the menu let’s say 5, the program will executes without a problem. However, if the user chooses a number that is not on the menu, the user will see an error message telling them that is an invalid input and they should please try again. That is done using the try/catch statement. (Worked with Robert Marino).

TemperatureSensorStats Code:

package lab2;

import java.util.Scanner;

/**
 * @author Osasere Osayande
 * 
 *         Class to process temperature statics
 * 
 */
public class TemperatureSensorStats {

	static int count;
	static int curr_temp;
	static int first_temp = 0;
	static int last_temp = 0;
	static int high_temp = 0;
	static int low_temp = 0;
	static int total_temp = 0;
	static double avg_temp = 0;

	static TemperatureSensor t = new TemperatureSensor();
	private static Scanner input;
	private static Scanner input2;

	/**
	 * @param args
	 * 
	 *            Main method to invoke program
	 */
	public static void main(String[] args) {

		int option;
		do {
			System.out.println("Please Choose:");
			System.out.println("(1) Winter");
			System.out.println("(2) Spring");
			System.out.println("(3) Summer");
			System.out.println("(4) Fall");
			System.out.println("(5) To exit");
			option = getInput();

			if (option == 5) {
				System.exit(0);
			} else {
				processSimulation(option);
				displayStats();
			}

		} while (option != 5);

	}

	/**
	 * Method to get and validate user input from options
	 * 
	 * @return
	 */
	static int getInput() {
		int option = 0;
		boolean bError = true;
		input = new Scanner(System.in);

		do {
			try {
				System.out.println("Choose an Option: ");
				option = Integer.parseInt(input.nextLine());
				if (option  5) {
					Exception myException = new Exception();
					throw myException;
				}
				bError = false;
			} catch (Exception e) {
				System.out.println("Invalid Input! Please try again");
				input.reset();
			}
		} while (bError);

		return option;
	}

	/**
	 * Method to get and validate number of simulation
	 * 
	 * @return
	 */
	static int getSimulation() {
		int n = 0;
		boolean bError = true;
		input2 = new Scanner(System.in);

		do {
			try {
				System.out.println("Please enter number of simulations:");
				n = Integer.parseInt(input2.nextLine());
				bError = false;
			} catch (Exception e) {
				System.out.println("Invalid Input! Please try again");
				input2.reset();
			}
		} while (bError);
		return n;
	}

	/**
	 * Method to process simulation
	 * 
	 * @param option
	 */
	static void processSimulation(int option) {
		int n = getSimulation();
		count = 0;
		for (int i = 1; i <= n; i++) {
			System.out.println("Simulation #" + i);
			curr_temp = getSeasonTemp(option);
			total_temp = total_temp + curr_temp;
			count++;

			if (first_temp == 0) {
				first_temp = curr_temp;
			}

			if (low_temp == 0 || curr_temp  high_temp) {
				high_temp = curr_temp;
			}

		}
		last_temp = curr_temp;
		avg_temp = total_temp / count;

	}

	/**
	 * Method to get respective season temperature
	 * 
	 * @param option
	 * @return
	 */
	static int getSeasonTemp(int option) {
		int temp;
		switch (option) {
		case 1:
			temp = t.getWinterTemp();
			System.out.println("Winter Temperature: " + temp);
			return temp;

		case 2:
			temp = t.getSpringTemp();
			System.out.println("Spring Temperature: " + temp);
			return temp;

		case 3:
			temp = t.getSummerTemp();
			System.out.println("Summer Temperature: " + temp);
			return temp;

		case 4:
			temp = t.getFallTemp();
			System.out.println("Fall Temperature: " + temp);
			return temp;

		case 5:
			System.exit(0);
		}
		return 0;
	}

	/**
	 * Method to display statics
	 * 
	 */
	static void displayStats() {

		System.out.println("First temperature generated:" + first_temp);
		System.out.println("Last temperature generated:" + last_temp);
		System.out.println("Lowest temperature generated:" + low_temp);
		System.out.println("Highest temperature generated:" + high_temp);
		System.out.println("Total sum of all temperatures generated:"
				+ total_temp);
		System.out.println("Average for the season:" + avg_temp);
		System.out.println();

	}

}


TemperatureSensor code:

package lab2;

import java.util.Random;

/**
 * @author Osasere Osayande
 * 
 * Class to generate season temperature 
 *
 */
public class TemperatureSensor {
	int max;
	int min;
	Random r = new Random();

	/**
	 * Method to get winter temperature
	 * @return
	 */
	public int getWinterTemp() {
		min = 20;
		max = 40;
		return r.nextInt(max - min + 1) + min;
	}

	/**
	 * Method to get spring temperature
	 * @return
	 */
	public int getSpringTemp() {
		min = 40;
		max = 70;
		return r.nextInt(max - min + 1) + min;
	}

	/**
	 * Method to get summer temperature
	 * @return
	 */
	public int getSummerTemp() {
		min = 70;
		max = 90;
		return r.nextInt(max - min + 1) + min;
	}

	/**
	 * Method to get fall temperature
	 * @return
	 */
	public int getFallTemp() {
		min = 40;
		max = 60;
		return r.nextInt(max - min + 1) + min;
	}

}



lab2.1
lab2.2
lab2.3
lab2.4

option 5 is basically nothing, because no case 5 was included.

option 5 is basically nothing, because no case 5 was included.


here we can see the try/catch taking effect, because the user enter an invlia input

here we can see the try/catch taking effect, because the user enter an invalid input

Leave a Reply

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