Lab 2 — Temperature Generator with the Use of Classes

Lab Description:

Lab 2 is very similar to Lab 1 in the fact that it is using Temperature Generator program. But Lab 2 uses two separate classes, one called TemperatureSensorStats which contains the menu to call the temperature generator functions and the other class is called the Temperature Sensor which contains all the temperature generator functions for each season. Using a separate class (TemperatureSensorStats) to call the temperature generator functions, a new object must be made with a constructor using the TemperatureSensor class. In the main public class “TemperatureSensorStats”, the program asks the user what season would they like to simulate. When the user enters a menu number, the program goes into the switch and goes to the specific menu option, and within that menu option it callls the “tempgen()”, and “winter()” (winter is being used as an example, replace with function with the season chosen) from the TemperatureSensor class. After the functions are called then the program emulates the temperatures for the season they chosen based on the amount of simulations they requested. Upon exiting the program the user is asked if they would like to continue, and then based on that decision it changes boolean variable “value” to true or false which then either stops or continues the execution of the do while which the whole program is contained in. The TemperatureSensor class contains the following functions, tempgen(), winter(), spring(), summer(), fall(). The tempgen() function uses a system.out.println and asks the user how many simulations would they like to do, and accepts the users input. The winter(), spring(), summer(), fall() functions generate the temperatures by a for loop which contains a new object from the Random class that will generate the random temperatures. The for loop containing the random object will stop until it reaches the desired amount of simulations and output desired amount of random numbers, the sum, the average, highest temperature, lowest temperature, first temperature outputted, and the last temperature outputted.

Code:

---TemperatureSensorStats.java-----
import java.util.Scanner;


public class TemperatureSensorStats {
    public static void main(String main[]){
        //boolean value;
        int decision = 0, cont = 1;
        
        //Create new object from TemperatureSensor class.
        TemperatureSensor climate;
        climate = new TemperatureSensor();
        
        
        
                
        //do while, runs if bool value is true
        do{
            try {
            System.out.println("What season would you like to simulate?, Type in the season in lower case letters or according number menu number");
            System.out.println("Enter 1 (winter), 2 (spring), 3 (summer), 4 (fall), or 5 (exit the program)");
            
            //Create input for the question for temperature generator.
            Scanner userInput = new Scanner(System.in);
            
            //User input for menu decision
            decision = userInput.nextInt();    
            
            
                //Location of the menu options which contain function calls to the TemperatureSensor class.
                switch (decision){
                
                case 1: //Option 1 on the menu, Winter temperature generator.
                    
                    climate.tempgen(); //Call the tempgen function to ask the user for how many simulations.
                    climate.winter(); //Call the winter temperature generator function.
                    
                    break;
    
                case 2: //Option 2 on the menu, Spring temperature generator.
                    
                    climate.tempgen(); //Call the tempgen function to ask the user for how many simulations.
                    climate.spring(); //Call the spring temperature generator function.
                    
                    break;
    
                case 3: //Option 3 on the menu, Summer temperature generator.
                    
                    climate.tempgen(); //Call the tempgen function to ask the user for how many simulations.
                    climate.summer(); //Call the summer temperature generator function.
                    
                    break;
                    
                case 4: //Option 4 on the menu, Fall temperature generator.
                    
                    climate.tempgen(); //Call the tempgen function to ask the user for how many simulations.
                    climate.fall(); //Call the fall temperature generator function.
                    
                    break;
                
                case 5: // Option 5 to exit the program
                    
                    System.out.println("Exiting program now");
                    System.exit(0);
                    break;
                    
                default:
                    
                    System.out.println("Please enter correct menu number");
                    break;
                        
                } //end of switch statement
                
            } //end of the try statement
            
            catch (Exception e){ //If user enters any character or number other then the ones listed in the menu then the program will redirect the user.
                System.out.println("Please enter menu number, letters are not supported.");
                
            }

            
        }while(cont == 1);
        
        
    

    }//end bracket for 'public static void main(String main[]){'
    
} //end bracket for 'public class TemperatureSensorStats {'

-----------------------------TemperatureSensor.java--------------------
import java.util.Random;
import java.util.Scanner;

public class TemperatureSensor {
int temp, count, max = 0, min = 0, maxtemp, mintemp, total = 0, sum = 0, average = 0, lastnum, finalnum = 0;

