A City Tech OpenLab ePortfolio

LAB 3 – INHERITANCE

LAB 3 – Inheritance

Lab Description:

In this lab, we will be recreating the Temperature sensor from lab 2 but with the help of inheritance. This application will be compromised of four class java files in total which are Sensor.java, TemperatureSensor.java, HumiditySensor.java, and SensorStatsApp.java. The Sensor class will contain the set/get reading methods which will be in charge of generating the random values from within the provided ranges by the user through the use of overloading methods. The TemperatureSensor class will be inheriting from the Sensor class and will contain the get methods that obtain the random temperature readings for the desired season within the provided range by calling the methods from its superclass. The HumiditySensor class will also be inheriting from the Sensor class and will contain the get methods for the selected humidity range and will be making use of the overloaded methods in the superclass. The SensorStatsApp will be the main application file. This will contain the Main Menu of the application which will ask the user for the season, humidity range, and the repetitions as well as contain the code that will be used to calculate and display the required outputs to the user. Like in the past, try and catch statements will be used to verify for valid inputs by the user.

Code:

Sensor Class Code:

public class Sensor {

	private int reading;

	public int setReading()
	{
		return reading = 0 + (int)(Math.random() * ((100 - 0) + 1));	
	}

	public int setReading(int u)
	{
		return reading = 0 + (int)(Math.random() * ((u - 0) + 1));	
	}

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

	public int getReading()
	{
		return reading;	
	}

}

TemperatureSensor class code:

public class TemperatureSensor extends Sensor{

	private int u;
	private int l;

	public int getWinterTemp(int l, int u)
	{
		return super.setReading( l, u );
	}

	public int getSpringTemp(int l, int u)
	{
		return super.setReading( l, u );
	}

	public int getSummerTemp(int l, int u)
	{
		return super.setReading( l, u );
	}

	public int getFallTemp(int l, int u)
	{
		return super.setReading( l, u );
	}

}

HumiditySensor class code:

public class HumiditySensor extends Sensor{

	private int u;
	private int l; 

	public int getHumidity()
	{
		return super.setReading();
	}

	public int getLowHumidity(int u)
	{
		return super.setReading( u );
	}

	public int getHighHumidity(int l, int u)
	{
		return super.setReading( l, u );
	}
}

SensorStatsApp code:

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

public class SensorStatsApp {

	private static Object String;

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner scan1 = new Scanner(System.in); //Creates new scanner object needed for input from user
		int season = 0;							//Field to hold the season selection
		int humidity = 0;						//Field to hold the humidity selection
		int counter = 0;						//Field to hold number of repetitions
		int again = 0;							//Field used to test for repetition of program 
		boolean startOver = false;				//Boolean used for while loop used to repeat program
		String season1 = "";
		String season2 = "";
		String hum1 = "";
		String hum2 = "";

		/*  Start of do...while loop used to Start Over Program Again */
		do {
			boolean valid1;						//Boolean used followed do...while loop used for try/catch Exceptions on Season selection 
			do {
				try {
					valid1 = true;
					System.out.println("Select a season");
					System.out.println("1.Winter 2.Spring 3.Summer 4.Fall 5.Random 6.Exit");
					season = scan1.nextInt();

					/*  If...else if selection used to check for correct integer selection Exception*/
					if (season = 6)
						valid1 = true;
	            	else if (season  6 )
						throw new NumberFormatException("Integer is out of range.");

					}catch ( InputMismatchException nfe) {		//Number Check Exception
						System.out.println( "Input is not a number." ); 
						valid1 = false;
						scan1.next();
					}catch (NumberFormatException nfe) {			//Range check exception
						System.out.println("Number is out of option range");
						valid1 = false;
						scan1.nextLine();
						}
				}while (!valid1);	//End of do...while loop used for try/catch exception of season selection

			if( season == 6 )	//Exit selection From Main Menu
				break;

				boolean valid4;						//Boolean used followed do...while loop used for try/catch Exceptions on Humidity selection 
				do {
					try {
						valid4 = true;
						System.out.println("Type of Humidity");
						System.out.println("1.Full Range 2.Low 3.High 4.Random 5.Exit");
						humidity = scan1.nextInt();

						/*  If...else if selection used to check for correct integer selection Exception*/
						if (humidity = 5)
							valid4 = true;
		            	else if (humidity  5 )
							throw new NumberFormatException("Integer is out of range.");

						}catch ( InputMismatchException nfe) {		//Number Check Exception
							System.out.println( "Input is not a number." ); 
							valid4 = false;
							scan1.next();
						}catch (NumberFormatException nfe) {			//Range check exception
							System.out.println("Number is out of option range");
							valid4 = false;
							scan1.nextLine();
							}
					}while (!valid4);	//End of do...while loop used for try/catch exception of Humidity selection

				if( humidity == 5 )	//Exit selection From Menu
					break;

			boolean valid2;			//Boolean used for second do...while loop for try/catch exceptions in # of reps
			do {
				try {
					System.out.println("How many times you want to simulate?");
					counter = scan1.nextInt();
					valid2 = true;
					}catch ( InputMismatchException nfe ) {		//Check for Valid Integer input
						System.out.println( "Input is not a valid number." );
						valid2 = false;
						scan1.next();	
						}
				}while (!valid2);	//End of do..while loop used for try/catch exception in # of reps	

				TemperatureSensor myTemp = new TemperatureSensor();	//New Temperature Sensor object created
				HumiditySensor myHumi = new HumiditySensor();   //New Humidity Sensor object created
				int temp[] = new int[counter];			//New Array initialized for required number of values
				int humi[] = new int[counter];			//New Array initialized for required number of values

				/*  If Selection statements used to call the required method depending on selection of season*/

				if( season == 5){
					season = 1 + (int)(Math.random() * ((4 - 1) + 1));
					season1 = "Random:";
				}

				if( season == 1){
					for(int i = 0; i<counter; i++)	//For loop used to generate required number of values
					{
						season2 = "Winter";
						int rValueT = myTemp.getWinterTemp(20,40);
						temp[i] = rValueT;
						System.out.println("Winter Temperature = " + rValueT);
					}
				}

				else if( season == 2){
					for(int i = 0; i<counter; i++)	//For loop used to generate required number of values
					{
						season2 = "Spring";
						int rValueT = myTemp.getSpringTemp(40,70);
						temp[i] = rValueT;
						System.out.println("Spring Temperature = " + rValueT);
					}
				}

				else if( season == 3){
					for(int i = 0; i<counter; i++)	//For loop used to generate required number of values
					{
						season2 = "Summer";
						int rValueT = myTemp.getSummerTemp(70,90);
						temp[i] = rValueT;
						System.out.println("Summer Temperature = " + rValueT);
					}
				}

				else if( season == 4){
					for(int i = 0; i<counter; i++)	//For loop used to generate required number of values
					{
						season2 = "Fall";
						int rValueT = myTemp.getFallTemp(40,60);
						temp[i] = rValueT;
						System.out.println("Fall Temperature = " + rValueT);
					}
				}

				if( humidity == 4){
					humidity = 1 + (int)(Math.random() * ((3 - 1) + 1));
					hum1 = "Random:";
				}

				if( humidity == 1){
					for(int i = 0; i<counter; i++)	//For loop used to generate required number of values
					{
						hum2 = "Full Range";
						int rValueT = myHumi.getHumidity();
						humi[i] = rValueT;
						System.out.println("Full Range Humidity = " + rValueT);
					}
				}

				else if( humidity == 2){
					for(int i = 0; i<counter; i++)	//For loop used to generate required number of values
					{
						hum2 = "Low Humidity";
						int rValueT = myHumi.getLowHumidity(50);
						humi[i] = rValueT;
						System.out.println("Low Humidity = " + rValueT);
					}
				}

				else if( humidity == 3){
					for(int i = 0; i<counter; i++)	//For loop used to generate required number of values
					{
						hum2 = "High Humidity";
						int rValueT = myHumi.getHighHumidity(50,100);
						humi[i] = rValueT;
						System.out.println("High Humidity = " + rValueT);
					}
				}

				int maxValueT = temp[0];  
				for(int i=1;i  maxValueT)
					{  
						maxValueT = temp[i];		
					}
				}

