Lab 3

Lab Description:

This Lab is carried on since from Lab 1 and Lab 2, extending it to be used with multiple classes and using inheritance to bring down some methods. Inheritance allows programmers the flexibility to share some traits between different classes and provides ability to do moderate modification or overwrite whole.

All the features that was added during Lab 1 and Lab 2 are preserved in this program. Some features that was preserved in the program are checking for wrong inputs and correctly leads the user through the menus using try/catch block. Added feature to the program includes random choice in the menu which the program randomly selects an option. Another feature is added humidity sensor simulation in addition to preexsisting temperature sensor.

Source Code:

SensorsStatsApp.java

package Lab3;

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

public class SensorsStatsApp {

	public static int simNumChoice;
	private static int temp[], hum[];
	private static String season = null, humType = null, tempRanChoice = " ",
			humRanChoice = " ";

	public static void main(String[] args) {
		TemperatureSensor tempSensor = new TemperatureSensor();
		HumiditySensor humSensor = new HumiditySensor();
		Random ranNum = new Random();
		int seasonMenu = 0, humidityMenu = 0;

// while(true) allow it to loop infinitely
		while (true) {
			try {
// Display Menu for Selecting Seasons and Checks Input			
				Scanner input = new Scanner(System.in);
				System.out.println("\nChoose your Season: \n" + "(1)Winter\n"
						+ "(2)Spring\n" + "(3)Summer\n" + "(4)Fall\n"
						+ "(5)Random\n" + "(6)EXIT");
				seasonMenu = input.nextInt();
				if (seasonMenu > 6 || seasonMenu  5 || humidityMenu <= 0) {
					System.out
							.println("You Only Had 5 Options \nTry Again from the Top!");
					continue;
				}
				// Selects Random Humidity Type
				if (humidityMenu == 4) {
					humRanChoice = " *Random* ";
					humidityMenu = ranNum.nextInt(3) + 1;
				} else if (humidityMenu == 5) {
					System.out.println("BYE!");
					System.exit(0);
				}
			} catch (Exception e) {
				System.out.println("INTEGER ONLY! \nTry Again from the Top!");
				continue;
			}

			try {
// Asks Number of Simulations and Checks Input
				Scanner input = new Scanner(System.in);
				System.out.println("How Many Simulations?");
				simNumChoice = input.nextInt();
			} catch (Exception e) {
				System.out.println("INTEGER ONLY! \nTry Again from the Top!");
				continue;
			}
// Switch for Seasons
			switch (seasonMenu) {
			case 1:
				season = "Winter";
				temp = tempSensor.getWinterTemp();
				break;
			case 2:
				season = "Spring";
				temp = tempSensor.getSpringTemp();
				break;
			case 3:
				season = "Summer";
				temp = tempSensor.getSummerTemp();
				break;
			case 4:
				season = "Fall";
				temp = tempSensor.getFallTemp();
				break;
			default:
				System.out.println("You Only Have 6 Options");
			}
// Display All Temperature Info to User			
			tempOutput();
// Switch for Humidity
			switch (humidityMenu) {
			case 1:
				humType = "Full";
				hum = humSensor.getHumidity();
				break;
			case 2:
				humType = "Low";
				hum = humSensor.getLowHumidity();
				break;
			case 3:
				humType = "High";
				hum = humSensor.getHighHumidity();
				break;
			default:
				System.out.println("You Only Have 5 Options");
			}
//	Display All Humidity Info to User		
			humOutput();

		} // END While(true)

	}

// Method For Calculating all Required Info
	public static void tempOutput() {
	// Output All Values
		// for (int i = 0; i < simNumChoice; ++i) {
		// System.out.println(temp[i] + " ");
		// }
	// Display Season, First & Last Temp
		System.out.println("\nSeason:" + tempRanChoice + season);
		System.out.println("1- First Temp: " + temp[0] + "\n2- Last Temp: "
				+ temp[simNumChoice - 1]);
	// Display Lowest and Highest Temp
		int minNum = Integer.MAX_VALUE, maxNum = Integer.MIN_VALUE;
		for (int i = 0; i  temp[i]) {
				minNum = temp[i];
			}
			if (maxNum < temp[i]) {
				maxNum = temp[i];
			}
		}
		System.out.println("3- Lowest Temp: " + minNum + "\n4- Highest Temp: "
				+ maxNum);
	// Display Sum and Average
		double sum = 0;
		for (int i = 0; i < simNumChoice; ++i) {
			sum += Double.valueOf(temp[i]);
		}
		double avg = sum / simNumChoice;
		System.out.printf("5- Total Sum: %.00f\n", sum);
		System.out.printf("6- Average: %.02f\n", avg);
	}

	public static void humOutput() {
	// Output All Values
		// for (int i = 0; i < simNumChoice; ++i) {
		// System.out.println(hum[i] + " ");
		// }
	// Display Humidity Type, First & Last Humidity Value
		System.out.println("\nHumidity Type:" + humRanChoice + humType);
		System.out.println("1- First Humidity: " + hum[0]
				+ "\n2- Last Humidity: " + hum[simNumChoice - 1]);
	// Display Lowest and Highest Humidity
		int minNum = Integer.MAX_VALUE, maxNum = Integer.MIN_VALUE;
		for (int i = 0; i  hum[i]) {
				minNum = hum[i];
			}
			if (maxNum < hum[i]) {
				maxNum = hum[i];
			}
		}
		System.out.println("3- Lowest Humidity: " + minNum
				+ "\n4- Highest Humidity: " + maxNum);
	// Display Sum and Average
		double sum = 0;
		for (int i = 0; i < simNumChoice; ++i) {
			sum += Double.valueOf(hum[i]);
		}
		double avg = sum / simNumChoice;
		System.out.printf("5- Total Sum: %.00f\n", sum);
		System.out.printf("6- Average: %.02f\n", avg);
	}

}

