Lab3

Description: This lab is about recreating lab 2 but we use Inheritance. We created four classes: Sensor, TemperatureSensor, HumiditySensor, and SensorsStatsApp. The Sensor class contains an int field reading, setReading and getReading methods. The setReading () will generate a reading value between 0 and 100, the overloaded method setReading (int u) will generate a reading value between 0 and u with 0 <= u <= 100, and the overloaded method setReading (int l, int u) will generate a reading value between l and u with 0 <=1 <=u <= 100. And the getReading method returns the last reading generated. For the TemperatureSensor class, it should inherit the sensor class, and implement these methods: getWinterTemp that should return a number between 20 and 40, getSpringTemp that should return a number between 40 and 70, getSummerTemp that returns a number between 70 and 90, and getFallTemp that returns a number between 40 and 60 and they must be implemented by calling the super class methods. Also the Humidity class should inherit the sensor class, and implement these methods: getHumidity that should return a number between 0 and 100, getLowHumidity that should return a number between 0 and 50, and getHighHumidity that returns a number between 50 and 100 and they must be implemented by calling the super class methods. The fourth class SensorsStatsApp should implement the application. In the application you will display a menu that will first ask the user what season to simulate (1) winter (2) spring (3) summer (4) fall (5) random (6) exit. Random will choose the season for the user. After that, another menu will ask the user what type of humidity to simulate (1) full range (2) low (3) high (4) random (5) exit. Random will choose the type of humidity for the user. When the user selects the season and the type of humidity then the program should ask the user how many simulations to generate. Each simulation will call the corresponding method from the TemperatureSensor and HumiditySensor.

Code:

First class (Sensor):

package lab3;

public class Sensor {

	private int reading;

	public int getReading() {
		return reading;
	}

	public void setReading() {

	}

	public void setReading(int u) {
		this.reading = reading;

	}

	public void setReading(int l, int u) {
		this.reading = reading;

	}
}

Second class (TemperatureSensor):

package lab3;

public class TemperatureSensor extends Sensor {

	public int getWinterTemp() {
		int winterTemp = 0;
		super.setReading(20, 40);
		winterTemp = super.getReading();
		return winterTemp;
	}

	public int getSpringTemp() {
		int springTemp = 0;
		super.setReading(40, 70);
		springTemp = super.getReading();
		return springTemp;
	}

	public int getSummerTemp() {
		int summerTemp = 0;
		super.setReading(70, 90);
		summerTemp = super.getReading();
		return summerTemp;
	}

	public int getFallTemp() {
		int fallTemp = 0;
		super.setReading(20, 40);
		fallTemp = super.getReading();
		return fallTemp;
	}

}

Third class (HumiditySensor): 

package lab3;

public class HumiditySensor extends Sensor {

	public int getHumidity() {
		int humidity = 0;
		super.setReading(0, 100);
		humidity = super.getReading();
		return humidity;
	}

	public int getLowHumidity() {
		int lowhumidity = 0;
		super.setReading(0, 50);
		lowhumidity = super.getReading();
		return lowhumidity;
	}

	public int getHighHumidity() {
		int highhumidity = 0;
		super.setReading(50, 100);
		highhumidity = super.getReading();
		return highhumidity;
	}

}

Fourth class (SensorsStatsApp):

package lab3;

import java.util.Random;
import java.util.Scanner;

public class SensorsStatsApp {

	public static void main(String[] args) {

		try {

			Scanner Seasoninput = new Scanner(System.in);
			Random s = new Random();
			Random f = new Random();
			Random wi = new Random();
			Random sp = new Random();

			int SeasonChoice;
			int Simulations;
			int LowS = 70;
			int HighS = 90;
			int LowF = 40;
			int HighF = 60;
			int LowWi = 20;
			int HighWi = 40;
			int LowSp = 40;
			int HighSp = 70;
			int highStemp = Math.max(LowS, HighS);
			int lowStemp = Math.min(HighS, LowS);
			int sumtemp = 0;
			int avgtemp;
			int firsttemp = 0;
			int lasttemp = 0;

			int S = s.nextInt(HighS - LowS) + LowS;
			int F = f.nextInt(HighF - LowF) + LowF;
			int Wi = wi.nextInt(HighWi - LowWi) + LowWi;
			int Sp = sp.nextInt(HighSp - LowSp) + LowSp;

			System.out.println("Please choose a season to simulate");
			System.out
					.println("Enter (1)summer (2)fall (3)winter (4)spring (5)Random (6) exit");

			SeasonChoice = Seasoninput.nextInt();
			System.out.println("Enter # sim");
			Simulations = Seasoninput.nextInt();

			for (int i = 0; i  5) {
					System.out.println("Not valid option");
				}
				if (SeasonChoice == 1) {
					S = s.nextInt(HighS - LowS) + LowS;
					System.out.println("New temp: " + S);
					sumtemp += S;
					firsttemp = SeasonChoice;
					lasttemp = SeasonChoice;
					if (i == 0) {
						highStemp = S;
					}
					if (S > highStemp) {
						highStemp = S;

					}
					if (S  highStemp) {
						highStemp = F;

					}
					if (F  highStemp) {
						highStemp = Wi;

						if (Wi  highStemp) {
						highStemp = Sp;
					}
					if (Sp < lowStemp)
						lowStemp = Sp;
				}

			}
			System.out.println("High Temp: " + highStemp);
			System.out.println("Low temp: " + lowStemp);

			System.out.println("Sum temp of summer: " + sumtemp);

			avgtemp = sumtemp / Simulations;
			System.out.println("The summer average: " + avgtemp);
			System.out
					.println("what type of humidity to simulate (1) full range (2) low (3) high (4) random (5) exit");

		} catch (Exception e) {
			System.out.println("Invalid");

		}
	}

}

Screen Shots:

Untitled

 

 

Leave a Reply

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