A City Tech OpenLab ePortfolio

LAB 2 – METHODS AND CLASSES

LAB 2 – Classes and Methods

Lab Description:

In this lab, the temperature sensor created in the lab #1 will be recreated but it will be separated into classes. A class which will be named TemperatureSensor which will be used to implement the different get Methods that will create the different season simulations. Like in lab #1, each simulation will generate a random set of numbers on a range dependent on the season. For winter the numbers range from 20 – 40, for spring numbers range from 40 – 70, for summer numbers range from 70 – 90, and for fall numbers range from 40 – 60 except this will now be implemented with the method calls getWinterTemp, getSpringTemp, getSummerTemp, and getFallTemp. Each method called will be in charge of keeping track of the statistics of 1) First temperature generated, 2) Last temperature generated, 3) Lowest temperature generated, 4) Highest temperature generated, 5) Sum of all temperatures generated, and 6) Average for the season.

The Second Class created will be the TemperatureSensorStats which will be the one to implement the application. This will be the class in charge of presenting the menu to the user and ask what season would like to be simulated or if the program should exit. If a season is selected, then it asks the user the number of simulations that should be run. From there, depending on the input, the corresponding method will be called from the TemperatureSensor class.

In addition to this, try/catch statements will be included in the code in order to detect any invalid input from the user. These statements will throw exceptions, letting the user know of the invalid input and display the question again until a valid input is entered.

Code:

TemperatureSensor class code:

public class TemperatureSensor {

	private int maxValue = 0;  //Will hold Max Value Generated by Sensor
	private int minValue = 0;  //Will hold Min Value Generated by Sensor
	private int sumValue = 0;  //Will hold Sum of Values Generated by Sensor
	private int aveValue = 0;  //Will hold Average of Values Generated by Sensor

