A City Tech OpenLab ePortfolio

Lab 2

Lab Description:
The idea of Lab 2 basically is to re-create Lab 1 with separating it into 2 classes which is TemperatureSensor and TemperatureSensorStats. In the TemperatureSensor class, we need to implement four kinds of methods which include getWinterTemp that simulate temperature between 20-40, getSpringTemp that simulate temperature between 40-70, getSummerTemp that simulate temperature between 70-90, and getFallTemp that simulate temperature between 40-60. On the other hand, the other class TemperatureSensorStats will consist of a main menu that have 5 options of the season with and exit. Whenever the user asked to do simulation on this class, the class should call the corresponding method from the TemperatureSensor class we created separately. Other than that, the program still keep track of all the statistics which include first temperature, last temperature, lowest temperature, highest temperature, total sum of all temperature and average temperature generated in each season just like in Lab 1.

Code:
TemperatureSensor Code:

public class TemperatureSensor {
	
	int maxValue = 0;  // Will hold Max Value Generated by Sensor
	int minValue = 0;  // Will hold Min Value Generated by Sensor
	int sumValue = 0;  // Will hold Sum of Values Generated by Sensor
	int avgValue = 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  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];
			}
			
			avgValue = sumValue/counter; // Operation for getting average for values in array
			
			/* Lines used to generate the different desired outputs to user*/
			System.out.println("First Temperature Simulated");
			System.out.println(temp[0]);
			System.out.println("Last Temperature Simulated");
			System.out.println(temp[counter-1]);
			System.out.println("Highest Temperature Simulated");
			System.out.println(maxValue);
			System.out.println("Lowest Temperature Simulated");
			System.out.println(minValue);
			System.out.println("Sum of Temperatures Simulated");
			System.out.println(sumValue);
			System.out.println("Average of Temperatures Simulated");
			System.out.println(avgValue);
			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  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];
			}
			
			avgValue = sumValue/counter;
			
			System.out.println("First Temperature Simulated");
			System.out.println(temp[0]);
			System.out.println("Last Temperature Simulated");
			System.out.println(temp[counter-1]);
			System.out.println("Highest Temperature Simulated");
			System.out.println(maxValue);
			System.out.println("Lowest Temperature Simulated");
			System.out.println(minValue);
			System.out.println("Sum of Temperatures Simulated");
			System.out.println(sumValue);
			System.out.println("Average of Temperatures Simulated");
			System.out.println(avgValue);
			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  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];
			}
			
			avgValue = sumValue/counter;
			
			System.out.println("First Temperature Simulated");
			System.out.println(temp[0]);
			System.out.println("Last Temperature Simulated");
			System.out.println(temp[counter-1]);
			System.out.println("Highest Temperature Simulated");
			System.out.println(maxValue);
			System.out.println("Lowest Temperature Simulated");
			System.out.println(minValue);
			System.out.println("Sum of Temperatures Simulated");
			System.out.println(sumValue);
			System.out.println("Average of Temperatures Simulated");
			System.out.println(avgValue);
			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  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];
			}
			
			avgValue = sumValue/counter;
			
			System.out.println("First Temperature Simulated");
			System.out.println(temp[0]);
			System.out.println("Last Temperature Simulated");
			System.out.println(temp[counter-1]);
			System.out.println("Highest Temperature Simulated");
			System.out.println(maxValue);
			System.out.println("Lowest Temperature Simulated");
			System.out.println(minValue);
			System.out.println("Sum of Temperatures Simulated");
			System.out.println(sumValue);
			System.out.println("Average of Temperatures Simulated");
			System.out.println(avgValue);
			return 0;
	
	}
		
}

TemperatureSensorStats Code:

import java.util.InputMismatchException; // Import try/catch function.
import java.util.Scanner; // Import scanner to get input from user.

public class TemperatureSensorStats {

	
	private static Scanner scan1;

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

		
		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 = 5)
						valid1 = true;
	            	else if (season  5 )
						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 == 5 )	// Exit selection From Main 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 repetitions
			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 use only 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 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)			
			            	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
		
	}
}

Screenshot:
Lab 2

Leave a Reply

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