Lab 2

Description

In this lab a TemperatureSensor class was created and contains all the set and get methods that will be used in the main program. A second class, TemperatureSensorStats, will implement the application. It will contain a menu which prompts the user to choose a season and number of simulations they would like to run. When the application is run the following information will be displayed.

  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

The application also validates the user inputs and upon successfully inputting the correct data the application executes and displays the above mentioned information.

Code

TemperatureSensor.java

//Ryan Lawrence
//TemperatureSensor.java contains all the methods used in the main java source.

import java.util.Random;

public class TemperatureSensor {

	private int  numberOfSim;
	private int firstTemp = 0, lastTemp = 0, lowTemp=999999, highTemp=0, sumTemp =0, randTempValue,avgTemp=0;
	Random randTemp = new Random();

	//Set method used to change the private variable
	public void setNumberOfSim(int numOfSim){
		numberOfSim = numOfSim;
	}

	//Method used to reinitialize all the variables
	public void reInitialize(){
		firstTemp=0;
		lastTemp=0;
		lowTemp=999999;
		highTemp=0; 
		sumTemp =0;
		avgTemp=0;
	}
	//Methods used for each season
	public void getWinterTemp(){
		for(int count = 0; count < numberOfSim; count++){
			randTempValue = 20 + randTemp.nextInt(20);	//Generate a random number between 20-40
			//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 <= lowTemp){	 				lowTemp = randTempValue; 			} 			//checks to see if the current random temperature is higher than the stored highest. 			if (randTempValue >= highTemp){
				highTemp = randTempValue;
			}
			//Stores the last value in the iteration
			if (count == (numberOfSim - 1)){
				lastTemp = randTempValue;
			}
			//Adds all the Generated Temperatures
			sumTemp += randTempValue;

		}
		//Calculate the average of all the generated temperatures.
		avgTemp = sumTemp/numberOfSim;

}
	public void getSpringTemp(){

		for(int count = 0; count < numberOfSim; count++){
			randTempValue = 40 + randTemp.nextInt(30);
			if (count == 0){
				firstTemp=randTempValue;
			}
			if (randTempValue <= lowTemp){ 				lowTemp = randTempValue; 			} 			if (randTempValue >= highTemp){
				highTemp = randTempValue;
			}
			if (count == (numberOfSim - 1)){
				lastTemp = randTempValue;
			}
			sumTemp += randTempValue;

		}
		avgTemp = sumTemp/numberOfSim;
	}

	public void getSummerTemp(){
		for(int count = 0; count < numberOfSim; count++){
			randTempValue = 70 + randTemp.nextInt(20);
			if (count == 0){
				firstTemp=randTempValue;
			}
			if (randTempValue <= lowTemp){ 				lowTemp = randTempValue; 			} 			if (randTempValue >= highTemp){
				highTemp = randTempValue;
			}
			if (count == (numberOfSim - 1)){
				lastTemp = randTempValue;
			}
			sumTemp += randTempValue;

		}
		avgTemp = sumTemp/numberOfSim;
	}

	public void getFallTemp(){
		for(int count = 0; count < numberOfSim; count++){
			randTempValue = 40 + randTemp.nextInt(20);
			if (count == 0){
				firstTemp=randTempValue;
			}
			if (randTempValue <= lowTemp){ 				lowTemp = randTempValue; 			} 			if (randTempValue >= highTemp){
				highTemp = randTempValue;
			}
			if (count == (numberOfSim - 1)){
				lastTemp = randTempValue;
			}
			sumTemp += randTempValue;

		}
		avgTemp = sumTemp/numberOfSim;
	}
	//method diplays all the information captured
	public void getOutputs(){
		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);
	}

}

TemperatureSensorStats.java

//Ryan Lawrence
//TemperatureSensorStats.java implements the TemperatureSensor.java
import java.util.Scanner;
import java.util.InputMismatchException;
public class TemperatureSensorStats {

	public static void main(String[] args) {

		String loopControl = "yes";		//loop control for program
		Scanner loopControlInput = new Scanner(System.in);	//scanner object
		TemperatureSensor tempSensor = new TemperatureSensor();	//creates a new TemperatureSensor object
		Scanner numSim = new Scanner(System.in);
		Scanner season = new Scanner(System.in);

		//while loop used to keep the program running until user enters no
		while(loopControl.equals("yes")){

			int choice;
			int numSimValue;
			//Exception handling- tests to ensure the user enters an integer
			try{
				System.out.println("Select Season");
				System.out.println("1 - Winter\n2 - Spring\n3 - Summer\n4 - Fall\n");
				System.out.println("Choice: ");
				choice = season.nextInt();
			}
			//If the user enters anything that is not an integer an error is displayed and the program restarts
			catch (InputMismatchException inputMismatchException){
				System.out.printf("\nException: %s\n", inputMismatchException);
				season.nextLine();
				System.out.println("Must enter an Integer\n");
				continue;

			}
			//If statement is used to ensure the user enters a value from the choices given
			//If they do not the program restarts
			if(choice==0 || choice > 4){
				System.out.println("Invalid Choice\n");
				continue;
			}
			//Exception handling- tests to ensure the user enters an integer
			try{
				System.out.println("Enter Number of Simulations to Run: ");
				numSimValue = numSim.nextInt();		
			}
			//If the user enters anything that is not an integer an error is displayed and the program restarts
			catch(InputMismatchException inputMismatchException2){
				System.out.printf("\nException: %s\n", inputMismatchException2);
				numSim.nextLine();
				System.out.println("Must enter an Integer. Restarting Program\n");
				continue;
			}
			//set method called
			tempSensor.setNumberOfSim(numSimValue);
			//switch statement used to determine which get method to use
			switch (choice){
			case 1:
				tempSensor.getWinterTemp();
				tempSensor.getOutputs();
				break;
			case 2:
				tempSensor.getSpringTemp();
				tempSensor.getOutputs();
				break;
			case 3:
				tempSensor.getSummerTemp();
				tempSensor.getOutputs();
				break;
			case 4:
				tempSensor.getFallTemp();
				tempSensor.getOutputs();
				break;
			}
		tempSensor.reInitialize();	//reinitialize all the variables of the object

		//Prompts the user, if they would like to run the program again
		System.out.println("Would you like to run the simulation again?[yes/no]: ");
		loopControl=loopControlInput.nextLine();
		}
	}

}

 Screenshot

3640_lab_2

Leave a Reply

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