Lab # 1

Lab Description:

In this lab the requirement is to create a temperature sensor simulator. This means a program that relies upon the random generator function of the math library built into java to create a bunch of different values within specific ranges and then print out statistical data based upon those values. The initial instructions require the program to display a menu that will ask the user what season to simulate “(1) winter (2) spring (3) summer (4) fall or (5) to exit.” Once the user selects the season the program should ask the user how many “simulations” or random values to generate. The ranges for the seasons selected are as follows ; for winter a number between 20-40, for spring between 40-70, for summer between 70-90, and for fall between 40-60. The statistical data to print out is;

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

 

Code:

Yevgeniy Babkin CET - 3640

//first the random generator utility needs to be imported
import java.util.Random;
// secondly we need to import the Scanner in order to get input from user
import java.util.Scanner;
// Then we start the application by creating a class for our method
public class Temperature_Simulator

{
    //this refers to the method or (function) "simulate" which will set the Seasonal range and
    //generate our random values according to users input
    public static void simulate(int numofsim,int range, int end)

    {
        //"ran" will be used to generate random values
        Random ran = new Random();

        // here we create and initialize the variables we need

        int firstTemp;

        int temp;

        int lastTemp;

        int lowestTemp;

        int highestTemp;

        int sumTemp = 0;

        int averageTemp = 0;

        // generating random values for (first temp) according to user selected choice

        firstTemp = ran.nextInt(end) + range;

        //here we set our reference as the first temp generated
        sumTemp = firstTemp;

        highestTemp = firstTemp;

        lowestTemp = firstTemp;

        // for loop to generate the user requested amount of values

        for(int i=0; i<numofsim-2;i++)

        {
            // we use a method to generate random value "[name].nextInt(int n);"

            temp = ran.nextInt(end)+range;

            if(temp > highestTemp)

                highestTemp = temp;

            if(temp < lowestTemp)

                lowestTemp = temp;

            sumTemp += temp;

        }

        lastTemp = ran.nextInt(end) + range;

        sumTemp += lastTemp;

        if(lastTemp > highestTemp)

            highestTemp = lastTemp;

        if(lastTemp < lowestTemp)

            lowestTemp = lastTemp;

        averageTemp = sumTemp/numofsim;

        //Print out all the required variables

        System.out.println("1 - First temperature generated: " +firstTemp +"\n" +

"2- Last temperature generated: " +lastTemp +"\n" +

"3- Lowest temperature generated: " +lowestTemp +"\n" +

"4- Highest temperature generated: " +highestTemp +"\n" +

"5- Total sum of all temperatures generated: " + sumTemp+"\n" +

"6- Average for the season: " + averageTemp+"\n" );

    }
// program 
    public static void main (String [] args)

    {

        Scanner scan = new Scanner(System.in);
// declare and initialize the couple of variables that will be used in this scope
        int choice=0;

        int NumberOfSimulations;

        System.out.println("What season would you like to simulate");

        System.out.println("Enter 1 for Winter.\nEnter 2 for Sring.\nEnter 3 for Summer.\nEnter 4 for Fall.\nEnter 5 to Exit.");

        choice = scan.nextInt();
// use a while loop
        while(choice != 5)

        {

            System.out.println("How many times would you like to simulate");

            NumberOfSimulations = scan.nextInt();

            if (choice == 1)

            {
                //call simulate function with the specific range from 20-40 and 
                simulate(NumberOfSimulations,20,20);

            }

            else if(choice == 2)

            {

                simulate(NumberOfSimulations,40,30);

            }

            else if(choice == 3)

            {

                simulate(NumberOfSimulations,70,20);

            }

            else if(choice == 4)

            {

                simulate(NumberOfSimulations,40,20);

            }

            System.out.println("Enter 1 for Winter.\nEnter 2 for Sring.\nEnter 3 for Summer.\nEnter 4 for Fall.\nEnter 5 to Exit.");

            choice = scan.nextInt();

        }

        System.out.println("Thanks for using this Temperature Simulator\n\n" +"\n"+ "goodbye");

    }

}

Screenshot:

Lab 2 cet3640

Leave a Reply

Your email address will not be published. Required fields are marked *