Sensor.java

package Lab3;

import java.util.Random;

public class Sensor {

	private int[] reading; 
	private Random ranNum = new Random();

	public void setReading() {
		reading = new int[SensorsStatsApp.simNumChoice];
		for (int i = 0; i < SensorsStatsApp.simNumChoice; ++i) {
			this.reading[i] = ranNum.nextInt(101);
		}
	}

	public void setReading(int u) {
		reading = new int[SensorsStatsApp.simNumChoice];
		for (int i = 0; i < SensorsStatsApp.simNumChoice; ++i) {
			this.reading[i] = ranNum.nextInt(u + 1);
		}
	}

	public void setReading(int l, int u) {
		reading = new int[SensorsStatsApp.simNumChoice];
		for (int i = 0; i < SensorsStatsApp.simNumChoice; ++i) {
			this.reading[i] = ranNum.nextInt(l + 1) + u;
		}
	}

	public int[] getReading() {
		return reading;
	}
}

TemperatureSensor.java

package Lab3;

public class TemperatureSensor extends Sensor {

//	Creates Bounds for each seasons
//	setReading(20,20) is equivalent to setting bounds from 20 to 40

	public int[] getWinterTemp() {
		super.setReading(20, 20);
		int winterTemp[] = super.getReading();
		return winterTemp;
	}

	public int[] getSpringTemp() {
		super.setReading(30, 40);
		int springTemp[] = super.getReading();
		return springTemp;
	}

	public int[] getSummerTemp() {
		super.setReading(20, 70);
		int summerTemp[] = super.getReading();
		return summerTemp;
	}

	public int[] getFallTemp() {
		super.setReading(20, 40);
		int fallTemp[] = super.getReading();
		return fallTemp;
	}
}

HumiditySensor.java

package Lab3;

public class HumiditySensor extends Sensor{

//	Creates Bounds for each Humidity Type
//	setReading(100,0) is equivalent to setting bounds from 0 to 100

	public int[] getHumidity(){
		super.setReading(100,0);
		int[] humidity = super.getReading();
		return humidity;
	}

	public int[] getLowHumidity(){
		super.setReading(50,0);
		int[] lowHumidity = super.getReading();
		return lowHumidity;
	}

	public int[] getHighHumidity(){
		super.setReading(50,50);
		int[] highHumidity = super.getReading();
		return highHumidity;
	}
}

Screenshots:

Lab 3_Output

Leave a Reply

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