Description: This lab is about re-creating lab #1 and separating it into two classes, TemperatureSensor and TemperatureSensorStats. The first class should implement four methods, getWinterTemp, getSpringTemp, getSummerTemp, getFallTemp, and each method of them should return a number between two values. Method getWinterTemp should return a number between 20-40, getSpringTemp between 40-70, getSummerTemp between 70-90, and getFallTemp between 40-60. The TemperatureSensorStats class should implement the application, in which the application should display a menu that has 5 options, (1) winter (2) spring (3) summer (4) fall or (5) to exit, to ask the user which season to simulate. and when the user choose an option, it then asks how many simulations to generate. The corresponding method from the temperature class should be called by each simulation. Also the program should outputs 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. The user should input any number between 1 and 5, and any other option should be invalid.
Code: The first class:
import java.util.Random; public class TemperatureSensor { public static void getWintertemp() { Random wi = new Random(); int LowWi = 20; int HighWi = 40; int Wi = wi.nextInt(HighWi - LowWi) + LowWi; System.out.println("Winter temp is: " + Wi); } public static void getSpringtemp() { Random sp = new Random(); int LowSp = 40; int HighSp = 70; int Sp = sp.nextInt(HighSp - LowSp) + LowSp; System.out.println("Spring temp is: " + Sp); } public static void getSummertemp() { Random s = new Random(); int LowS = 70; int HighS = 90; int S = s.nextInt(HighS - LowS) + LowS; System.out.println("Summer temp is: " + S); } public static void getFalltemp() { Random f = new Random(); int LowF = 40; int HighF = 60; int F = f.nextInt(HighF - LowF) + LowF; System.out.println("Fall temp is: " + F); } }
The Second class:
import java.util.Random; import java.util.Scanner; public class TemperatureSensorStats extends TemperatureSensor { public static void main(String args[]) { @SuppressWarnings("resource") Scanner Seasoninput = new Scanner(System.in); Random s = new Random(); Random f = new Random(); Random wi = new Random(); Random sp = new Random(); // Variables used for seasons and user input int SeasonChoice; int Simulations; int LowS = 70; int HighS = 90; int LowF = 40; int HighF = 60; int LowWi = 20; int HighWi = 40; int LowSp = 40; int HighSp = 70; int highStemp = Math.max(LowS, HighS); int lowStemp = Math.min(HighS, LowS); int sumtemp = 0; int avgtemp; // Algorithm used to calculate seasons int S = s.nextInt(HighS - LowS) + LowS; int F = f.nextInt(HighF - LowF) + LowF; int Wi = wi.nextInt(HighWi - LowWi) + LowWi; int Sp = sp.nextInt(HighSp - LowSp) + LowSp; do { highStemp = 0; sumtemp = 0; avgtemp = 0; F = 0; S = 0; Sp = 0; Wi = 0; System.out .println("Enter (1)summer (2)fall (3)winter (4)spring (5)exit"); SeasonChoice = Seasoninput.nextInt(); System.out.println("Enter # sim"); Simulations = Seasoninput.nextInt(); // Code to generate seasons for (int i = 0; i highStemp) { highStemp = S; } if (S highStemp) { highStemp = F; } if (F highStemp) { highStemp = Wi; if (Wi highStemp) { highStemp = Sp; } if (Sp < lowStemp) lowStemp = Sp; } if (SeasonChoice == 5) { System.out.println("Not valid option"); } } System.out.println("High Temp: " + highStemp); System.out.println("Low temp: " + lowStemp); System.out.println("Sum temp of summer: " + sumtemp); avgtemp = sumtemp / Simulations; System.out.println("The summer average: " + avgtemp); } while (SeasonChoice != 5); } }
Screenshot: