Lab 3 Inheritance

Discussion:
In this lab we had to recreate lab 2 but with a lot more features added to it. We had to create 4 different classes Sensor, TemperatureSensor, HumiditySensor, and, SensorsStatsApp. The Sensor class containan int field reading and the methods such as, setReading, getReading Method setReading() generated a value from 0 and 100, overloaded method setReading( int u) generated a value 0 and u with 0 <= u <= 100 and overloaded method setReading( int l, int u) will generate a reading value between l and u with 0 <= l <= u <= 100. Next we created another class that inherits Sensor which was called using the keyword extends. The TemperatureSensor inherited Sensor and implemented the methods: getWinterTemp, getSpringTemp, getSummerTemp, getFallTemp. Method getWinterTemp was to return a value from 20-40, getSpringTemp between 40-70, getSummerTemp between 70-90, and getFallTemp between 40-60. Next we the third called HumiditySensor also inherited properties from the Sensor class by using the keyword extends. This class contains the methods, getHumidity, getLowHumidity, getHighHumidity. Method getHumidity return a number value of 0-100, getLowHumidity between 0-50, getHighHumidity between 50-100. This was implemented by calling super which will then call the parent method. Super is used to call the constructor, methods and properties of parent class. Lastly we created the SensorsStatsApp which was the contains the menu code and the code we run, After you run the code, the menu will be prompt and ask the user what they want to select. Once the user selects the season and the type of humidity they desire the program will then ask the user how many simulations to generate. Each simulation then call the corresponding method from the TemperatureSensor and HumiditySensor. (Worked with Robert Marino).

Code

Sensor:
import java.util.Random;


public class Sensor {

	private int temperature;

	Random r = new Random();

	public int getTemperature() {
		return temperature;
	}

	public void setTemperature() {
		temperature = r.nextInt(100 - 0 + 1) + 0;
	}

	public void setTemperature(int u) {
		temperature = r.nextInt(u - 0 + 1) + 0;
	}

	public void setTemperature(int l, int u) {
		temperature = r.nextInt(u - l + 1) + l;
	}

}

TemperatureSensor:

public class TempretureSensor extends Sensor {

	/**
	 * 20-40
	 * @return
	 */
	public int getWinterTemp(){
		super.setTemperature(20, 40);
		return super.getTemperature();
	}
	/**
	 * 40-70
	 * @return
	 */
	public int getSpringTemp(){
		super.setTemperature(40, 70);
		return super.getTemperature();
	}
	/**
	 * 70-90
	 * @return
	 */
	public int getSummerTemp(){
		super.setTemperature(70, 90);
		return super.getTemperature();
	}
	/**
	 * 40-60
	 * @return
	 */
	public int getFallTemp(){
		super.setTemperature(40, 60);
		return super.getTemperature();
	}

}

HumiditySensor:


public class HumiditySensor extends Sensor {

	/**
	 * 0-100
	 * @return
	 */
	public int getHumidity(){
		super.setTemperature();
		return super.getTemperature();
	}


	/**
	 * 0-50
	 * @return
	 */
	public int getLowHumidity(){
		super.setTemperature(50);
		return super.getTemperature();
	}


	/**
	 * 50-100
	 * @return
	 */
	public int getHighHumidity(){
		super.setTemperature(50, 100);
		return super.getTemperature();
	}

}

SensorsStatsApp:
import java.util.Random;
import java.util.Scanner;


/**
 * @author osasere osayande
 *
 */
public class SensorsStatsApp {

	static TempretureSensor ts = new TempretureSensor();
	static HumiditySensor hs = new HumiditySensor();
	private static Scanner input;

	public static void main(String[] args) {

		int seasonType;
		int humdityType;

		do {
			displayTempMenu();
			seasonType = getInput(1);
			displayHumidityMenu();
			humdityType = getInput(2);
			triggerSimulation(seasonType, humdityType);
		} while (true);

	}

