Skip to content

Categories:

Lab 2

Lab Description:

This lab utilizes the same concept as the previous lab (Lab 1), with the
addition of using another class to generate the random numbers. The reason for
using this is the same reason one might use functions in C++ for example. In a
lot of jobs that require programming, people have program some functionality.
Since everyone has different conventions when it comes to programming, creating
classes makes it easier when combining the program together later on. The way
this is done is by creating a new class apart from the main class, and designing
the class anyway you needed it to be, and returning that value to the main program.

The try and catch statement was also used to prevent the user from inputting
something invalid where the input requires a number rather than a letter. By using
the try and catch statement, the program will continue working the program as
long as you instruct it to. Generally if the statement was not used, the program
would give an error and stop the program completely. The only issue I had while
working on this lab was the try and catch statement, it kept looping infinitely
until I cleared the input which took a while to understand the concept.

Code:
Main Class:

package lab2;
import java.util.Scanner;
import java.util.InputMismatchException;
public class TemperatureSensorStats {

	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
		int season = 0; //initialize input 'season'
		float avg = 0, x = 0; //float variables
		Scanner choice = new Scanner(System.in); //scanner created for keyboard input
		TemperatureSensor seasonObject = new TemperatureSensor(); //object for season
		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: ");
			try{ //try statement
				season = choice.nextInt(); //enter season number
				while (season > 5 || season < 1) //while season is not any of the available choices
				{
					System.out.println("Invalid choice! Please re-enter a valid choice!");
					System.out.print("Choice: ");
					season = choice.nextInt(); //re-enter value
				}
				if (season != 5){ //if displayed seasons are chosen
					System.out.print("Please enter the number of generations for the season: ");
					iteration = choice.nextInt(); //enter number of generations/iterations
				}
			} catch (InputMismatchException e){ //catch invalid input
				System.out.println("This input is invalid!\n");
				choice.nextLine(); //clear input value
				continue; //re-enter program
			} 
			if (season == 1) { //if season = 1
				while (n <= iteration) { //loop from 1 to number of generations
					temp = seasonObject.getWinterTemp(n); //call method getWinterTemp with argument n
					if (n == 1) { //on the first iteration
						first = temp; //first 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 = seasonObject.getSpringTemp(n); //call method getSpringTemp with argument n
					last = temp;
					if (n == 1) {
						first = temp;
					}
					if (temp  highest) {
						highest = temp;
					}
					last = temp;
					sum = sum + temp;
					n++;
				}
			}
			else if (season == 3) { //if season = 3
				while (n <= iteration) {
					temp = seasonObject.getSummerTemp(n); //call method getSummerTemp with argument n
					if (n == 1) {
						first = temp;
					}
					if (temp  highest) {
						highest = temp;
					}
					last = temp;
					sum = sum + temp;
					n++;
				}
			}
			else if (season == 4) { //if season = 4
				while (n <= iteration) {
					temp = seasonObject.getFallTemp(n); //call method getFallTemp with argument n
					if (n == 1) {
						first = 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
	}
}
Temperature Sensor Class:
package lab2;
import java.util.Random;
public class TemperatureSensor {
	private int temp; //private variable for class
	Random generate = new Random(); //function to generate random numbers between 0 and n
	public int getWinterTemp(int n) {
		temp = 20 + generate.nextInt(21); //generate random number from 0-20 and add 20
		return temp; //return temp
	}
	public int getSpringTemp(int n) {
		temp = 40 + generate.nextInt(31); //generate random number from 0-30 and add 40
		return temp;
	}
	public int getSummerTemp(int n) {
		temp = 70 + generate.nextInt(21); //generate random number from 0-20 and add 70
		return temp;
	}
	public int getFallTemp(int n) {
		temp = 40 + generate.nextInt(21); //generate random number from 0-20 and add 40
		return temp;
	}
}

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.