Lab 2

Lab Description: In this lab, the goal was to create the same program used in lab 1, but with classes in Java. The original code in the previous program was used to create a temperature simulator. The sections of code to simulate each season were converted to classes instead in lab 2. Finally the section of code that keeps track of statistical data such as the highest temperature , mean value of the seasons and the menu was also converted into a class. By completing this lab, it was possible to observe the functionality of classes in Java programming. Classes allow the program code to be compacted into a smaller code that also has more functionality.

 

Source Code: The code below is what was used to create the Java program in lab 2.

Here is the first class used

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);

	}
}

 

Here is the second class used.

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);

	}

}


 

Screenshots: The image below is the screenshot of the output once the code is executed.

lab2

Leave a Reply

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