Lab 3

Lab 3: Temperature Sensor Simulator (w/ Inheritance)

Description:

In this first lab, we were made to use a lot of the different programming concepts we learned and create a program. The second lab was to make the lab a bit easier to read. We used more than one class, and various methods, instead of using the main method. The current lab’s requirement is still to simulate the temperatures of all four seasons. However, we are to use the technique of inheritance, which says that any new classes that are created are extensions of the main (super) class, except with modifications.

So I had one class called Sensor that would act as a superclass. It’s job was to set up the reading variable that would help us access the other methods within the sub classes, to store and display our results. Another 2 classes were made to set up the Temperature and Humidity readings, by enabling those methods to detect and return numbers for each season and type of humidity at certain ranges. The last class was where our actual application was to be implemented, where our menus were to be displayed, and where we were to call the other methods in the other classes.

Within our last class, we were to implement a try/catch code, that would make sure we only enter the appropriate choices, and catch all other errors we may enter.

Code:

I didn’t have to use a lot of my previous code to implement this code, because of the different methods created. However, it did take a bit of extra effort.

Sensor class

public class Sensor {

public int reading;

public void setReading(){
reading = (int) (Math.random() * 100); // random returns a value between 0 and 1 multiplied to the seed
}

public void setReading(int u){
reading = (int) (Math.random() * u);
}
public void setReading(int l, int u){
int range = u-l;
reading = (int) ((Math.random() * range) + l);
}
public int getReading(){
return reading;
}
}

TemperatureSensor class

public class TemperatureSensor extends Sensor {

	public int getWinterTemp() {              
        setReading(20, 40);                
        return getReading();
}
	public int getSpringTemp() {              
        setReading(40, 70);                 
        return getReading();
}
	public int getSummerTemp() {              
        setReading(70, 90);                 
        return getReading();
	}

	public int getFallTemp() {              
        setReading(40, 60);                
        return getReading();
	}
}

HumiditySensor class

public class HumiditySensor extends Sensor{

	public int getHumidity() {              
        setReading(0, 100);                
        return getReading();
}
	public int getLowHumidity() {              
        setReading(0, 50);                 
        return getReading();
}
	public int getHighHumidity() {              
        setReading(50, 100);                 
        return getReading();
	}
}

SensorStatsApp class

import java.util.Scanner;
import java.io.IOException;
import java.util.InputMismatchException;

public class SensorStatsApp extends HumiditySensor{
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);

		HumiditySensor hSensor = new HumiditySensor();
		int sChoice = 0;               
		int hChoice = 0;               
		int seasonTemp = 0;              
		int humidityTemp = 0;        
		int iterations = 0;   
		int firstSeasonTemp = 0;
		int lastSeasonTemp = 0;
		int highSeasonTemp = 0;
		int lowSeasonTemp = 100;
		int totalSeasonTemp = 0;
		int averageSeasonTemp = 0;
		int firstHumidityTemp = 0;
		int lastHumidityTemp = 0;
		int highHumidityTemp = 0;
		int lowHumidityTemp = 100;
		int totalHumidityTemp = 0;
		int averageHumidityTemp = 0; 

		while(true){
			try {
				System.out.println("What season would you like to simulate?");
				System.out.println(" 1. Winter \n 2. Spring \n 3. Summer \n 4. Fall \n 5. Random \n 6. EXIT");
				System.out.print("Selection: ");
				sChoice = sc.nextInt();             // gathers user input for which season to simulate
				System.out.println();               // adds a break from the previous question

				if(sChoice >= 6){   
					System.exit(0); // exits the program 
				}

				System.out.println("What humidity would you like to simulate?");
				System.out.println(" 1. Full Range \n 2. Low Humidity \n 3. High Humidity \n 4. Random \n 5. EXIT");
				System.out.print("Selection: ");
				hChoice = sc.nextInt();             // gathers user input for humidity to simulate
				System.out.println();               // adds a break from previous question

				if(hChoice >= 5)
					System.exit(0); // exits the program 

				System.out.print("Input number of simulations: ");
				iterations = sc.nextInt();  // gathers user input for number of iterations

			}catch(InputMismatchException e) {     // if user inputed non numeric characters
				System.out.println(e + " - Error: expecting a number for input");
				return; // exits the program
			}

			firstSeasonTemp = seasonToSimulate(sChoice);
			firstHumidityTemp = humidityToSimulate(hChoice);
			totalSeasonTemp += firstSeasonTemp;
			totalHumidityTemp += firstHumidityTemp;

			try {
				display(seasonTemp, humidityTemp, 1);        // displays the current iteration of information
				for(int i = 2; i < iterations; i++){ 					seasonTemp = seasonToSimulate(sChoice);      // sends the sChoice int to be converted 					humidityTemp = humidityToSimulate(hChoice);  // sends the sChoice int to be converted and processed 					totalSeasonTemp += firstSeasonTemp; 					totalHumidityTemp += firstHumidityTemp; 					display(seasonTemp, humidityTemp, i);        // displays the current iteration of information 				}		 				lastSeasonTemp = seasonToSimulate(sChoice); 				lastHumidityTemp = humidityToSimulate(hChoice); 			 				if(seasonTemp > highSeasonTemp)
					highSeasonTemp = seasonTemp;
				if(seasonTemp < lowSeasonTemp) 					lowSeasonTemp = seasonTemp; 				if(lastSeasonTemp > highSeasonTemp)
					highSeasonTemp = lastSeasonTemp;
				if(lastSeasonTemp < lowSeasonTemp) 					lowSeasonTemp = lastSeasonTemp; 				 				if(firstSeasonTemp > highSeasonTemp)
					highSeasonTemp = firstSeasonTemp;
				if(firstSeasonTemp < lowSeasonTemp) 					lowSeasonTemp = firstSeasonTemp; 				 				if(humidityTemp > highHumidityTemp)
					highHumidityTemp = humidityTemp;
				if(humidityTemp < lowHumidityTemp) 					lowHumidityTemp = humidityTemp; 				if(lastHumidityTemp >= highHumidityTemp)
					highHumidityTemp = lastHumidityTemp;
				if(lastHumidityTemp <= lowHumidityTemp) 					lowHumidityTemp = lastHumidityTemp; 				 				if(firstHumidityTemp > highHumidityTemp)
					highHumidityTemp = firstHumidityTemp;
				if(firstHumidityTemp < lowHumidityTemp)
					lowHumidityTemp = firstHumidityTemp;

				totalSeasonTemp += firstSeasonTemp;
				totalHumidityTemp += firstHumidityTemp;
				averageSeasonTemp = totalSeasonTemp / iterations;
				averageHumidityTemp = totalHumidityTemp / iterations;

			}
			catch(IOException e){

				System.out.println(e + " - Error: user input");   // lets the user know why their sChoice failed.
				return;     // doesn't return anything cause the return type is void. This just ends the program.
			}

			System.out.println("1- First temperature: " +firstSeasonTemp +"\n" +
					"2- Last temperature: " +lastSeasonTemp +"\n" +
					"3- Lowest temperature: " +lowSeasonTemp +"\n" +
					"4- Highest temperature: " +highSeasonTemp +"\n" +
					"5- Total sum: " + totalSeasonTemp+"\n" +
					"6- Average: " + averageSeasonTemp+"\n" );

			System.out.println("1- First humidity: " +firstHumidityTemp +"\n" +
					"2- Last humidity: " +lastHumidityTemp +"\n" +
					"3- Lowest humidity: " +lowHumidityTemp +"\n" +
					"4- Highest humidity: " +highHumidityTemp +"\n" +
					"5- Total sum: " + totalHumidityTemp+"\n" +
					"6- Average: " + averageHumidityTemp+"\n" );
		}
	}
    public static void display(int sTemp, int hTemp, int iteration){
    }
	public static int humidityToSimulate(int choice) throws IOException {
		int temp = -2;
		HumiditySensor hSensor = new HumiditySensor();

		Boolean done = false;   // if random another iteration through switch is needed

		while(!done){ // if the user decides to use random then done != true so I can iterate one more time in the switch statement
			switch(choice){      // sChoice is the season in terms of an int

			case 1:   {     // full range, if choice is 1 it falls into this case and breaks at the statement break; (ends the current switch)
				temp = hSensor.getHumidity();
				done = true;
				break;
			}
			case 2:   {    // low humidity, if choice is 2 it falls into this case.
				temp = hSensor.getLowHumidity();
				done = true;
				break;
			}
			case 3:   {      // high humidity
				temp = hSensor.getHighHumidity();
				done = true;
				break;
			}
			case 4:   {      // random
				choice = (int) Math.random() * 3;   // random times (3) for 3 humidity types
				if(choice == 0) choice++;           // 0 is a possibility but not an option
				break;
			}

			default:
			{
				throw new IOException(); // user gave improper option
			}       
			}
		}

		return temp;
	}
	public static int seasonToSimulate(int choice) throws IOException {
		int temp = -1;
		TemperatureSensor tSensor = new TemperatureSensor();
		Boolean done = false;   // if random another iteration through switch is needed

		while(!done){
			switch(choice) {      
			case 1: {               // winter
				temp = tSensor.getWinterTemp();
				done = true;
				break;
			}
			case 2: {              // spring
				temp = tSensor.getSpringTemp();
				done = true;
				break;
			}
			case 3: {             // summer
				temp = tSensor.getSummerTemp();
				done = true;
				break;
			}
			case 4: {        // fall
				temp = tSensor.getFallTemp();
				done = true;
				break;
			}
			case 5:  {       // random
				choice = (int) Math.random() * 4;   // random times(4) for 4 seasons
				if(choice == 0) choice++;           // 0 is a possibility not an option
				break;
			}

			default:
			{
				throw new IOException(); // user gave improper option
			}       
			}
		}
		return temp;
	}
}

Screenshot:

CET3640 LAB 3

Leave a Reply

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