				int minValueT = temp[0];  
				for(int i=1;i < counter; i++) //For loop used to test for Min Value in array
				{  
					if(temp[i] < minValueT) 
					{  
						minValueT = temp[i];	
					}
				}

				int sumValueT = temp[0];
				for(int i=1 ;i < counter; i++) //For Loop used to add all values in array
				{  
					sumValueT = sumValueT + temp[i];
				}

				int aveValueT = sumValueT/counter; //Operation for getting average for values in array

				int maxValueH = humi[0];  
				for(int i=1;i  maxValueH)
					{  
						maxValueH = humi[i];	
					}
				}

				int minValueH = humi[0];  
				for(int i=1;i < counter; i++) //For loop used to test for Min Value in array
				{  
					if(humi[i] < minValueH) 
					{  
						minValueH = humi[i];	
					}
				}

				int sumValueH = humi[0];
				for(int i=1 ;i < counter; i++) //For Loop used to add all values in array
				{  
					sumValueH = sumValueH + humi[i];
				}

				int aveValueH = sumValueH/counter; //Operation for getting average for values in array

				/* Lines used to generate the different desired outputs to user*/
				System.out.println("");
				System.out.println("Season = " + season1 + season2);
				System.out.println("First Temp Generated = " + temp[0]);
				System.out.println("Last Temp Generated = " + temp[counter-1]);
				System.out.println("Highest Temp Generated = " + maxValueT);
				System.out.println("Lowest Temp Generated = " + minValueT);
				System.out.println("Sum of Temp Generated = " + sumValueT);
				System.out.println("Average of Temp Generated = " +aveValueT);
				System.out.println("");
				System.out.println("Humidity = " + hum1 + hum2);
				System.out.println("First Humidity Generated = " + humi[0]);
				System.out.println("Last Humidity Generated = " + humi[counter-1]);
				System.out.println("Highest Humidity Generated = " +maxValueH);
				System.out.println("Lowest Humidity Generated = " + minValueH);
				System.out.println("Sum of Humidities Generated = " + sumValueH);
				System.out.println("Average of Humidities Generated = " + aveValueH);
				System.out.println("");

				boolean valid3;		//Boolean used for do...while loop to catch exception in repeat simulation question.
				do {
					try {
						valid3 = true;
						System.out.println("Would you like to run the simuluation again? (Yes = 1 / No = 2)");
						again = scan1.nextInt();

						/*  If selection statement to test for end of simulation or repeat*/
			            if (again == 1){			
			            	startOver = true;
			            	season1 = "";
			            	hum1 = "";

			            }
			            else if (again == 2)
							startOver = false;
			            else if (again != 1 | again != 2 )
			                throw new InputMismatchException();
						}
						catch ( InputMismatchException nfe ) {		//Valid Input Check
							System.out.println( "Not a matching input" );
							valid3 = false;
							scan1.nextLine();	
							}

					}while (!valid3);	//End of do..while loop used for try/catch exception in end or repeat question

		}while (startOver);		//End of do...while loop used to Start Over Program Again

		System.out.println("Thank you!");
	}
}

Screenshot:

Lab3

 

Leave a Reply

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