Lab 3

Description

In this lab you I have recreated Lab #2 using inheritance. First, I created a class and name it Sensor. The Sensor class contains a single private field element, a variable int reading. The Season class also contains following setter and getter methods: setReading, getReading. The Method setReading() generates random values between 0 and 100 and sets that value to the reading variable. The getReading() method always retrieves the last value stored on the reading variable. I have also created 2 more setReading() methods this is called overloading.

I’ve created the  setReading( int u) that generates values between 0 and the set number U. The seconded overloaded method setReading( int l, int u) generates values between l and u which counts for the inclusive range. I also created another class called TemperatureSensor that inherits class Sensor and implements the following methods: getWinterTemp, getSpringTemp, getSummerTemp, getFallTemp. These methods all generate a set inclusive range of temperature. getWinterTemp returns a number between 20-40, getSpringTemp between 40-70, getSummerTemp between 70-90, and getFallTemp between 40-60. These methods use the overloaded method setReading(int l, int u) to set the range and the  super.getReading() as the return statement of methods appropriately.

The third class I created was the HumiditySensor Class that inherits from the  class Sensor and  implement the following methods: getHumidity, getLowHumidity, getHighHumidity. The methods of this class all return a range a number. getHumidity returns a number between 0-100, getLowHumidity between 0-50, getHighHumidity between 50-100. These methods use the all 3 of the setReading() methods in class Sensor. they are refer to by the super. keyword and all use the getReading() method to retrieved the value for reading respectively.

The last class SensorsStatsApp is the Driver class for this program and it implement the application. In the application I display a menu that will first ask the user what season they would like to simulate[1] winter [2] spring [3] summer [4] fall [5] random [6] exit. In this program there is a new option called Random that will choose a season for the user at random.Another menu that is new to this application will ask the user what type of humidity they would like to simulate [1] full range [2] low [3] high [4] random [5] exit. Again the Random option will select a type of humidity for the user at random. Once the user has made their selection the prompt will now ask the user how many simulations to generate. Each simulation will call the corresponding method from the TemperatureSensor and HumiditySensor. At the end of the program it display a summary.

CODE:

public class Sensor {
	//Fields
	private int reading;

	//Methods
	public void setReading(){
			this.reading = (int)(101*Math.random()); //sets reading 0 - 100
	}
	public int getReading(){
			return this.reading;
	}

	//overloaded Methods
	public void setReading(int u) {
			int limitU=u+1;
			this.reading = (int)(limitU*Math.random()); //sets reading 0 to u
	}
	public void setReading(int l, int u) { //sets reading l to u
		int limitU = u+1;
		int limitL = l+1;
		int value;
		do{
			value = (int)(limitU*Math.random());
		}while((value<l)||(value>u)); //will ensure only values from l to u inclusive will be produce
		this.reading = value;	
}
}
___________________________________________________________________________________

public class HumiditySensor extends Sensor{

	//methods
	public int getHumidity(){
		super.setReading();
		return super.getReading();
	}
	public int getLowHumidity(){
		super.setReading(50);
		return super.getReading();
	}
	public int getHighHumidity(){
		super.setReading(50, 100);
		return super.getReading();
	}
}
_______________________________________________________________________________________

public class TemperatureSensor extends Sensor{

	public int getWinterTemp(){
		super.setReading(20, 40);
		return super.getReading();
	}
	public int getSummerTemp(){
		super.setReading(70, 90);
		return super.getReading();
	}
	public int getSpringTemp(){
		super.setReading(40, 70);
		return super.getReading();
	}
	public int getFallTemp(){
		super.setReading(40, 60);
		return super.getReading();
	}
}
______________________________________________________________________________________________
import java.util.Scanner;

public class SensorsStatsApp {
	static Scanner input = new Scanner(System.in);
	static HumiditySensor humid = new HumiditySensor();
	static TemperatureSensor seas = new TemperatureSensor();