	//Create new object for scanner and random
	Scanner userInput = new Scanner(System.in);
	Random r = new Random();

	//Gets the users input 
	void tempgen(){

		System.out.println("How many tempuratures would you like to simulate");

		//Get users input

		temp = userInput.nextInt();
	}

	void winter(){ //For Winter generate numbers between 20-40.	

		//Upper and lower bounds for max and min temperatures.
		mintemp = 40;
		maxtemp = 20;

		//Using a for loop generate random temperatures up to users requested amount of simulations
		for(count = 0; count 0){
				max = Math.max(max, randomNum);
				min = Math.min(min, randomNum);
			}

			sum += randomNum;
			finalnum = randomNum; //final number outputted

		} 

		lastnum = finalnum;

		total += sum; // The sum of all the temperatures generated.

		average += (total / temp); // average = total / 'number of temperatures generated'

		//Outputs last number generated, Max, Min, Total sum, Average values
		if (count == temp){
			System.out.println("The last temperature generated is: " + lastnum);
			System.out.println(); //Create space from the other values going to be displayed.
			System.out.println("The max temperature is: " + max);
			System.out.println("The minimum temperature is: " + min);
			System.out.println("The sum of temperatures is: " + total);
			System.out.println("The average temperature is: " + average);
		}
	}

	void spring(){ //For Spring generate numbers between 40-70.

		//Upper and lower bounds for max and min temperatures.
		maxtemp = 40; 
		mintemp = 70;

		for(count = 0; count 0){
				max = Math.max(max, randomNum);
				min = Math.min(min, randomNum);
			}
			sum += randomNum;
			finalnum = randomNum;
		}

		lastnum = finalnum;

		total += sum; // The sum of all the temperatures generated.

		average += (total / temp); // average = total / 'number of temperatures generated'

		//Outputs last number generated, Max, Min, Total sum, Average values
		if (count == temp){
			System.out.println("The last temperature generated is: " + lastnum);
			System.out.println(); //Create space from the other values going to be displayed.
			System.out.println("The max temperature is: " + max);
			System.out.println("The minimum temperature is: " + min);
			System.out.println("The sum of temperatures is: " + total);
			System.out.println("The average temperature is: " + average);
		}
	}

	void summer(){ //For Summer generate numbers between 70-90.

		//Upper and lower bounds for max and min temperatures.
		maxtemp = 70;
		mintemp = 90;

		//Using a for loop generate random temperatures up to users requested amount of simulations
		for(count = 0; count 0){
				max = Math.max(max, randomNum);
				min = Math.min(min, randomNum);
			}

			sum += randomNum;
			finalnum = randomNum;
		}

		lastnum = finalnum;
		total += sum; // The sum of all the temperatures generated.

		average += (total / temp); // average = total / 'number of temperatures generated'

		//Outputs last number generated, Max, Min, Total sum, Average values
		if (count == temp){
			System.out.println("The last temperature generated is: " + lastnum);
			System.out.println(); //Create space from the other values going to be displayed.
			System.out.println("The max temperature is: " + max);
			System.out.println("The minimum temperature is: " + min);
			System.out.println("The sum of temperatures is: " + total);
			System.out.println("The average temperature is: " + average);
		}
	}

	void fall(){ //For Fall generate numbers between 40-60.

		//Upper and lower bounds for max and min temperatures.
		maxtemp = 40;
		mintemp = 60;

		//Using a for loop generate random temperatures up to users requested amount of simulations
		for(count = 0; count 0){
				max = Math.max(max, randomNum);
				min = Math.min(min, randomNum);
			}
			sum += randomNum;
			finalnum = randomNum;
		}

		lastnum = finalnum;

		total += sum; // The sum of all the temperatures generated.

		average += (total / temp); // average = total / 'number of temperatures generated'

		//Outputs last number generated, Max, Min, Total sum, Average values
		if (count == temp){
			System.out.println("The last temperature generated is: " + lastnum);
			System.out.println(); //Create space from the other values going to be displayed.
			System.out.println("The max temperature is: " + max);
			System.out.println("The minimum temperature is: " + min);
			System.out.println("The sum of temperatures is: " + total);
			System.out.println("The average temperature is: " + average);
		}
	}

}

 

Screenshots:

Lab 2

Leave a Reply

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