LAB 1

Description: In this lab we create a temperature sensor simulator which will display a menu to the user asking them to choose what weather season simulation they want. Each season has a different value that we have to assign. Depending on what value the user inputs, there should be an output displaying the : First temperature generated, Last temperature generated, Lowest temperature generated, Highest temperature generated, Total sum of all temperatures generated, and Average for the season.

Source Code:
import java.util.Random;
import java.util.Scanner;

public class Simulation

{

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

{

Random ran = new Random();

int firstTemp;
int temp;
int lastTemp;
int lowestTemp;
int highestTemp;
int sumTemp = 0;
int averageTemp = 0;

firstTemp = ran.nextInt(end) + range;
sumTemp = firstTemp;
highestTemp = firstTemp;
lowestTemp = firstTemp;

for(int i=0; i highestTemp)
highestTemp = temp;

if(temp highestTemp)

highestTemp = lastTemp;

if(lastTemp < lowestTemp)

lowestTemp = lastTemp;
averageTemp = sumTemp/numofsim;

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

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("Enter 1 for Winter.\nEnter 2 for Sring.\nEnter 3 for Summer.\nEnter 4 for Fall.\nEnter 5 to Exit.");
choice = sc.nextInt();

while(choice != 5)

{

System.out.println("How many times would you like to simulate");

numberOfSimulation = sc.nextInt();

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);

}

System.out.println("Enter 1 for Winter.\nEnter 2 for Sring.\nEnter 3 for Summer.\nEnter 4 for Fall.\nEnter 5 to Exit.");

}

System.out.println("Thank you for using our simulation");

}

}

Leave a Reply

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