Skip to content

Categories:

Lab 3

This lab dwells further into classes and how to use a different method when sharing information between 
those classes. Lab 2 required the use of objects to be able to use the method of another class. In this 
case, we perform the similar task where instead of creating an object, we inherit methods from another 
class. In order to do that, we use the keyword extends on a class where we want to inherit the methods. 
To use the methods from the inherited class, we use the keyword super. This gives access to the methods, 
and its variables. Other than that, the lab require more classes to take care of more tasks making the 
program look much cleaner.

One of the issues I had with the lab was the logic behind retrieving information in the main method. It 
became difficult when I was trying to change the season or humidity title to random. However, I was able 
to fix the problem by just rearranging the if statements so that the program checks the condition prior 
to generating numbers. All lot of the conditions prior to this lab were generally the same, especially 
the try and catch statement and the while loop "wall" to block the user from entering invalid inputs.

Code:
Main Class:
import java.util.InputMismatchException; //library for input mismatch
import java.util.Scanner; //library for scanner
import java.util.Random; //library for random number generator
public class SensorStatsApp {

	public static void main(String[] args) { //main method
		int start = 1; //integer start = 1
		Scanner choice = new Scanner(System.in);
		Random randObject = new Random(); //object for random
		TemperatureSensor seasonObject = new TemperatureSensor();//object for class TemperatureSensor
		HumiditySensor humidityObject = new HumiditySensor();//object for class Humidity Sensor
		do{ //do-while loop
			int season = 0, season5 = 0, humidity = 0, humidity4 = 0, sim = 0, n = 1, temp = 0, hum = 0, first = 0, firsth = 0;
			int last = 0, lasth = 0, lowest = 999, lowesth = 999, highest = 0, highesth = 0, sum = 0, sumh = 0;
			float avg = 0, avgh = 0, x = 1, xhum = 1; //variables that reset values after loop
			String seasonchoice = null, humiditychoice = null; //strings
			System.out.println("\nWhich season do you want to simulate?"); //print statements
			System.out.println("(1) Winter\n(2) Spring\n(3) Summer\n(4) Fall\n(5) Random\n(6) Exit");
			try{ //catches any input mismatch
				season = choice.nextInt();
				while (season > 6 || season  5 || humidity <1){ //restricts inputs to only 1-5
						System.out.println("Invalid choice! Please re-enter a valid choice!");
						System.out.print("Choice: ");
						humidity = choice.nextInt(); //re-enter value
					}
					if (humidity == 4){ //if user chooses random, choose random humidity
						humidity = 1 + randObject.nextInt(2); //chooses from 1-3
						humidity4 = 4; //Changes title print out to Random:
					}
					if (humidity == 5){ //exits program
						start = 0;
						System.out.println("Exiting Program!");
					}
					else{ //number of generations
						System.out.println("How many simulations do you want to generate?");
						sim = choice.nextInt();
						while (sim  0){ //loop until number of simulations reaches zero
				switch(season){ //switch statement season
				case 1: //if season is 1
					if (season5 == 5){ //Condition only for random
						seasonchoice = "Random: Winter\n";
					}
					else{
						seasonchoice = "Season: Winter\n";
					}
					temp = seasonObject.getWinterTemp(); //uses method to grab random number
					if (n == 1) { //on the first iteration
						first = temp; //first equals temp
					}
					if (temp  highest) { //if temp is greater than highest
						highest = temp; //highest equals temp
					}
					last = temp; //last equals temp
					sum = sum + temp; //sum equals to old sum plus the new temp
					avg = sum/x; //calculate current average
					x++;
					n++; //increment n by 1
					sim--;
					break;
				case 2:
					if (season5 == 5){
						seasonchoice = "Random: Spring\n";
					}
					else{
						seasonchoice = "Season: Spring\n";
					}
					temp = seasonObject.getSpringTemp();
					if (n == 1) { //on the first iteration
						first = temp; //first equals temp
					}
					if (temp  highest) { //if temp is greater than highest
						highest = temp; //highest equals temp
					}
					last = temp; //last equals temp
					sum = sum + temp; //sum equals to old sum plus the new temp
					avg = sum/x;
					x++;
					n++; //increment n by 1
					sim--;
					break;
				case 3:
					if (season5 == 5){
						seasonchoice = "Random: Summer\n";
					}
					else{
						seasonchoice = "Season: Summer\n";
					}
					temp = seasonObject.getSummerTemp();
					if (n == 1) { //on the first iteration
						first = temp; //first equals temp
					}
					if (temp  highest) { //if temp is greater than highest
						highest = temp; //highest equals temp
					}
					last = temp; //last equals temp
					sum = sum + temp; //sum equals to old sum plus the new temp
					avg = sum/x;
					x++;
					n++; //increment n by 1
					sim--;
					break;
				case 4:
					if (season5 == 5){
						seasonchoice = "Random: Fall\n";
					}
					else{
						seasonchoice = "Season: Fall\n";
					}
					temp = seasonObject.getFallTemp();
					if (n == 1) { //on the first iteration
						first = temp; //first equals temp
					}
					if (temp  highest) { //if temp is greater than highest
						highest = temp; //highest equals temp
					}
					last = temp; //last equals temp
					sum = sum + temp; //sum equals to old sum plus the new temp
					avg = sum/x;
					x++;
					n++; //increment n by 1
					sim--;
					break;
				}
				System.out.print("\n"+seasonchoice); //Resulting print statements
				System.out.println("First temperature generated: " + first);
				System.out.println("Last temperature generated: " + last);
				System.out.println("Lowest temperature generated: " + lowest);
				System.out.println("Highest temperature generated: " + highest);
				System.out.println("Total sum of temperatures generated: " + sum);
				System.out.println("Average for the season: " + avg);
				switch(humidity){ //switch statement humidity
				case 1: //case 1
					if (humidity4 == 4){ //Condition only for random
						humiditychoice = "Random: Full";
					}
					else{
						humiditychoice = "Humidity Type: Full";
					}
					hum = humidityObject.getHumidity();
					if (n == 2) { //on the first iteration
						firsth = hum; //first equals temp
					}
					if (hum  highesth) { //if temp is greater than highest
						highesth = hum; //highest equals temp
					}
					lasth = hum; //last equals temp
					sumh = sumh + hum; //sum equals to old sum plus the new temp
					avgh = sumh/xhum; //calculate average
					xhum++;
					break;
				case 2:
					if (humidity4 == 4){
						humiditychoice = "Random: Low";
					}
					else{
						humiditychoice = "Humidity Type: Low";
					}
					hum = humidityObject.getLowHumidity();
					if (n == 2) { //on the first iteration
						firsth = hum; //first equals temp
					}
					if (hum  highesth) { //if temp is greater than highest
						highesth = hum; //highest equals temp
					}
					lasth = hum; //last equals temp
					sumh = sumh + hum; //sum equals to old sum plus the new temp
					avgh = sumh/xhum;
					xhum++;
					break;
				case 3: 
					if (humidity4 == 4){
						humiditychoice = "Random: High";
					}
					else{
						humiditychoice = "Humidity Type: High";
					}
					hum = humidityObject.getHighHumidity();
					if (n == 2) { //on the first iteration
						firsth = hum; //first equals temp
					}
					if (hum  highesth) { //if temp is greater than highest
						highesth = hum; //highest equals temp
					}
					lasth = hum; //last equals temp
					sumh = sumh + hum; //sum equals to old sum plus the new temp
					avgh = sumh/xhum;
					xhum++;
					break;
				}
				System.out.println("\n" + humiditychoice); //resulting print statement
				System.out.println("First humidity reading generated: " + firsth);
				System.out.println("Last humidity reading generated: " + lasth);
				System.out.println("Lowest humidity reading generated: " + lowesth);
				System.out.println("Highest humidity reading generated: " + highesth);
				System.out.println("Total sum of all humidity readings generated: " + sumh);
				System.out.println("Average humidity reading " + avgh);
			}
		}while(start == 1); //do-while condition for ending program
	}
}

Sensor Class
import java.util.Random; //library for random number generation
public class Sensor { //class with overloading methods
	private int reading; //integer reading
	private int range; //integer range
	Random generate = new Random(); //object for random method

	public void setReading(){ //generate numbers from 0-100 (no parameters)
		reading = generate.nextInt(101);
	}

	public void setReading(int u){ //generate numbers from 0-u (one parameter)
		reading = generate.nextInt(u);
	}

	public void setReading(int l, int u){ //generate numbers from l to u (two parameters)
		range = u-l; //subtract u by l to determine number range
		reading = l + generate.nextInt(range); //
	}

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

TemperatureSensor Class
public class TemperatureSensor extends Sensor { //This subclass inherits class Sensor
	public int getWinterTemp(){ //obtains random temperatures from 20-40
		int low = 20, high = 40;
		super.setReading(low,high);
		return super.getReading();
	}

	public int getSpringTemp(){ //obtains random temperatures from 40-70
		int low = 40, high = 70;
		super.setReading(low,high);
		return super.getReading();
	}

	public int getSummerTemp(){ //obtains random temperatures from 70-90
		int low = 70, high = 90;
		super.setReading(low,high);
		return super.getReading();
	}

	public int getFallTemp(){ //obtains random temperatures from 40-60
		int low = 40, high = 60;
		super.setReading(low,high);
		return super.getReading();
	}
}

HumiditySensor Class:
public class HumiditySensor extends Sensor { //This subclass inherits class Sensor
	public int getHumidity(){ //method of numbers ranging 0-100
	super.setReading(); //generate random number
	return super.getReading(); //get return value
	}

	public int getLowHumidity(){ //method of numbers ranging 0-50
	int low = 51; //integer low set to 51
	super.setReading(low); //send low into method
	return super.getReading(); //get return value
	}

	public int getHighHumidity(){ //method of numbers ranging from 50-100
	int low = 50, high = 100; //integer low = 50, integer high = 100
	super.setReading(low, high); //send low and high into method
	return super.getReading(); //get return value
	}
}

image1


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.