Lab 3

Description:

The program in this lab has the same function as the one in last lab, but this one uses inheritance.

I first created Sensor.java file. This file is used to generate integer random number.

I then created TemperatureSensor.java and HumiditySensor.java. These two files are basically the same. Both of them inherit Sensor.java, but the meaning of the data generated form each file is different; one is temperature, and the other one is humidity.

The last file I created is SensorsStatsApp.java. This file is almost the same as last lab. However, I have to use previous two file to handle the data generate.

 

Code:

package Lab3; 
//Written by Luis Hu

public class Sensor {

	private static int reading;

	//return reading
	public static int getReading() {
		return reading;
	}

	//generate range between l and u
	public static void setReading(int l, int u) {

		int reading = (int) (Math.random()*(u-l)+l);

		Sensor.reading = reading;
	}

	public static void setReading( int u) {
		setReading(0, u);
	}	
	public static void setReading() {
		setReading(0,100);
	}

}
package Lab3;

public class TemperatureSensor extends Sensor {

	public static int getSpring() {
		setReading(40, 70);
		return getReading();
	}

	public static int getSummer() {
		setReading(70, 90);
		return getReading();
	}

	public static int getFall() {
		setReading(40, 60);
		return getReading();
	}

	public static int getWinter() {
		setReading(20, 40);
		return getReading();
	}

}
package Lab3;
//Written by Luis Hu

public class HumiditySensor extends Sensor {

	public static int getHumidity() {
		setReading();
		return getReading();
	}

	public static int getLowHumidity() {
		setReading(50);
		return getReading();
	}

	public static int getHighHumidity() {
		setReading(50, 100);
		return getReading();
	}

}
package Lab3;

import java.util.Scanner;

public class SensorsStatsApp {

	private static Scanner scanner = new Scanner(System.in);

	public static void main(String[] args) {
		int a;
		int simulateSeason;
		int simulateHumidity;
		int roll = 0;

		int programLooping = 0;
		outer: while (programLooping == 0) {

			//Get input and set up season selection
			System.out
					.println("Selece a season to simulate:\n(1) winter \n(2) spring \n(3) summer \n(4) fall \n(5) random \n(6) exit");

			String userSelectSeason = scanner.nextLine();

			switch (userSelectSeason) {
			case ("1"):
			case ("2"):
			case ("3"):
			case ("4"):
				simulateSeason = Integer.parseInt(userSelectSeason);
				break;
			case ("5"):
				simulateSeason = (int) (Math.random() * 3 + 1);
				break;
			case ("6"):
				programLooping = 1;
				return;
			default:
				System.out.print("invalid input\n");
				continue outer;
			}

			//Get input and set up humidity selection
			System.out
					.println("Select humidity simulate range \n(1) full range \n(2) low \n(3) high \n(4) random \n(5) exit");

			String userSelectHumidity = scanner.nextLine();

			switch (userSelectHumidity) {
			case ("1"):
			case ("2"):
			case ("3"):
				simulateHumidity = Integer.parseInt(userSelectHumidity); 
				break;
			case ("4"):
				simulateHumidity = (int) (Math.random() * 3 + 1);
				break;
			case ("5"):
				programLooping = 1;
				return;
			default:
				System.out.print("invalid input\n");
				continue outer;
			}

			// promote user to set up the number of trial
			System.out.println("Enter number of trials you want:");
			String StInTrial = scanner.nextLine();
			int intInTrial1;

			try { // check if input is valid
				intInTrial1 = Integer.parseInt(StInTrial);
			} catch (NumberFormatException ex) {
				System.out.println("invalid input\n");
				continue outer;
			}

			//display selection
			System.out.print("Selected season: "
					+ getSeason(Integer.parseInt(userSelectSeason)));
			if (Integer.parseInt(userSelectSeason) == 5) {
				System.out.println(" (" + getSeason(simulateSeason) + ")");
			} else
				System.out.println("");
			System.out.print("Selected humidity: "
					+ getHumidity(Integer.parseInt(userSelectHumidity)));
			if (Integer.parseInt(userSelectHumidity) == 4) {
				System.out.println(" (" + getHumidity(simulateHumidity) + ")");
			} else
				System.out.println("");
			System.out.println("For " + intInTrial1 + " trial(s)");

			//processing Temperature
			System.out.println("==Temperature Data==");

			int highTemp = 0;
			int lowTemp = 100;
			int totalTemp = 0;

			for (int Counter = 0; Counter < intInTrial1; Counter++) {
				switch(simulateSeason){
				case 1:
					roll = TemperatureSensor.getWinter();
					break;
				case 2:
					roll = TemperatureSensor.getSpring();
					break;
				case 3:
					roll = TemperatureSensor.getSummer();
					break;
				case 4:
					roll = TemperatureSensor.getFall();
					break;
				}

				if (Counter ==  0) System.out.println("The first temperature  generated is :" + roll);
				if (Counter ==  intInTrial1) System.out.println("The last temperature  generated is :" + roll);
				if (highTemproll) lowTemp = roll;
				totalTemp += roll;				

			}
			// print out the rest of collected data .
			System.out.println("The highest temperature  generated is :"
					+ highTemp);
			System.out.println("The lowest temperature  generated is :"
					+ lowTemp);
			System.out.println("The sum of generated temperature  is :"
					+ totalTemp);
			System.out.println("The avarage of generated temperature  is :"
					+ totalTemp / intInTrial1);

			// processing Humidity
			System.out.println("==Humidity Data==");

			int highHumidity = 0;
			int lowHumidity = 100;
			int totalHumidity = 0;

			for (int Counter = 0; Counter < intInTrial1; Counter++) {
				switch(simulateHumidity){
				case 1:
					roll = HumiditySensor.getHumidity();
					break;
				case 2:
					roll = HumiditySensor.getLowHumidity();
					break;
				case 3:
					roll = HumiditySensor.getHighHumidity();
					break;
				}

				if (Counter ==  0) System.out.println("The first humidity reading generated is :" + roll);
				if (Counter ==  intInTrial1) System.out.println("The last humidity reading generated is :" + roll);
				if (highHumidityroll) lowHumidity = roll;
				totalHumidity += roll;				

			}
			// print out the rest of collected data .
			System.out.println("The highest humidity reading  generated is :"
					+ highHumidity);
			System.out.println("The lowest humidity reading  generated is :"
					+ lowHumidity);
			System.out.println("The sum of generated humidity reading  is :"
					+ totalHumidity);
			System.out.println("The avarage of generated humidity reading  is :"
					+ totalHumidity / intInTrial1);

			System.out.println("==End of Simulating==");

		}
	}

	public static String getSeason(int a) {
		String b = "";
		switch (a) {
		case 1:
			b = "winter";
			break;
		case 2:
			b = "spring";
			break;
		case 3:
			b = "summer";
			break;
		case 4:
			b = "fall";
			break;
		case 5:
			b = "random";
			break;
		}
		return b;

	}

	public static String getHumidity(int a) {
		String b = "";
		switch (a) {
		case 1:
			b = "full range";
			break;
		case 2:
			b = "low";
			break;
		case 3:
			b = "high";
			break;
		case 4:
			b = "random";
			break;
		}
		return b;

	}

}

Screenshot:

Capture

Leave a Reply

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