	/* getWinter Temp code below. Code is basically a repetition in all the get methods*/
	public int getWinterTemp(int counter)
	{
		int temp[] = new int[counter];	//New Array initialized for required number of values
		int rand;						//Variable used to hold random generated value
		for(int i = 0; i<counter; i++)	//For loop used to generate required number of values
		{
			rand = 20 + (int)(Math.random() * ((40 - 20) + 1));
			temp[i] = rand;
			System.out.println(rand);
		}
			maxValue = temp[0];  
			for(int i=1;i < counter; i++) //For loop used to test for the Max Value in array 			{   				if(temp[i] > maxValue)
				{  
					maxValue = temp[i];

				}

			}

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

				}

			}

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

			aveValue = sumValue/counter; //Operation for getting average for values in array

			/* Lines used to generate the different desired outputs to user*/
			System.out.println("First Temp Generated");
			System.out.println(temp[0]);
			System.out.println("Last Temp Generated");
			System.out.println(temp[counter-1]);
			System.out.println("Highest Temp Generated");
			System.out.println(maxValue);
			System.out.println("Lowest Temp Generated");
			System.out.println(minValue);
			System.out.println("Sum of Temp Generated");
			System.out.println(sumValue);
			System.out.println("Average of Temp Generated");
			System.out.println(aveValue);
			return 0;

	}

	/* getSpring Temp code below. Code is basically a repetition in all the get methods*/
	public int getSpringTemp(int counter)
	{
		int temp[] = new int[counter];
		int rand;
		for(int i = 0; i<counter; i++)
		{
			rand = 40 + (int)(Math.random() * ((70 - 40) + 1));
			temp[i] = rand;
			System.out.println(rand);
		}
			maxValue = temp[0];  
			for(int i=1;i < counter; i++) 			{   				if(temp[i] > maxValue)
				{  
					maxValue = temp[i];

				}

			}

			minValue = temp[0];  
			for(int i=1;i < counter; i++)
			{  
				if(temp[i] < minValue)
				{  
					minValue = temp[i];

				}

			}

			sumValue = temp[0];
			for(int i=1 ;i < counter; i++)
			{  
				sumValue = sumValue + temp[i];
			}

			aveValue = sumValue/counter;

			System.out.println("First Temp Generated");
			System.out.println(temp[0]);
			System.out.println("Last Temp Generated");
			System.out.println(temp[counter-1]);
			System.out.println("Highest Temp Generated");
			System.out.println(maxValue);
			System.out.println("Lowest Temp Generated");
			System.out.println(minValue);
			System.out.println("Sum of Temp Generated");
			System.out.println(sumValue);
			System.out.println("Average of Temp Generated");
			System.out.println(aveValue);
			return 0;

	}

	/* getSummer Temp code below. Code is basically a repetition in all the get methods*/
	public int getSummerTemp(int counter)
	{
		int temp[] = new int[counter];
		int rand;
		for(int i = 0; i<counter; i++)
		{
			rand = 70 + (int)(Math.random() * ((90 - 70) + 1));
			temp[i] = rand;
			System.out.println(rand);
		}
			maxValue = temp[0];  
			for(int i=1;i < counter; i++) 			{   				if(temp[i] > maxValue)
				{  
					maxValue = temp[i];

				}

			}

			minValue = temp[0];  
			for(int i=1;i < counter; i++)
			{  
				if(temp[i] < minValue)
				{  
					minValue = temp[i];

				}

			}

			sumValue = temp[0];
			for(int i=1 ;i < counter; i++)
			{  
				sumValue = sumValue + temp[i];
			}

			aveValue = sumValue/counter;

			System.out.println("First Temp Generated");
			System.out.println(temp[0]);
			System.out.println("Last Temp Generated");
			System.out.println(temp[counter-1]);
			System.out.println("Highest Temp Generated");
			System.out.println(maxValue);
			System.out.println("Lowest Temp Generated");
			System.out.println(minValue);
			System.out.println("Sum of Temp Generated");
			System.out.println(sumValue);
			System.out.println("Average of Temp Generated");
			System.out.println(aveValue);
			return 0;

	}

	/* getFallr Temp code below. Code is basically a repetition in all the get methods*/
	public int getFallTemp(int counter)
	{
		int temp[] = new int[counter];
		int rand;
		for(int i = 0; i<counter; i++)
		{
			rand = 40 + (int)(Math.random() * ((60 - 40) + 1));
			temp[i] = rand;
			System.out.println(rand);
		}
			maxValue = temp[0];  
			for(int i=1;i < counter; i++) 			{   				if(temp[i] > maxValue)
				{  
					maxValue = temp[i];

				}

			}

			minValue = temp[0];  
			for(int i=1;i < counter; i++)
			{  
				if(temp[i] < minValue)
				{  
					minValue = temp[i];

				}

			}

			sumValue = temp[0];
			for(int i=1 ;i < counter; i++)
			{  
				sumValue = sumValue + temp[i];
			}

			aveValue = sumValue/counter;

			System.out.println("First Temp Generated");
			System.out.println(temp[0]);
			System.out.println("Last Temp Generated");
			System.out.println(temp[counter-1]);
			System.out.println("Highest Temp Generated");
			System.out.println(maxValue);
			System.out.println("Lowest Temp Generated");
			System.out.println(minValue);
			System.out.println("Sum of Temp Generated");
			System.out.println(sumValue);
			System.out.println("Average of Temp Generated");
			System.out.println(aveValue);
			return 0;

	}

}

TemperatureSensorStats class code:

import java.util.*; // Imported all java.util class since needed to handle exceptions in code.

public class TemperatureSensorStats {

	/**
	 * @param args
	 */
	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 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

		/*  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.Exit");
					season = scan1.nextInt();

					/*  If...else if selection used to check for correct integer selection Exception*/
					if (season <= 1 && season >= 5)
						valid1 = true;
	            	else if (season < 1 || season > 5 )
						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 == 5 )	//Exit selection From Main 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 mySensor = new TemperatureSensor();	//New Temperature Sensor object created

				/*  If Selection statements used to call the required method depending on selection of season*/
				if( season == 1 )
					mySensor.getWinterTemp(counter);

				if( season == 2 )
					mySensor.getSpringTemp(counter);

				if( season == 3 )
					mySensor.getSummerTemp(counter);

				if( season == 4 )
					mySensor.getFallTemp(counter);

				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;
			            else if (again == 2){
							startOver = false;
							System.out.println("Thank you!");
							}
			            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

	}
}

Screenshot:

Lab2_Running

Leave a Reply

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