Lab 1
Temperature Sensor Simulator
Description
This lab is first lab for the Spring 2014 CET 3640 Class. For this lab I was asked to create a program that generates random numbers which represent the temperature of a selected season. the Ranges of the seasons and selections are as follows; [1] Winter 20 - 40 [2] Spring 40 - 70 [3] Summer 70- 90 [4] Fall 40 - 60 The program needs to display 1. the first temperature 2. the last temperature 3. Lowest tempertue 4. highest temperature 5. total sum of all temperature 6. Average for the season this program must be done with the JAVA programming language.
CODE:
import java.util.Scanner; public class Temperature { public static void main(String[] args){ int season; //For the Selected Menu Item double random=0, first=0, greatest = 0, lowest=0,last=0,sum=0, min=0, max=0; //all local variables Scanner input = new Scanner(System.in); //Use to receive commands from the keyboard int resp; double[] values= new double[100]; //Array to store all 100 values do{ //displays the menu then loops it back as long as 5 is not input sum = 0;//resets the sum back to zero System.out.println("Temperature Sensor Simulator"); System.out.println("Which Season would you like to simulate"); System.out.println("\t[1] Winter\n\t[2] Spring \n\t[3] Summer \n\t[4] Fall \n\t[5] Exit"); season = input.nextInt(); if((season0)){ switch(season){ //this is sets the boundaries case 1: //for winter System.out.println("You have selected: Winter"); min = 20; max = 40; break; case 2: //for Spring System.out.println("You have selected: Spring"); min = 40; max = 70; break; case 3: //for summer System.out.println("You have selected: Summer"); min = 70; max = 90; break; case 4: //for Fall System.out.println("You have selected: Fall"); min = 40; max = 60; break; } for(int i=0; i<100; i++){ //loops the program 100 times getting random numbers in the range of the season random = rand(min, max); //gets random values by calling the rand() funtion values[i]=random; if(i==0){ first= greatest = lowest =random; } if(i==99){ last=random; } if(randomgreatest){ greatest = random; } sum += random; } System.out.printf("\tFirst Value= %5.2f\n\tLast Value= %5.2f\n",first,last); System.out.printf("\tHighest Value= %5.2f\n\tLowest= %5.2f\n",greatest,lowest); System.out.printf("\tSum Value= %f\n\tAverage= %5.2f \n",sum,(sum/100)); System.out.println("Would you like a display of all 100 the values? [1]Yes [2]no"); resp = input.nextInt(); if(resp==1){ for(int i=0; i<25; i++){ System.out.printf("%d. %5.2f\t%d. %5.2f\t%d. %5.2f\t%d. %5.2f \n",(i+1),values[i],(i+26),values[i+25],(i+51),values[i+50],(i+76),values[i+75]); } } }}while(season != 5); System.out.println("Now terminating...."); System.out.println("GoodBye!"); } static double rand(double low, double high){ //takes in the range of the season spits out random value withing the limits inclusive double val; double max; max = high+1; val = max*Math.random(); while((val=high)){ val = high*Math.random(); } return val; } }
OpenLap Often Crops the codes and deletes special characters In case this happens the full code can be downloaded by clicking this link
Code: Click Here
ScreenShot
Leave a Reply