A City Tech OpenLab ePortfolio

Lab 3

Lab Description:
Lab 3 is basically a lab 2 recreation but with some modification using inheritance. First of all we need to create a class call Sensor which contain an int field reading and 2 kind of methods which is setReading and getReading. After that we are require to create 2 other classes call TemperatureSensor and HumiditySesor. In TemperatureSensor, we need to implement the getWinterTemp, getSpringTemp, getSummerTemp, and getFallTemp methods by calling the super class methods appropriately. On the other hand, we need to implement getHumidity, getLowHumidity, and getHighHumidity in the HumiditySensor class. Lastly we need to create the SensorsStatsApp class that do all the calculation and display a menu to ask the user what simulation they want to use and after the simulation, the application should display the results for the user.

Code:
Sensor:

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:

public class TemperatureSensor extends Sensor{

	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:

public class HumiditySensor extends Sensor{

	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 );
	}
}

SensorsStatsApp:

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

public class SensorsStatsApp {

	private static Scanner scan1;

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

		scan1 = new Scanner(System.in);
		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 seasonStr = null;				//String used to hold the season for output
		String humidityStr = null;				//String used to hold the humidity for output
		String randomSeason = " ";				//String hold random for season
		String randomHumidity = " ";			//String hold random for humidity

		/*  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();

				}
				catch (InputMismatchException nfe) {		// Number Check Exception
					System.out.println( "Invalid input. Please try again using only number." ); 
					valid1 = false;
					scan1.next();
				}
				catch (NumberFormatException nfe) {			// Range check exception
					System.out.println("Number is out of range. Please try again.");
					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
			{
				System.out.println("Thank you for using this simulation. Hope you get what you are looking for. :)");
				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();

				}
				catch ( InputMismatchException nfe) {		//Number Check Exception
					System.out.println( "Invalid input. Please try again using only number." ); 
					valid4 = false;
					scan1.next();
				}
				catch (NumberFormatException nfe) {			//Range check exception
					System.out.println("Number is out of range. Please try again.");
					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
			{
				System.out.println("Thank you for using this simulation. Hope you get what you are looking for. :)");
				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( "Invalid input. Please try again using only 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*/

			System.out.println("Temperature Generated:");
			if( season == 5){
				randomSeason = " Random: ";
				season = 1 + (int)(Math.random() * ((4 - 1) + 1));
			}

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

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

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

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


			System.out.println("Humidity Generated:");
			if( humidity == 4){
				randomHumidity = " Random: ";
				humidity = 1 + (int)(Math.random() * ((3 - 1) + 1));
			}

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

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

			else if( humidity == 3){
				humidityStr ="High";
				for(int i = 0; i<counter; i++)	//For loop used to generate required number of values
				{
					int rValueT = myHumi.getHighHumidity(50,100);
					humi[i] = rValueT;
					System.out.println(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 avgValueT = 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 avgValueH = 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 =" + randomSeason + seasonStr);
			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 = " + avgValueT);
			System.out.println("");
			System.out.println("Humidity =" + randomHumidity + humidityStr);
			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 = " + avgValueH);
			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 re-run the simuluation? (Yes = 1 / No = 2)");
					again = scan1.nextInt();

					/*  If selection statement to test for end of simulation or repeat*/
					if (again == 1){		
						randomSeason = " ";
						randomHumidity = " ";
						startOver = true;
					}
					else if (again == 2){
						startOver = false;
						System.out.println("Thank you for using this simulation. Hope you get what you are looking for. :)");
					}
					else if (again != 1 | again != 2 )
						throw new InputMismatchException();
				}
				catch ( InputMismatchException nfe ) {		//Valid Input Check
					System.out.println( "Invalid input. Please try again." );
					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

	}
}

Screenshots:
Lab 3

Leave a Reply

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