	public static void main(String[] args) {
		int menuSelect=0, humiditySelect=0, runTime = 0; //menu selections
		int random=0;
		int lowestS=0, highestS=0, firstS=0, lastS=0,sumS=0; //season variables
		int lowestH=0, highestH=0, firstH=0, lastH=0,sumH=0; //Humidity variables
		boolean run = true;

		boolean randomSeason, randomHumidity;
		do{
			sumS = sumH =0;
			while(run){
				try{
					disSeasonMenu();	//Displays the Season Menu
					menuSelect = input.nextInt();
					if(menuSelect>=1 && menuSelect<=6){ 						break; 					}else{ 						System.out.println("Invalid Selection: Please Select Value from Menu"); 						continue; 					} 				} 				catch(Exception e){ 					input.nextLine(); 					System.out.println("Please Input Integer Values Only"); 				} 			} 			if(menuSelect>0 && menuSelect<6){
				if(menuSelect == 5){
					do{
						menuSelect = (int)(5*Math.random());
					}while(menuSelect4);
					randomSeason = true;
				}else{
					randomSeason = false;
				}
				while(run){
					try{
						disHumidityMenu();	//Displays the humidity Menu
						humiditySelect = input.nextInt(); //humidity input
						if(humiditySelect>=1 && humiditySelect<=5){ 							break; 						}else{ 							System.out.println("Invalid Selection: Please Select Value from Menu"); 							continue; 						} 					}catch(Exception h){ 						input.nextLine(); 						System.out.println("Error: Please input Only Integer Values"); 					} 				} 				if(humiditySelect>0 && humiditySelect <5){
					if(humiditySelect == 4){
						do{
							humiditySelect = (int)(4*Math.random());
						}while(humiditySelect3);
						randomHumidity = true;
					}else{
						randomHumidity = false;
					}while(run){
						try{
							System.out.println("How Many runs?");
							runTime = input.nextInt();
							if(runTime>0){
								break;
							}else{
								System.out.println("Please enter positive integers greater than 0");
								continue;
							}
						}catch(Exception r){
							input.nextLine();
							System.out.println("Error Please input valid Integers ");
						}
					}
					for(int i=0; i<runTime; i++)
					{ 
						switch(menuSelect){
						case 1:
							random = seas.getWinterTemp(); 
							break;
						case 2:
							random = seas.getSpringTemp();
							break;
						case 3:
							random = seas.getSummerTemp();
							break;
						case 4:
							random = seas.getFallTemp();
							break;
						}
						if(i==0)
							firstS = highestS = lowestS = random;
						else if(i==(runTime-1))
							lastS=random;
						if(random<lowestS) 							lowestS = random; 						if(random>highestS)
							highestS = random;
						sumS += random;
					}
					disSeasonResults(randomSeason,menuSelect,firstS,lastS,lowestS,highestS,sumS,runTime);
					for(int h=0; h<runTime; h++)
					{ 
						switch(humiditySelect){
						case 1:
							random = humid.getHumidity(); 
							break;
						case 2:
							random = humid.getLowHumidity();
							break;
						case 3:
							random = humid.getHighHumidity();
							break;
						}
						if(h==0)
							firstH = highestH = lowestH = random;
						else if(h==(runTime-1))
							lastH=random;
						if(random<lowestH) 							lowestH = random; 						if(random>highestH)
							highestH = random;
						sumH += random;
					}
					dispHumidityResults(randomHumidity,humiditySelect,firstH,lastH,lowestH,highestH,sumH,runTime);

				}else if(humiditySelect == 5){
					menuSelect = 6;
				}
			}
		}while(menuSelect != 6);
	}

	static void disSeasonMenu(){
		System.out.println("Temperature Sensor Simulator");
		System.out.println("Which Season would you like to simulate");
		System.out.println("\t[1] Winter\n\t[2] Spring \n\t[3] Summer \n\t[4] Fall \n\t[5] Random \n\t[6] Exit");
	}
	static void disHumidityMenu(){
		System.out.println("Which Range of Humidity would you like to simulate");
		System.out.println("\t[1] Full Range \n\t[2] Low \n\t[3] High \n\t[4] Random \n\t[5] Exit");
	}
	static void disSeasonResults(boolean RanS,int season, int fstS, int lastS, int lowS, int higS, int sum, int run){
		System.out.print("\nSeason: ");
		if(RanS){
			System.out.print("Random: ");
		}
		switch(season){
		case 1:
			System.out.println("Winter");
			break;
		case 2:
			System.out.println("Spring");
			break;
		case 3:
			System.out.println("Summer");
			break;
		case 4:
			System.out.println("Fall");
			break;
		}
		System.out.println("1- First temperature generated: "+ fstS);
		System.out.println("2- Last temperature generated: "+ lastS);
		System.out.println("3- Lowest temperature generated: "+ lowS);
		System.out.println("4- Highest temperature generated: "+ higS);
		System.out.println("5- Total sum of all temperatures generated: "+ sum);
		System.out.println("6- Average for the season: "+ (sum/run)+"\n");
	}
	static void dispHumidityResults(boolean RanH,int humidity, int fstH, int lastH, int lowH, int higH, int sum, int run){
		System.out.print("Humidity: ");
		if(RanH){
			System.out.print("Random: ");
		}
		switch(humidity){
		case 1:
			System.out.println("Full Range");
			break;
		case 2:
			System.out.println("Low");
			break;
		case 3:
			System.out.println("High");
			break;
		}
		System.out.println("1- First Humidity generated: "+ fstH);
		System.out.println("2- Last Humidity generated: "+ lastH);
		System.out.println("3- Lowest Humidity generated: "+ lowH);
		System.out.println("4- Highest Humidity generated: "+ higH);
		System.out.println("5- Total sum of all Humidities generated: "+ sum);
		System.out.println("6- Average Humidity reading: "+ (sum/run)+"\n");
	}
}
OpenLap Often Crops the codes and deletes special characters
In case this happens the full code can be downloaded by clicking this link

Code: Sensor HumiditySensor SensorsStatsApp TemperatureSensor
ScreenShot

1 3

Leave a Reply

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