	static void triggerSimulation(int seasonType, int humdityType){

		int n,temperature=0,humidity=0;
		int totalTemp=0, firstTemp=0, lowTemp=0, highTemp=0, lastTemp=0;
		int totalHumi=0, firstHumi=0, lowHumi=0, highHumi=0,  lastHumi=0;
		double  avgTemp=0, avgHumi=0;
		boolean seasonRandom = false, humidityRandom = false;

		Random r = new Random();
		if(seasonType ==5){
			seasonType = r.nextInt(4 - 1 + 1) + 1;
			seasonRandom = true;	
		}

		if(humdityType ==4){
			humdityType = r.nextInt(3 - 1 + 1) + 1;
			humidityRandom = true;	
		}

		System.out.println("How many simulations to generate: ");
		n=getInput(3);

		for(int i =1;i<=n;i++){
			temperature = getSeasonTemp(seasonType);
			humidity = getHumity(humdityType);
			System.out.println("Simulation Number: " + i + " Temperature: "+ temperature+ " Humidity: "+humidity);

			//Calculating Stats for Temperature
			totalTemp = totalTemp + temperature;
			if (firstTemp == 0) 
				firstTemp = temperature;
			if (lowTemp == 0 || temperature  highTemp) 
				highTemp = temperature;

			//Calculating Stats for Humidity
			totalHumi = totalHumi + humidity;
			if (firstHumi == 0) 
				firstHumi = humidity;
			if (lowHumi == 0 || humidity  highHumi) 
				highHumi = humidity;	
		}	

		lastTemp = temperature;
		avgTemp = (double) totalTemp / n;

		lastHumi = humidity;
		avgHumi = (double) totalHumi / n;

		System.out.println("\n\n************Stats************");
		System.out.print("\nSeason: ");
		if(seasonRandom) System.out.print("Random:");
		System.out.println(getSeasonName(seasonType));
		System.out.println("1 First temperature generated: "+firstTemp);
		System.out.println("2 Last temperature generated: "+lastTemp);
		System.out.println("3 Lowest temperature generated: "+lowTemp);
		System.out.println("4 Highest temperature generated: "+highTemp);
		System.out.println("5 Total sum of all temperatures generated: "+totalTemp);
		System.out.println("6 Average for the season: "+avgTemp);

		System.out.print("\nHumidity Type: ");
		if(humidityRandom) System.out.print("Random:");
		System.out.println(getHumityName(humdityType));
		System.out.println("1 First humidity reading generated: "+firstHumi);
		System.out.println("2 Last humidity reading generated: "+lastHumi);
		System.out.println("3 Lowest humidity reading generated: "+lowHumi);
		System.out.println("4 Highest humidity reading generated: "+highHumi);
		System.out.println("5 Total sum of all humidity readings generated: "+totalHumi);
		System.out.println("6 Average humidity reading: "+avgHumi);
		System.out.println("\n");
	}

	static int getSeasonTemp(int seasonType) {

		switch (seasonType) {
		case 1:
			return ts.getWinterTemp();
		case 2:
			return ts.getSpringTemp();
		case 3:
			return ts.getSummerTemp();
		case 4:
			return ts.getFallTemp();
		}
		return 0;

	}

	static int getHumity(int humdityType) {

		switch (humdityType) {
		case 1:
			return hs.getHumidity();
		case 2:
			return hs.getLowHumidity();
		case 3:
			return hs.getHighHumidity();
		}
		return 0;
	}

	static String getSeasonName(int seasonType) {

		switch (seasonType) {
		case 1:
			return "Winter";
		case 2:
			return "Spring";
		case 3:
			return "Summer";
		case 4:
			return "Fall";
		}
		return null;
	}



	static String getHumityName(int humdityType) {

		switch (humdityType) {
		case 1:
			return "Full";
		case 2:
			return "Low";
		case 3:
			return "High";
		}
		return null;
	}

	static void displayTempMenu() {

		System.out.println("Choose Season:");
		System.out.println("(1) Winter");
		System.out.println("(2) Spring");
		System.out.println("(3) Summer");
		System.out.println("(4) Fall");
		System.out.println("(5) Random");
		System.out.println("(6) Exit");

	}

	static void displayHumidityMenu() {

		System.out.println("Choose Humdity Type:");
		System.out.println("(1) Full Range");
		System.out.println("(2) Low");
		System.out.println("(3) High");
		System.out.println("(4) Random");
		System.out.println("(5) Exit");

	}

	static int getInput(int menu) {
		int option = 0;
		boolean inputError = true;
		input = new Scanner(System.in);

		do {
			try {
				option = Integer.parseInt(input.nextLine());
				if (option  6) {
					System.out.println("Number should be between 1 and 6 only");
					throw new Exception();
				} else if (menu==2 && option > 5) {
					System.out.println("Number should be between 1 and 5");
					throw new Exception();
				} else{
					inputError = false;	
				}

			} catch (Exception e) {
				System.out.println("Error! Please Enter Valid input");
				input.reset();
			}
		} while (inputError);

		// Exit system if user choose exit 
		if( (menu==1 && option == 6) ||(menu ==2 && option == 5)){
			System.out.println("Exit option selected");
			System.exit(0);
		} 

		return option;
	}

}

Screenshots:
lab 3 code 1
lab 3 code 2
lab 3 code 3
lab 3 code 4
lab 3 code 5

Leave a Reply

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