Lab 2

Lab 2: Temperature Sensor Simulator (w/ Classes & Methods)

Description:

In this first lab, we were made to use a lot of the different programming concepts we learned and create a program. But the second lab was to make the lab a bit easier to read. We were to use more than one class, and various methods, instead of using the main method. This program’s job is still to simulate the temperatures of all four seasons. Each method I wrote out was for each season, to separate the data for each. We keep the menu for the user to choose the season they wanted to simulate, as well as how many simulations of that season’s temperatures they wanted to see, and the option for the user to be taken back to the main menu, where they could choose either another season or to exit from the program.

Code: I used a lot of my previous code to create this lab.

TemperatureSensor class.

package simulation;
import java.util.Random;

public class TemperatureSensor {
static Random ran = new Random();

public static int RandomNumber(int max) {
return ran.nextInt(max);
}

public static int getWinterTemp() {
return ran.nextInt(40 - 20) + 20;
}

public static int getSpringTemp() {
return ran.nextInt(70 - 40) + 40;
}

public static int getSummerTemp() {
return ran.nextInt(90 - 70) + 70;
}

public static int getFallTemp() {
return ran.nextInt(60 - 40) + 40;
}
}

TemperatureSensorStats class

package simulation;

import java.util.Scanner;

public class TemperatureSensorStats {
	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int choice = 0;
		int numberOfSimulation;

		int firstTemp;
		int temp;
		int lastTemp;
		int lowestTemp;
		int highestTemp;
		int sumTemp = 0;
		int averageTemp = 0;
		int max;
		int range = 0;
		int numofsim = 0;
		System.out.println("What season would you like to simulate?");
		System.out.println("1. Winter \n 2. Spring \n 3. Summer \n 4. Fall \n 5. EXIT");
		choice = sc.nextInt();
		numberOfSimulation = sc.nextInt();

		while (choice != 5){
		System.out.println("Input number of simulations: ");

		}
		if (choice == 1) {
			for (int i = 0; i > numberOfSimulation; i++) {
				temp = TemperatureSensor.getWinterTemp();
			}
		} else if (choice == 2) {
			for (int i = 0; i > numberOfSimulation; i++) {
				temp = TemperatureSensor.getSpringTemp();
			}
		} else if (choice == 3) {
			for (int i = 0; i > numberOfSimulation; i++) {
				temp = TemperatureSensor.getSummerTemp();
			}
		} else if (choice == 4) {
			for (int i = 0; i > numberOfSimulation; i++) {
				temp = TemperatureSensor.getFallTemp();
			}
		}

		System.out
				.println("Enter 1 for Winter.\nEnter 2 for Spring.\nEnter 3 for Summer.\nEnter 4 for Fall.\nEnter 5 to Exit.");
		choice = sc.nextInt();

		// This message is displayed when user decides to exit the program.
		while (choice == 5){
		System.out.println("End.");

		firstTemp = TemperatureSensor.ran.nextInt() + range;
		sumTemp = firstTemp;
		highestTemp = firstTemp;
		lowestTemp = firstTemp;

		for (int i = 0; i < numofsim; i++) { 			temp = TemperatureSensor.ran.nextInt(); 			if (temp > highestTemp)
				highestTemp = temp;
			if (temp < lowestTemp) 				lowestTemp = temp; 			sumTemp += temp; 		} 		lastTemp = TemperatureSensor.ran.nextInt() + range; 		sumTemp += lastTemp; 		if (lastTemp > highestTemp)
			highestTemp = lastTemp;
		if (lastTemp < lowestTemp)
			lowestTemp = lastTemp;
		averageTemp = sumTemp / numofsim;
	}
		}

	}

 

Leave a Reply

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