Lab 2

Description

Lab 2 is to re-create Lab 1 into two classes. One of them will be the main app that takes input from the user, and it will call another class to a generate random temperature value. The main app should catch improper inputs such as letters or numbers that are not on the menu. Rest of the program should be the same as Lab 1 that simulate a series of random temperature. After each simulation, the program should list the following: First, Last , Lowest , Highest, The Total sum of all temperatures generated, and the Average for the season.

Codes

TemperatureSensorStats

package Lab2;
/**
 * Wai Daat Tsang
 *
 * Lab 2 is to modify Lab 1 which is to use a separate class for generating
 * the random temperature. The program should use the input from the
 * user to generate a proper temperature within the range of the season. By the 
 * end of each simulation, the program should display the Lowest, Highest, First,
 * Last, Sum, and Average generated temperature.
 *
 **/

import java.util.Scanner;

public class TemperatureSensorStats {
    public static void main(String[] args) {
        // declare new objects and variables
        TemperatureSensor newTemp = new TemperatureSensor();
        Scanner input = new Scanner(System.in);
        int simulation, temp, season;
        int low, high;
        float sum;

        do {
            // reset all variables for each simulation
            simulation = season = low = high = 0;
            sum = 0;

            // display a menu
            System.out.println("Menu: ");
            System.out.println("1) winter\n2) spring \n3) summer \n4) fall \n5) exit");
            System.out.print("Choose an action: ");
            do {
                try {
                    // get input from user for season
                    season = Integer.parseInt(input.nextLine());
                    // error check for season
                    // range should between 1-5
                    if (season > 5 || season <= 0) {
                        System.out.print("Error! Out of range! Please enter a number from the menu: ");
                    }
                } catch (NumberFormatException e) {
                    // caught none numeric input and display a error message
                    System.out.print("Invalid input. Only enter a numeric number from the menu: ");
                }

            } while (5 < season || 1 > season);

            // if input is exactly 5, the program ends here and will not execute any line from here
            if (season == 5) {
                System.out.println("BYE");
                return;
            }

            // ask user to enter amount of number to simulate the program
            System.out.print("How many times to simulate? ");
            do {
                // try to catch character/string input, the program only takes
                // integer for its input
                try {
                    simulation = Integer.parseInt(input.nextLine());
                    if(simulation<1)
                    {
                        System.out.print("Error! Enter a number greater than 0: ");
                    }
                } catch (NumberFormatException e) {
                    System.out.print("Invalid input. Please enter a number: ");
                }
            } while (simulation <= 0); // keep trying until the input is greater than 0

            // start simulation
            for (int i = 0; i < simulation; i++) {                 // The following if conditional statements are calling appropriate methods                 // from the TemperatureSensor to generate a random number and assign it to a variable.                 if (season == 1) // 1 is to get a generated temperature for Winter                 {                     temp = newTemp.getWinterTemp();                 } else if (season == 2) // 2 is to get a generated temperature for Spring                 {                     temp = newTemp.getSpringTemp();                 } else if (season == 3) // 3 is to get a generated temperature for Summer                 {                     temp = newTemp.getSummerTemp();                 } else // 4 is to get a generated temperature for Fall                 {                     temp = newTemp.getFallTemp();                 }                 // calculate the sum of all generated temperature                 sum += temp;                 // first simulation                 if (i == 0) {                     // print out the first temperature that the program generates                     System.out.println("First temperature generated: " + temp);                     // assign the first generated temperature to the lowest temperature variable                     // which will use for comparison later                     low = temp;                 } else if (i + 1 == simulation) // last simulation                 {                     // print out the message saying that the last temperature generated                     System.out.println("Last temperature generated: " + temp);                 }                 // comparing the current generated temperature to the highest value,                 // otherwise, assign it to the lowest temperature                 if (temp > high) {
                    high = temp;
                } else if (temp < low) {
                    low = temp;
                }

            }
            // display the Lowest, Highest, Sum, and Average generated temperature
            System.out.println("Lowest: " + low);
            System.out.println("Highest: " + high);
            System.out.println("Sum: " + sum);
            System.out.printf("Average: %.2f \n\n", sum / simulation);
        } while (season != 5);
    }
}

TemperatureSensor

package Lab2;
/**
 * Wai Daat Tsang
 *
 * This file contains four classes which will be used for 
 * generating a random temperature in that season.
 *
 **/

// include the built-in library for generating a random number

import java.util.Random;

public class TemperatureSensor {
    // create a new private Random object called "randomGenerator"
    private Random randomGenerator = new Random();

    /**
     * getWinterTemp() - to generate a random value between 20-40
     *
     * @return int
     */
    public int getWinterTemp() {
        return randomGenerator.nextInt(21) + 20;
    }

    /**
     * getSpringTemp() - to generate a random value between 40-70
     *
     * @return int
     */
    public int getSpringTemp() {
        return randomGenerator.nextInt(31) + 40;
    }

    /**
     * getSummerTemp() - to generate a random value between 70-90
     *
     * @return int
     */
    public int getSummerTemp() {
        return randomGenerator.nextInt(21) + 70;
    }

    /**
     * getFallTemp() - generate a random value between 40-60
     *
     * @return int
     */
    public int getFallTemp() {
        return randomGenerator.nextInt(21) + 40;
    }
}

Screenshots

Leave a Reply

Your email address will not be published.