Skip to content

Categories:

Lab 1

Lab Description:

This lab is a temperature sensor which randomly generates an 'n' amount of
numbers based on the users choice on how many generations. It's designed to get
an understanding of the java syntax and a big refreshment of programming. This 
program required a numbers of things. Firstly, the condition was that the user 
is able to pick what season the user wants. So if the user picks winter, then 
the only temperatures that appear are temperatures that exist in winter (20-40 degrees). 
For that, I had to create five if statements for winter, spring, summer, fall 
and exit. The other condition was that the user is able to exit the program once 
the user was done. To simulate the exit, I put most of the program inside a do-while 
loop which allowed the program to run at least once before it is stopped by the user. 
Once the user chose exit, the program will fail the do-while loop ending the program.

Further into the program, another condition was that not only the program
would generate random numbers, but also within range of the temperatures
needed for each season. In order to accomplish that, I used a library that allowed
to use a function called random. What this function does is it generates random
numbers between 0 to n amount. If I wanted to generate numbers from 0-20, I would
put generate.nextInt(21). In order to get a higher temperature range, I added the
generated number to 20 for example, which then gives me a range of 20-40. Finally,
within each if statement, there would be more if statements that determine a
couple of things. The first temperature, last temperature, average, sum, highest
and lowest temperature were needed. The final step was just to clean up the
program. I had to make sure that if the user was going to use the program again,
then the values for each temperature would reset, so that it does not mix up
with the new values.

Code:

package lab1;
import java.util.Scanner;
import java.util.Random;
public class Lab_1 {

	public static void main(String[] args) {
		int start = 1, iteration = 0, n = 1, temp, first = 0, last = 0, lowest = 999, highest = 0, sum = 0; //integer variables
		float avg = 0, x = 0; //float variables
		Random generate = new Random(); //function to generate random numbers between 0 and n
		Scanner choice = new Scanner(System.in); //scanner created for keyboard input
		System.out.println("Welcome to the temperature sensor program, please select a season or enter '5' to exit: "); //menu string
		do { //do-while loop, loop until variable start = 0
			System.out.println("(1) Winter");
			System.out.println("(2) Spring");
			System.out.println("(3) Summer");
			System.out.println("(4) Fall");
			System.out.println("(5) Exit");
			System.out.print("Choice: ");
			int season = choice.nextInt(); //enter season number
			if (season != 5){
				System.out.print("Please enter the number of generations for the season: ");
				iteration = choice.nextInt(); //enter number of generations/iterations
			}
			if (season == 1) { //if season = 1
				while (n <= iteration) { //loop from 1 to number of generations
					temp = 20 + generate.nextInt(21); //generate random number from 0-20 and add 20
					if (n == 1) { //on the first iteration
						first = temp; //first equals temp
					}
					if (temp < lowest) { //if temp is less than lowest (pass the first time since lowest equals 999) 						lowest = temp; //lowest equals temp 					} 					if (temp > highest) { //if temp is greater than highest
						highest = temp; //highest equals temp
					}
					last = temp; //last equals temp
					sum = sum + temp; //sum equals to old sum plus the new temp
					n++; //increment n by 1
				}
			}
			else if (season == 2) { //if season = 2
				while (n <= iteration) {
					temp = 40 + generate.nextInt(31); //generate random number from 0-30 and add 40
					last = temp;
					if (n == 1) {
						first = temp;
					}
					if (temp < lowest) { 						lowest = temp; 					} 					if (temp > highest) {
						highest = temp;
					}
					last = temp;
					sum = sum + temp;
					n++;
				}
			}
			else if (season == 3) { //if season = 3
				while (n <= iteration) {
					temp = 70 + generate.nextInt(21); //generate random number from 0-20 and add 70
					if (n == 1) {
						first = temp;
					}
					if (temp < lowest) { 						lowest = temp; 					} 					if (temp > highest) {
						highest = temp;
					}
					last = temp;
					sum = sum + temp;
					n++;
				}
			}
			else if (season == 4) { //if season = 4
				while (n <= iteration) {
					temp = 40 + generate.nextInt(21); //generate random number from 0-20 and add 40
					if (n == 1) {
						first = temp;
					}
					if (temp < lowest) { 						lowest = temp; 					} 					if (temp > highest) {
						highest = temp;
					}
					last = temp;
					sum = sum + temp;
					n++;
				}
			}
			else if (season == 5) { //if season = 5
				start = 0; //exit do-while loop, exit program
			}
			if (start == 1) { //if start = 1 assuming user did not exit
				x = sum; //change sum to float value
				avg = x/iteration; //x divide by iteration
				System.out.println("");
				System.out.println("First temperature generated: "+first); //print first temperature
				System.out.println("Last temperature generated: "+last); //print last temperature
				System.out.println("Lowest temperature generated: "+lowest); //print lowest temperature
				System.out.println("Highest temperature generated: "+highest); //print highest temperature
				System.out.println("Sum of all temperatures: "+sum); //print sum of temperatures
				System.out.println("Average of all temperatures: "+avg); //print average of all temperatures
				System.out.println("");
				n = 0; //reset n
				sum = 0; //reset sum
				highest = 0; //reset highest
				lowest = 999; //reset lowest
			}
		} while (start == 1); //do-while condition
	}
}

Screenshots:


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.