Lab 2

Lab Description:
Lab 2 was basically the same as Lab 1 but, I had to separate it into classes. I created a TemperatureSensor class and TemperatureSensorStats class which runs the menu that asks for which season the user would like to select by using (1)Winter, (2)Summer, (3)Fall, (4)Spring, and (0) for exit. When you choose the season the TemperatureSensorStat will use if and else statement to go to the TemperatureClass which initiates the methods that i created which are getWinterTemp and so on. I did that by creating a method in the TemperatureStat and named it getWinterTemp. i than put the code from the previous lab for the Winter, and than I created an object in the main class which is the TemperatureSensor class which i named mytemp. I than used the name of the object plus call the method from that class by “mytemp.getWinterTemp”. i also than used the try/catch statement to try a code and if an error occurs than it will catch and display an error message.

Code:

package lab2;

import java.util.*;

public class TemperatureSensorStats {

	public static void main(String[] args) { // the main application

		try {// using try/catch with the code inside the try curly brackets
			int season;
			Scanner reader = new Scanner(System.in);
			do {

				System.out.println("Select Season");
				System.out.println("(1) Winter (2) Summer (3) Fall (4) Spring (0) Exit");
				season = reader.nextInt();

				TemperatureSensor mytemp = new TemperatureSensor();// creating
				// an object

				if (season == 1) {// basic if/else statement
					mytemp.getWinterTemp();
				} else if (season == 2) {
					mytemp.getSummerTemp();
				} else if (season == 3) {
					mytemp.getFallTemp();
				} else if (season == 4) {
					mytemp.getSpringTemp();
				} else if (season > 4) {
					System.out.println("Error Message");
				}

			} while (season != 0);

		}

		catch (Exception e) {// if an error occurs than it will catch and
			// display the error message

			System.out.println("Error Message");

		}

	}
}

package lab2;

import java.util.Random;
import java.util.Scanner;

public class TemperatureSensor {
	int simu = 0;
	int sum = 0;
	int average = 0;
	Scanner reader = new Scanner(System.in);

	public void getWinterTemp() {
		try {
			System.out.println("How many simulation do you want?");
			simu = reader.nextInt();
			Random random = new Random();
			int myarray[] = new int[simu];
			// random generator
			for (int i = 0; i < simu; i++) {
				myarray[i] = random.nextInt(21) + 20;
				// System.out.println(" temperature generated" + myarray[i]);

			}
			System.out.println("First temperature generated" + myarray[0]);
			// finds the mean
			for (int i = 0; i < simu; i++) {
				sum += myarray[i];
			}
			average = sum / simu;
			// finds the highest temperature
			for (int i = 0; i  myarray[0]) {
					myarray[0] = myarray[i];
				}
			}
			// find the lowest temperature

			System.out
			.println("Last temperature generated" + myarray[simu - 1]);
			System.out.println("Total sum of all temperatures generated" + sum);
			System.out.println("Average for the season" + average);
			System.out.println("Highest temperature generated" + myarray[0]);
			myarray[0] = myarray[0];
			for (int i = 0; i < simu; i++) {
				if (myarray[i] < myarray[0]) {
					myarray[0] = myarray[i];
				}
			}
			System.out.println("Lowest temperature generated" + myarray[0]);
		} catch (Exception e) {
			System.out.println("Error Message");
		}
	}

	public void getSummerTemp() {
		try {
			System.out.println("How many simulation do you want?");
			simu = reader.nextInt();
			Random random = new Random();
			int myarray[] = new int[simu];
			// random generator
			for (int i = 0; i < simu; i++) {
				myarray[i] = random.nextInt(21) + 70;
				// System.out.println(" temperature generated"+ myarray[i]);
			}
			System.out.println("First temperature generated" + myarray[0]);
			// finds the mean
			for (int i = 0; i < simu; i++) {
				sum += myarray[i];
			}
			average = sum / simu;
			// finds the highest temperature
			for (int i = 0; i  myarray[0]) {
					myarray[0] = myarray[i];
				}
			}
			// find the lowest temperature

			System.out
			.println("Last temperature generated" + myarray[simu - 1]);
			System.out.println("Total sum of all temperatures generated" + sum);
			System.out.println("Average for the season" + average);
			System.out.println("Highest temperature generated" + myarray[0]);
			myarray[0] = myarray[0];
			for (int i = 0; i < simu; i++) {
				if (myarray[i] < myarray[0]) {
					myarray[0] = myarray[i];
				}
			}
			System.out.println("Lowest temperature generated" + myarray[0]);
		} catch (Exception e) {
			System.out.println("Error Message");
		}

	}

	public void getFallTemp() {
		try {
			System.out.println("How many simulation do you want?");
			simu = reader.nextInt();
			Random random = new Random();
			int myarray[] = new int[simu];
			// random generator
			for (int i = 0; i < simu; i++) {
				myarray[i] = random.nextInt(21) + 40;
				// System.out.println(" temperature generated"+ myarray[i]);
			}
			System.out.println("First temperature generated" + myarray[0]);
			// finds the mean
			for (int i = 0; i < simu; i++) {
				sum += myarray[i];
			}
			average = sum / simu;
			// finds the highest temperature
			for (int i = 0; i  myarray[0]) {
					myarray[0] = myarray[i];
				}
			}
			// find the lowest temperature

			System.out
			.println("Last temperature generated" + myarray[simu - 1]);
			System.out.println("Total sum of all temperatures generated" + sum);
			System.out.println("Average for the season" + average);
			System.out.println("Highest temperature generated" + myarray[0]);
			myarray[0] = myarray[0];
			for (int i = 0; i < simu; i++) {
				if (myarray[i] < myarray[0]) {
					myarray[0] = myarray[i];
				}
			}
			System.out.println("Lowest temperature generated" + myarray[0]);
		} catch (Exception e) {
			System.out.println("Error Message");
		}

	}

	public void getSpringTemp() {
		try {
			System.out.println("How many simulation do you want?");
			simu = reader.nextInt();
			Random random = new Random();
			int myarray[] = new int[simu];
			// random generator
			for (int i = 0; i < simu; i++) {
				myarray[i] = random.nextInt(31) + 40;
				// System.out.println(" temperature generated"+ myarray[i]);
			}
			System.out.println("First temperature generated" + myarray[0]);
			// finds the mean
			for (int i = 0; i < simu; i++) {
				sum += myarray[i];
			}
			average = sum / simu;
			// finds the highest temperature
			for (int i = 0; i  myarray[0]) {
					myarray[0] = myarray[i];
				}
			}
			// find the lowest temperature

			System.out
			.println("Last temperature generated" + myarray[simu - 1]);
			System.out.println("Total sum of all temperatures generated" + sum);
			System.out.println("Average for the season" + average);
			System.out.println("Highest temperature generated" + myarray[0]);
			myarray[0] = myarray[0];
			for (int i = 0; i < simu; i++) {
				if (myarray[i] < myarray[0]) {
					myarray[0] = myarray[i];
				}
			}
			System.out.println("Lowest temperature generated" + myarray[0]);
		} catch (Exception e) {
			System.out.println("Error Message");
		}
	}
}



Screenshots:
lab1image

error

Leave a Reply