Lab 1

Lab 1: Temperature Sensor Simulator

Description:

In this first lab, we were made to use a lot of the different programming concepts we learned and create a program. This program’s job is to simulate the temperatures of all four seasons. Not only that, we have to create a menu for the user to choose the season they wanted to simulate, as well as how many simulations of that season’s temperatures they wanted to see. Afterwards, the user would be taken back to the main menu, where they could choose either another season or to exit from the program.

There were many different concepts used, such as do-while gate, which allows us to do various functions under a condition. I also used a for loop, to set up the algebraic portion of the numbers we were supposed to produce, like average, max, min, first number and last number. The function that helped me the most would be importing the Random utility, because it allowed me to set up a variable that would generate Random numbers, and everything else just went together. I go into a bit more detail in my code.

Code:

package simulation;

//import random utility for program (to create random numbers for each season).
import java.util.Random;
import java.util.Scanner;

public class Simulation {

//no return values; values are to be simulated and displayed in the end.

public static void simulate(int numofsim,int range, int end){

//declare variable for random numbers to be generated. 
//As well as variables for the values we are supposed to find.
	Random ran = new Random();	
	int firstTemp;
	int temp;
	int lastTemp;
	int lowestTemp;
	int highestTemp;
	int sumTemp = 0;
	int averageTemp = 0;

//setting up algorithms for the different values, such as max value, min value, average, etc.
//"firstTemp" is the temperature(s) that will be generated from the random number generator.

	firstTemp = ran.nextInt(end) + range;
	sumTemp = firstTemp;
	highestTemp = firstTemp;
	lowestTemp = firstTemp;
//for loop, used to create conditions for the program, based on the 
//the range of temperatures that are generated.

for(int i=0; i<numofsim-2;i++){ 	temp = ran.nextInt(end)+range; 	if(temp > highestTemp)
		highestTemp = temp;
	if(temp < lowestTemp) 		lowestTemp = temp; 		sumTemp += temp; 	} 	lastTemp = ran.nextInt(end) + range; 	sumTemp+= lastTemp; 	if(lastTemp > highestTemp)
		highestTemp = lastTemp;
	if(lastTemp < lowestTemp)
		lowestTemp = lastTemp;
		averageTemp = sumTemp/numofsim;

	System.out.println("1- First temperature: " +firstTemp +"\n" +
	"2- Last temperature: " +lastTemp +"\n" +
	"3- Lowest temperature: " +lowestTemp +"\n" +
	"4- Highest temperature: " +highestTemp +"\n" +
	"5- Total sum: " + sumTemp+"\n" +
	"6- Average: " + averageTemp+"\n" );
	}

//setting up the menu for the user to interact with upon execution of program
public static void main (String [] args){
	Scanner sc = new Scanner(System.in);
	int choice=0;
	int numberOfSimulation;

	System.out.println("What season would you like to simulate?");
	System.out.println("1. Winter \n 2. Spring \n 3. Summer \n 4. Fall \n 5. EXIT");
	choice = sc.nextInt();

//while loop is used so that user can access menu over and over again,
//as long as they do not choose to exit the program.
while(choice != 5){
	System.out.println("Input number of simulations: ");
	numberOfSimulation = sc.nextInt();

//conditions of the user's choice, which result in the simulation of temperatures
//within certain ranges that have been previously set.

//for each simulation, we start at the lowest number, and the next number 
//determines how many numbers come between the lowest and highest number in the range.
if (choice == 1){
	simulate(numberOfSimulation,20,20);
	}
else if(choice == 2){
	simulate(numberOfSimulation,40,30);
	}
else if(choice == 3){
	simulate(numberOfSimulation,70,20);
	}
else if(choice == 4){
	simulate(numberOfSimulation,40,20);
	}

//After the user gets their result, they are prompted back to the menu so that
//they can simulate temperatures over and over again, unless they exit.
	System.out.println("Enter 1 for Winter.\nEnter 2 for Spring.\nEnter 3 for Summer.\nEnter 4 for Fall.\nEnter 5 to Exit.");
		choice = sc.nextInt();
	}
//This message is displayed when user decides to exit the program.	
	System.out.println("End.");

	}
}

Screenshot:

CET3640 LAB1

Leave a Reply

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