Lab 2

The objective of this lab is to create a temperature sensor simulator similar to lab 1. This time, we must separate the temperatures in different classes. First we must create a class called Temperature sensor which will include different methods for each season; getWinterTemp, getSpringTemp, getSummerTemp, and getFallTemp. Each simulation will produce a random numbers as follows: For winter a number between 20-40, for spring between 40-70, for summer between 70-90, and for fall between 40-60. We must then create another class called, TemperatureSensorStats that will run the program. In the program  we must display a menu that would ask the user what season to simulate (1) winter (2) spring (3) summer (4) fall or (5) to exit. If the user input values that are not in the choice option, we must prompt the user that he or she as enter the wrong value using the try and catch command.

We must then display these results to the user. The Program must run until the user exits it.

1-      First temperature generated

2-      Last temperature generated

3-      Lowest temperature generated

4-      Highest temperature generated

5-      Total sum of all temperatures generated

6-      Average for the season

 

 

Source Code:

import java.util.Random;
import java.util.Scanner;

public class Temperature_Sensor {

private int winter_Temp;
private int spring_Temp;
private int summer_Temp;
private int fall_Temp;
static int Temp;
static Random  generator = new Random();

public int getwintertemp(){

Temp=generator.nextInt(20) + 20;
System.out.printf(“The WinterTemperature is %d\n”, Temp);

return winter_Temp;
}
public int getspringtemp(){

Temp=generator.nextInt(30) + 40;
System.out.printf(“The Spring Temperature is %d\n”, Temp);

return spring_Temp;
}
public int getsummertemp(){

Temp=generator.nextInt(20) + 70;// generate a value from 70 to 90
System.out.printf(“The Summer Temperature is %d\n”, Temp);

return summer_Temp;
}
public int getfalltemp(){

Temp=generator.nextInt(20) + 40;// generate a value from 40 to 60
System.out.printf(“The Fall Temperature is %d\n”, Temp);

return fall_Temp;
}

public static  class TempSenStats{

public static void main(String[] args) {
try{
Temperature_Sensor ts = new Temperature_Sensor();
// int Temperature = ts.getwintertemp();

Scanner input = new Scanner( System.in ); //Allows the USER to be able to read the user’s input

int season; //initialize the variable season
int simulation;//initialize the variable for the simulation
int time=0;
while(time<5)//infinity loop
{

System.out.println(“What is the season?”);//display the message
System.out.println(“Press 1 for Winter.”);//display the message
System.out.println(“Press 2 for Spring.”);//display the message
System.out.println(“Press 3 for Summer.”);//display the message
System.out.println(“Press 4 for Fall.”);//display the message
System.out.println(“Press 5 to exit the program.”);//display the message

season = input.nextInt(); //reads the input from the user
if(season>5) {
System.err.println(“You have to enter numbers from 1 to 5”);
continue; // goes back to the top of the loop
}
if(season == 5)
{
System.out.println(“Thank you for using the Program”);
System.out.println(“Good Bye!!”);
System.exit(0);// exits the program if the user enter 5
}
else//loop for the season
{

System.out.println(“How many simulations?”);//display the message
simulation = input.nextInt(); //reads the input from the user

int firstTemp;
int lastTemp;
int lowestTemp;
int highestTemp;
int sumTemp ; //variable for the sum of the first season
int averageTemp = 0;//variable for the average of the season
Temp=generator.nextInt(20) + 40;

firstTemp=Temp;
lastTemp=Temp;
lowestTemp=100;
highestTemp=0;
sumTemp = 0;

//loop for different amount of simulations

for(int counter=1 ; counter<=simulation; counter++)
{

//starts the switch statement for each season
switch( season )
{
case (1): //first case
int Temperature = ts.getwintertemp();
//Generating the highest and the lowest temperature
if(Temp>highestTemp)
{
highestTemp=Temp;
}
if(Temp<lowestTemp)
{
lowestTemp=Temp;
}

sumTemp =sumTemp + Temp; //adds all the generated temps

break;

case (2)://second case
//Generating the highest and the lowest temperature
int Temperature2 = ts.getspringtemp();
if(Temp>highestTemp)
{
highestTemp=Temp;
}
if(Temp<lowestTemp)
{
lowestTemp=Temp;
}

sumTemp =sumTemp + Temp; //adds all the generated temps

break;
case (3)://third case
int Temperature3 = ts.getsummertemp();
if(Temp>highestTemp)
{
highestTemp=Temp;
}
if(Temp<lowestTemp)
{
lowestTemp=Temp;
}

sumTemp =sumTemp + Temp;//adds all the generated temps

break;
case (4)://forth case
int Temperature4 = ts.getfalltemp();
if(Temp>highestTemp)
{
highestTemp=Temp;
}
if(Temp<lowestTemp)
{
lowestTemp=Temp;
}

sumTemp =sumTemp + Temp;//adds all the generated temps

System.out.printf(“The Temperature is %d\n”, Temp);// prints the value
break;
}

//Generating the first and last temp
//Generating for the first season
if (season==1)
{
if(counter==1)
{
firstTemp=Temp ;
System.out.printf(“The first temp is %d\n”, firstTemp);
}

if(counter==simulation)
{
lastTemp=Temp;
System.out.printf(“The last temp is %d\n”, lastTemp);
}
}

//Generating the first and last temp for the second season
if (season==2)
{
if(counter==1)
{
firstTemp=Temp ;
System.out.printf(“The first temp is %d\n”, firstTemp);
}

if(counter==simulation)
{
lastTemp=Temp;
System.out.printf(“The last temp is %d\n”, lastTemp);
}
}

//Generating the first and last temp for third season
if (season==3)
{
if(counter==1)
{
firstTemp=Temp ;
System.out.printf(“The first temp is %d\n”, firstTemp);
}

if(counter==simulation)
{
lastTemp=Temp;
System.out.printf(“The last temp is %d\n”, lastTemp);
}
}
//Generating the first and last temp for the forth season
if (season==4)
{
if(counter==1)
{
firstTemp=Temp ;
System.out.printf(“The first temp is %d\n”, firstTemp);
}

if(counter==simulation)
{
lastTemp=Temp;
System.out.printf(“The last temp is %d\n”, lastTemp);
}
}

}

//for loop ends

//outputing the highest and the lowest
System.out.printf(“The Highest temp is %d\n”, highestTemp);
System.out.printf(“The Lowest temp is %d\n”, lowestTemp);

//Generating the sum and the average for each season

if (season==1)
{

averageTemp = sumTemp/simulation;

System.out.println(“1- Total sum of all temperatures generated: ” + sumTemp+”\n” +
“2- Average for the season: ” + averageTemp+”\n” );
}
if (season==2)
{
averageTemp = sumTemp/simulation;

System.out.println(“1- Total sum of all temperatures generated: ” + sumTemp+”\n” +
“2- Average for the season: ” + averageTemp+”\n” );
}

if (season==3)
{
averageTemp = sumTemp/simulation;

System.out.println(“1- Total sum of all temperatures generated: ” + sumTemp+”\n” +
“2- Average for the season: ” + averageTemp+”\n” );
}

if (season==4)
{
averageTemp = sumTemp/simulation;

System.out.println(“1- Total sum of all temperatures generated: ” + sumTemp+”\n” +
“2- Average for the season: ” + averageTemp+”\n” );
}

}

}

}catch(Exception e){
System.err.println(“Try again, you must press numbers from 1 to 5 for decision… simulation must be an interger.”);

}

}
}
}

Screenshots:

lab 2 simulation

 

Leave a Reply

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