Lab 3

“INHERITANCE”

LAB DESCRIPTION:

Lab3 will be an addition of Lab2. Based on 2 classes have been created in lab2, we will develops lab3 by making a super class of those two classes. Sensor class will be super class of Temperature and Humidity, which will inherit all characteristics of Sensor class.

The Humidity will generate the values between 0 to 100 based on High, Low or Full humidity. In start class, there will be additional code for Humidity to ask user what they want to generate as we did for Temperature sensor. In addition, the user will have one more choice,which is Random. The computer will pick the season and generate based on requirement of user.

The inherited method will allow subclass to inherit all of their methods and fields. That will help the program to be shorter and understand easily. We can call super class by the key word “super”. The super class can be use by many subclasses but it won’t work in opposite way.

CODE:

Sensor.java

 
package Sensor;
import java.util.Random;

public class Sensor 
{
	private int reading;
	//Getting reading
	public int getReading() 
	{
		return reading;
	}
	//Generate number from 0-100
	public void setReading() 
	{
		Random RanNum = new Random();
		this.reading = RanNum.nextInt(101);
	}
	//Generate number from 0-read
	public void setReading(int read) 
	{
		Random RanNum = new Random();
		this.reading = RanNum.nextInt(read+1);
	}
	//Generate number from l to u
	public void setReading(int l,int u) 
	{
		Random RanNum = new Random();
		this.reading = RanNum.nextInt(u-l+1)+l;
	}

}

TemperatureSensor.java

 
package Sensor;

public class TemperatureSensor extends Sensor
{	
	//Getting Winter Temperature
	public int getWinterTemp()
	{
		super.setReading(20,40);
		int WinterTemp = super.getReading();
		return WinterTemp;
	}
	//Getting Spring Temperature
	public int getSpringTemp()
	{
		super.setReading(40,70);
		int SpringTemp = super.getReading();
		return SpringTemp;
	}
	//Getting Summer Temperature
	public int getSummerTemp()
	{
		super.setReading(70,90);
		int SummerTemp = super.getReading();
		return SummerTemp;
	}
	//Getting Fall Temperature
	public int getFallTemp()
	{
		super.setReading(40,60);
		int FallTemp = super.getReading();
		return FallTemp;
	}
	//Showing user the Output
	public void showOutput(double TotalTemp,double AveTemp, int FirstTemp,int LastTemp,int HighTemp, int LowTemp)
	{
		System.out.println("First Temperature of the season is: " + FirstTemp + " Degree");
		System.out.println("Last Temperature of the season is: " + LastTemp + " Degree");
		System.out.println("Highest Temperature of the season is: " + HighTemp + " Degree");
		System.out.println("Lowest Temperature of the season is: " + LowTemp + " Degree");
		System.out.println("Total Temperature of the season is: " + TotalTemp + " Degree");
		System.out.println("Average Temperature of the season is: " + AveTemp + " Degree");
	}
}

HumiditySensor.java

package Sensor;

public class HumiditySensor extends Sensor
{
	//Generate Humidity from 0-100
	public int getHumidity()
	{
		super.setReading(100);
		int Humidity = super.getReading();
		return Humidity;
	}
	//Generate Low Humidity from 0-50
	public int getLowHumidity()
	{
		super.setReading(50);
		int LowHumidity = super.getReading();
		return LowHumidity;
	}
	//Generate High Humidity from 50-100
	public int getHighHumidity()
	{
		super.setReading(50,100);
		int HighHumidity = super.getReading();
		return HighHumidity;
	}
	//Showing user the Output
	public void showOutput(double TotalHumidity,double AveHumidity, int FirstHumidity,int LastHumidity,int HighHumidity, int LowHumidity)
	{
		System.out.println("First Humidity of the season is: " + FirstHumidity);
		System.out.println("Last Humidity of the season is: " + LastHumidity);
		System.out.println("Highest Humidity of the season is: " + HighHumidity);
		System.out.println("Lowest Humidity of the season is: " + LowHumidity);
		System.out.println("Total Humidity of the season is: " + TotalHumidity);
		System.out.println("Average Humidity of the season is: " + AveHumidity);
	}

}

SensorStart.java

package Sensor;

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

public class SensorStart {

