Lab 1

Description

Lab 1 is to create a java program that simulates a temperature sensor. It allows user to pick a season and an exit trigger, then the program will generate numbers of random temperatures as many as user wants. Temperature will be generated to the proper degree within the season, such for winter a number between 20-40, for spring between 40-70, for summer between 70-90, and for fall between 40-60. After each simulation, the program will display the following items.

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

Codes

/**
 *	Name: Wai Daat Tsang
 *	CET3640 - Lab 1
 *	Description: create a temperature sensor simulator for four seasons; the program should display the first temperature generated, last temperature generated, lowest temperature generated, highest temperature generated, total sum of all temperature generated, and average for all season
 **/
package hw1;

/**
 * import built-in libraries;
 *	1) Scanner - get keyboard input
 *	2) Random - generate random number
 **/
import java.util.Scanner;
import java.util.Random;


public class Temperature {
	public static void main( String[] args)
	{
		// new Scanner variable
		Scanner input = new Scanner(System.in);
		// declare variables
		int run=0, season, randomTemp, low, high;
		float sum;

		do{
			// reset all variables for each run & initial the random seed
			Random randomGenerator = new Random();
			randomTemp=0;
			low = 0;
			high = 0;
			sum = 0;

			// display the main menu
			System.out.println("Menu:");
			System.out.println("1) winter\n2) spring \n3) summer \n4) fall \n5) exit");

			// check input, valid input are between 1-5.
			do{
				// get input from user
				System.out.print("Choose an item: ");
				season = input.nextInt();
				if(season>5)
				{
					System.out.print("Invalid input. Choose an item: ");
				}
			}while(season!=5 && season>5);

			// if input = 5, it's the end trigger
			if(season==5)
			{
				System.out.print("Bye");
				break;
			}

			// ask user for number to generate
			System.out.print("How many simluations to generate? ");
			run = input.nextInt();

			// generate simulations as many as user defines in variable: run
			for(int i = 0; i high )
				{
					high = randomTemp;
				}
				else if( randomTemp < low ){
					low = randomTemp;
				}

			}
			// display Lowest, Highest, Sum, and Average temperature
			System.out.println("Lowest: "+ low);
			System.out.println("Highest: "+ high);
			System.out.println("Sum: "+ sum);
			System.out.println("Average: "+ sum/run + "\n");
		}while(season!=5);
	}
}

Screenshots

Screenshot Part 1.

Screenshot Part 1.

Screenshot Part 2.

Screenshot Part 2.

Leave a Reply

Your email address will not be published.