Lab 1

Lab Description:

In this lab we were asked to create a temperature sensing program that generates random temperatures for each season. The user is asked to enter the desired season and the number of simulations. The program then tracks the highest, lowest, first and last temperatures for each simulation and calculates the total and average temperatures for each session. After the simulations are done the program will output the results and repeat the main menu if the user decides to continue.

Code:

 

/* Justine Ginchereau
 */
import java.util.Random;
import java.util.Scanner;

public class lab1 {

	public static void main(String[] args) 
	{
		Scanner keyboard = new Scanner(System.in);
		int choice;
		do
		{
		System.out.println("Enter which choice to simulate:");
		System.out.println("(1)Winter (2)Spring (3)Summer (4)Fall (5)Exit");
		choice = keyboard.nextInt();

		//Variables
		int Random=9;
		int DefaultTemp = 0;

		int HighTemp=0;
		int LowTemp=0;	

		int FirstTemp=0;
		int LastTemp=0;		

		//Calculation variables
		double TotalTemp=0;
		double AvgTemp=0;

		if(choice==1)
			DefaultTemp=20;

		else if (choice==2)
			DefaultTemp=40;

		else if (choice==3)
			DefaultTemp=70;

		else if (choice==4)
			DefaultTemp=40;

		else
		{	
			System.out.println("End of program");
			System.exit(0);
		}
		System.out.println("Number of simulations:");
		int input = keyboard.nextInt();

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

		//Simulation
		for ( int i=0; i<input; i++)
 		{ 	
		int RandomTemp = RanTemp.nextInt(Random);
 		//Total Temperature 
			TotalTemp+=(RandomTemp+DefaultTemp); 			System.out.println("Simulated Temperature " +(i+1)+" is: "+(RandomTemp+DefaultTemp) + "°"); 			 			if (i==0) 			{ 				FirstTemp=RandomTemp+DefaultTemp; 				HighTemp=DefaultTemp+RandomTemp; 				LowTemp=DefaultTemp+RandomTemp; 				 			} 			if (i==(input-1)) 				LastTemp=RandomTemp+DefaultTemp; 			 			//High and low temperature 			if((RandomTemp+DefaultTemp)>HighTemp)
			HighTemp=DefaultTemp+RandomTemp;
			if((RandomTemp+DefaultTemp)<LowTemp)
				LowTemp=DefaultTemp+RandomTemp;
		}	

			//Average Temperature
			AvgTemp=TotalTemp/input;

			//Output
			System.out.println("Total Sum of Temperatures: " + TotalTemp + "°");
			System.out.println("Average Temperature: " + AvgTemp + "°");
			System.out.println("First Temperature: " + FirstTemp + "°");
			System.out.println("Last Temperature: " + LastTemp + "°");
			System.out.println("Highest Temperature: " + HighTemp + "°");
			System.out.println("Lowest Temperature: " + LowTemp + "°");
			System.out.println("Repeat Simulation? (1)Yes (5)No");
			choice = keyboard.nextInt();

		}while(choice!=5);

		keyboard.close();
	}

}

Screenshots:

 

Capture

 

 

Leave a Reply

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