	public static void main(String[] args) 
	{
		Scanner keyboard = new Scanner(System.in);
		Random RanNum = new Random();
		int season=0;
		int input=0;
		String SeasonName = null;
		int Random = 0;
		String HumidityName = null;
		//Do while loop to re-do the simulation
		do
		{
			/****************************** Season Code ****************************************/
		//Asking for season
		System.out.println("Please! Enter the season for simulating:");
		System.out.println("Season Code: Winter(1); Spring(2);Summer(3);Fall(4); Random (5); (6) to Exit");
		//Checking for Integer input
		while(!keyboard.hasNextInt())
		{
			System.out.println("This is not an Integer Number");
			keyboard.next();
		}
		//Try to catch for errors
		try
		{
			season = keyboard.nextInt();
			if(season>6||season<=0)
			{
				throw new InputMismatchException("Your input is invalid! Please try again!");
			}

			//Checking for exit system and Random pick
			if(season==6)
				System.exit(0);
			else if(season==5)
			{
				Random = 1;
				season=RanNum.nextInt(4)+1;
			}

			//Asking for simulation
			System.out.println("How many time do you want to simulate?");
			input = keyboard.nextInt();
		}
		catch(InputMismatchException ex)
		{
			System.out.println(ex.getMessage());
			continue;
		}

		//Call for TemperatureSensor Object
		TemperatureSensor Temperature = new TemperatureSensor();

		//Get new Temperature
		int newTemp=0;
		//First Temperature
		int FirstTemp=0;
		//Last Temperature
		int LastTemp=0;

		//High Temperature
		int HighTemp=0;
		//Low Temperature
		int LowTemp=0;

		//Total Temperature
		double TotalTemp=0;
		//Average Temperature
		double AveTemp;

		for ( int i=0; i<input; i++)
		{
                        //Checking for season -using getTemperature-
			if(season==1)
			{
				newTemp = Temperature.getWinterTemp();
				SeasonName="Winter";
			}
			else if (season==2)
			{
				newTemp = Temperature.getSpringTemp();
				SeasonName="Spring";
			}
			else if (season==3)
			{
				newTemp = Temperature.getSummerTemp();
				SeasonName="Summer";
			}
			else if (season==4)
			{
				newTemp = Temperature.getFallTemp();
				SeasonName="Fall";
			}

			//Calculate Total Temperature		
			TotalTemp+=newTemp;

			//Assign for first, high and low temperature
			if (i==0)
			{
				FirstTemp=newTemp;
				HighTemp=newTemp;
				LowTemp=newTemp;

			}
			//Assign for last temperature
			if (i==(input-1))
				LastTemp=newTemp;

			//Checking for high and low temperature
			if(newTemp>HighTemp)
			HighTemp=newTemp;
			if(newTemp<LowTemp)
				LowTemp=newTemp;
		}	

		//Calculate Average Temperature
		AveTemp=TotalTemp/input;

		//Checking for Printout with Random
		if(Random == 1)
		{
			System.out.println("Season is: Random "+SeasonName);
			Random = 0;
		}
		else
			System.out.println("Season is:"+SeasonName);
		//Calling outputTemperature method
		Temperature.showOutput(TotalTemp,AveTemp,FirstTemp,LastTemp,HighTemp,LowTemp);

		/****************************** Humidity Code ****************************************/
				//Asking for season
				System.out.println("What type of Humidity do you want to simulate?");
				System.out.println("Humidity Code: Full(1); Low(2);High(3);Random(4); (5) to Exit");
				//Checking for Integer input
				while(!keyboard.hasNextInt())
				{
					System.out.println("This is not an Integer Number");
					keyboard.next();
				}
				//Try to catch for errors
				try
				{
					season = keyboard.nextInt();
					if(season>5||season<=0)
					{
						throw new InputMismatchException("Your input is invalid! Please try again!");
					}

					//Checking for exit system and Random Pick
					if(season==5)
						System.exit(0);
					else if(season==4)
					{
						season=RanNum.nextInt(3)+1;
						Random = 1;
					}
					//Asking for simulation
					System.out.println("How many time do you want to simulate?");
					input = keyboard.nextInt();
				}
				catch(InputMismatchException ex)
				{
					System.out.println(ex.getMessage());
					continue;
				}

				//Call for HumiditySensor Object
				HumiditySensor Humidity = new HumiditySensor();
				//Get new Humidity
				int newHumidity=0;
				//First Humidity
				int FirstHumidity=0;
				//Last Humidity
				int LastHumidity=0;

				//High Humidity
				int HighHumidity=0;
				//Low Humidity
				int LowHumidity=0;

				//Total Humidity
				double TotalHumidity=0;
				//Average Humidity
				double AveHumidity;
                                 for ( int i=0; i<input; i++)
                                 {
                                        if(season==1)
					{
						newHumidity = Humidity.getHumidity();
						HumidityName="Full";
					}
					else if (season==2)
					{
						newHumidity = Humidity.getLowHumidity();
						HumidityName="Low";
					}
					else if (season==3)
					{
						newHumidity = Humidity.getHighHumidity();
						HumidityName="High";
					}

					//Calculate Total Humidity	
					TotalHumidity+=newHumidity;

					//Assign for first, high and low Humidity
					if (i==0)
					{
						FirstHumidity=newHumidity;
						HighHumidity=newHumidity;
						LowHumidity=newHumidity;

					}
					//Assign for last Humidity
					if (i==(input-1))
						LastHumidity=newHumidity;

					//Checking for high and low Humidity
					if(newHumidity>HighHumidity)
					HighHumidity=newHumidity;
					if(newHumidity<LowHumidity)
						LowHumidity=newHumidity;
				}
				//Calculate Average Humidity
				AveHumidity=TotalHumidity/input;
				//Checking for Printout with Random
				if(Random == 1)
				{
					System.out.println("Humidity Type is: Random "+HumidityName);
					Random = 0;
				}
				else
					System.out.println("Humidity Type is:"+HumidityName);
				//Calling outputTemperature method
				Humidity.showOutput(TotalHumidity,AveHumidity,FirstHumidity,LastHumidity,HighHumidity,LowHumidity);
		//Asking for simulating again
		System.out.println("Do you want to simulate again? (1==Yes;5==No)");

		season = keyboard.nextInt();
		}while(season!=5);
	}
	}
SCREENSHOT:

lAB3 lAB3A