Lab 2

Temperature Sensor Simulator
Description

In this lab the objective is to re-create the first labĀ using 2 Separate Classes. The first Class I created was the TemperatureSensor Class. This class was created to store the minimun and maximun values for each season. This value can be retrieved with get methods, I created a get method for every season using the format “getSeasonName()”. The second class is calledĀ TemperatureSensorStats. This class contains the main method which is the method that is used to run the program.

How It Works

I created this program to give a menu to the user. It displays 5 options for the user to select from. In this lab I implemented the try catch blocks available in java. I had to learn how to use these components, I managed to get it to check if the user is Entering the correct format which is expecting in this case a Integer value. One a option is selected it proceeds to ask how many times does the user want to run the simulation. Again the program checks if the entry is valid. This part of the program only accepts positive integers.

CODE:

public class TemperatureSensor { 
        //Random method takes in upper limit sends Random values 1 less than limit 
        private static int setRandom(int num){ 
            return (int)(num * Math.random()); 
        } 
        //returns winter random generated values between 20 - 40 
        static int getWinterTemp(){ 
            int W; 
            do{  
                W = setRandom(41); 
            }while(W40); //range setup 
            return W; 
        } 
        //returns spring random generated values between 40 - 70 
        static int getSpringTemp(){ 
            int Sp; 
            do{  
                Sp = setRandom(71); 
            }while(Sp70); 
            return Sp; 
        } 
        //returns summer random generated values between 40 - 70 
        static int getSummerTemp(){ 
            int Sm; 
            do{  
                Sm = setRandom(91); 
            }while(Sm90); 
            return Sm; 
        } 
        static int getFallTemp(){ 
            int F; 
            do{  
                F = setRandom(61); 
            }while(F60); 
            return F; 
        }        
        static void disMenu(){ 
            System.out.println("Temperature Sensor Simulator"); 
            System.out.println("Which Season would you like to simulate"); 
            System.out.println("\t[1] Winter\n\t[2] Spring \n\t[3] Summer \n\t[4] Fall \n\t[5] Exit"); 
        } 
        static void disResults(int times, int low, int high, int first, int last, int sum){ 
            System.out.printf("\tTimes Run= %d\n",times); 
            System.out.printf("\tFirst Value= %d\n\tLast Value= %d\n",first,last); 
            System.out.printf("\tHighest Value= %d\n\tLowest= %d\n",high,low); 
            System.out.printf("\tSum Value= %d\n\tAverage= %d\n\n",sum,(sum/times)); 
        } 
} 
___________________________________________________________________________________
import java.util.Scanner; 
import java.io.*; 

public class TemperatureSensorStats  
{ 
    static Scanner input = new Scanner(System.in); //Creates Scanner Object to be able to read from the keyboard 
    static TemperatureSensor temp = new TemperatureSensor(); //creates a temperature object 

    public static void main(String[] args)  
    { 
        //Variables Used in program 
        int season = 0,random=0, times = 0,first=0, greatest = 0,lowest=0,last=0,sum=0; 
        boolean run=true; 
        //looping Program 

        do
        { 
            try{ 
                sum = 0;    //resets the sum back to 0 
                temp.disMenu(); //displays selection menu 
                season = input.nextInt();//user selection 
                try{ 
                    while(run){ 
                        System.out.println("How many numbers would you like to generate?"); //ask user how many numbers to generate 
                        times = input.nextInt(); 
                        run=false; 
                    } 
                }catch(Exception e){ 
                    System.out.println("Please Enter a Valid Positive Integer Number\n"); 
                    input.nextLine(); 
                    System.out.println("How many numbers would you like to generate?"); //ask user how many numbers to generate 
                    times = input.nextInt(); 
                } 
                run = true; 
                if(times>0){ 
                    if(season>0 && season <5) 
                    { 
                        for(int i=0; i<times; i++) 
                        {  
                            if(season == 1) 
                                random = temp.getWinterTemp();  
                            else if(season == 2) 
                                random = temp.getSpringTemp(); 
                            else if(season == 3) 
                                random = temp.getSummerTemp(); 
                            else if(season == 4) 
                                random = temp.getFallTemp(); 
                            if(i==0) 
                                first = greatest = lowest = random; 
                            else if(i==(times-1)) 
                                last=random; 
                            if(randomgreatest) 
                                greatest = random; 
                            sum += random; 
                        } 
                        temp.disResults(times, lowest, greatest, first, last, sum); 
                    } 
                    else if(season != 5 && season != 0) 
                    { 
                        System.out.print("You Have Enter and Invalid Entry Please Select From Available Selection\n"); 
                        System.out.println("If you Wish To Exit Please Select 5\n"); 
                    } 
                    else
                        continue; 
                }else{ 
                    System.out.println("You have Enter an Invalid Run Amount\n"); 
                } 
            } 
            catch(Exception a) 
            { 
                System.out.println("You have Entered an Invalid Entry please only input Integers\n"); 
                input.nextLine(); 
            } 
        } 
        while(season != 5); 
    }  
}
OpenLap Often Crops the codes and deletes special characters
In case this happens the full code can be downloaded by clicking this link

Code: TemperatureSensor TemperatureSensorStats
ScreenShot

1 2

Leave a Reply

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