Lab1

“TEMPERATURE SIMULATION”

Lab Description:

This lab will simulate the temperature for four season of the years. User will be asked for input and how many times to simulation. In this lab, student will be able to study some Java new class such as: Random, Scanner and new loops: for, do while.

It will get some trouble when using the Random class because it only pick the random number start from 0, so if we want to pick range from 0-20, we need to input “RanTemp = RanNum.nextInt(21);”, we need to input 21 instead of 20 because there are 21 number from 0 to20.

Student also need to arrange the code to get the first, last, high and low temperature. As the result, they will get only one value at output and it will overlap old temperature. In addition, when new temperature is simulated, it will updated and add total and at the end, it will calculate the average.

CODE:

/* Huy Ly
* CET 3640 SecTion:D476
* Date: Feb 3, 2014
*/
import java.util.Random;
import java.util.Scanner;

public class TemperatureSensor
{
       public static void main(String[] args)
       {
             Scanner keyboard = new Scanner(System.in);
             int season;
             //Starting the program
             do
             {
                  //Asking for season
                  System.out.println("Please! Enter the season for simulating:");
                  System.out.println("Season Code: Winter(1); Spring(2);Summer(3);Fall(4); (5) to Exit");
                  season = keyboard.nextInt();

                  //Random range
                  int RanPick=21;

                  //Set up Default Temperature
                  int DefaultTemp = 0;

                 //First Temperature
                 int FirstTemp=0;
                 //Last Temperature
                 int LastTemp=0;

                 //High Temperature
                 int HighTemp=0;
                //Low Temperature
                int LowTemp=0;

                //Total Temperature
                double TotalTemp=0;
                //Average Temperature
                double AveTemp;

                //Checking for input value
                if(season==1)
                     DefaultTemp=20;
                else if (season==2)
                {
                     DefaultTemp=40;
                     RanPick=31;
                }
                else if (season==3)
                    DefaultTemp=70;
                else if (season==4)
                    DefaultTemp=40;
                else
                    System.exit(0);

                //Asking for simulation
                System.out.println("How many time do you want to simulate?");
                int input = keyboard.nextInt();

                //Random class
                Random RanNum = new Random();

                //Starting simulation based on input
                for ( int int i=0; i<input; i++)
                {
                      int RanTemp = RanNum.nextInt(RanPick);

                      //Calculate Total Temperature:
                      TotalTemp+=(RanTemp+DefaultTemp);
                      System.out.println("Current Temperature is: "+(RanTemp+DefaultTemp) + " Degree");

                      //Assign for first, high and low temperature
                      if (i==0)
                      {
                            FirstTemp=RanTemp+DefaultTemp;
                            HighTemp=DefaultTemp+RanTemp;
                            LowTemp=DefaultTemp+RanTemp;
                     }
                     //Assign for last temperature
                     if (i==(input-1))
                          LastTemp=RanTemp+DefaultTemp;

                     //Checking for high and low temperature
                     if((RanTemp+DefaultTemp)>HighTemp)
                          HighTemp=DefaultTemp+RanTemp;
                     if((RanTemp+DefaultTemp)<LowTemp)
                          LowTemp=DefaultTemp+RanTemp;
                }

                //Calculate Average Temperature
                AveTemp=TotalTemp/input;

                //Showing user the Output
                System.out.println("Total Temperature of the season is: " + TotalTemp + " Degree");
                System.out.println("Average Temperature of the season is: " + AveTemp + " Degree");
                System.out.println("First Temperature of the season is: " + FirstTemp + " Degree");
                System.out.println("Last Temperature of the season is: " + LastTemp + " Degree");
                System.out.println("Highest Temperature of the season is: " + HighTemp + " Degree");
                System.out.println("Lowest Temperature of the season is: " + LowTemp + " Degree");

                //Asking for simulating again
                System.out.println("Do you want to simulate again? (1==Yes;5==No)");
                season = keyboard.nextInt();

             }while(season!=5);
       }

}

 

SCREEN SHOT :

TemPicture