Lab 1

Introduction:

The objective of this lab is to create a temperature sensor simulator. First the program must ask the  user to identify what season to simulate . 1 for winter. 2 for spring. 3 for summer, and 4 for fall or 5 to exit. After the season is selected, the program will ask the user how many time he want to simulate. 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 display this 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

 

CODE:

// Lab 1
//A simple Program for randomizing temperature
import java.util.Scanner; //for use of the scanner command
import java.util.Random;  //for use of the random command
public class Temperature_Sensor {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner input = new Scanner( System.in ); //Allows the USER to be able to read the user's input
		Random generator = new Random();//allow the program to generate random numbers
		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.");//display the message
		season = input.nextInt(); //reads the input from the user
		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=90;
		int highestTemp=20;
		int sumTemp = 0; //variable for the sum of the first season
		int sumTemp2 = 0;//variable for the sum of the second season
		int sumTemp3 = 0;//variable for the sum of the third season
		int sumTemp4 = 0;//variable for the sum of the forth season
		int averageTemp = 0;//variable for the average of the season
		int Temp;//variable for the first temperature
		int Temp2;
		int Temp3;
		int Temp4;

		//loop for different amount of simulations

		for(int counter=1 ; counter<=simulation; counter++) 
		{
			 Temp=generator.nextInt(20) + 20;// generate a value from 20 to 40
			 Temp2=generator.nextInt(30) + 40;// generate a value from 40 to 60
			 Temp3=generator.nextInt(20) + 70;// generate a value from 70 to 90
			 Temp4=generator.nextInt(20) + 40;// generate a value from 40 to 60

			//starts the switch statement for each season
	    switch( season ) 
		{
	    	case (1): //first case

	    		//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

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

	    		break;
	    	case (2)://second case
	    		//Generating the highest and the lowest temperature
	    		if(Temp2>highestTemp)
	    		 {
	    			 highestTemp=Temp2;
	    		 }
	    		 if(Temp2<lowestTemp)
	    		 {
	    			 lowestTemp=Temp2;
	    		 }

	    		sumTemp2 =sumTemp2 + Temp2; //adds all the generated temps

	    	System.out.printf("The Temperature is %d\n", Temp2);// prints the value
	    		break;
	    	case (3)://third case

	    		if(Temp3>highestTemp)
	    		 {
	    			 highestTemp=Temp3;
	    		 }
	    		 if(Temp3<lowestTemp)
	    		 {
	    			 lowestTemp=Temp3;
	    		 }

	    		sumTemp3 =sumTemp3 + Temp3;//adds all the generated temps

	    	System.out.printf("The Temperature is %d\n", Temp3);// prints the value
	    		break;
	    	case (4)://forth case

	    		if(Temp4>highestTemp)
	    		 {
	    			 highestTemp=Temp4;
	    		 }
	    		 if(Temp4<lowestTemp)
	    		 {
	    			 lowestTemp=Temp4;
	    		 }

	    		sumTemp4 =sumTemp4 + Temp4;//adds all the generated temps

	        System.out.printf("The Temperature is %d\n", Temp4);// 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=Temp2 ;
	    	 System.out.printf("The first temp is %d\n", firstTemp);
	     }

	    if(counter==simulation)
	     {
	    	 lastTemp=Temp2;
	    	 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=Temp3 ;
	    	 System.out.printf("The first temp is %d\n", firstTemp);
	     }

	    if(counter==simulation)
	     {
	    	 lastTemp=Temp3;
	    	 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=Temp4 ;
	    	 System.out.printf("The first temp is %d\n", firstTemp);
	     }

	    if(counter==simulation)
	     {
	    	 lastTemp=Temp4;
	    	 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 = sumTemp2/simulation;

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

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

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

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

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

		}
		}

		}

}

Sample Outputs:

 

Untitled

 

 

Leave a Reply

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