Lab 3

Introduction:

This lab is similar to lab 2 but we are using inheritance. First we must create a new class named Sensor. This sensor class will have a set and get method for reading. A setReading() method will generate random numbers between  0 and 100., while a setReading(int u) method will generate random numbers between  0 and u, and another overloaded method setReading( int l, int u) will generate a reading value between l and u. getReading() method will return the value of the interger reading. another class TemperatureSensor is created which will have methods getWinterTemp, getSpringTemp, getSummerTemp, getFallTemp. Method getWinterTemp should return a number between 20-40, getSpringTemp between 40-70, getSummerTemp between 70-90, and getFallTemp between 40-60. Another class would be created called HumiditySensor. It would have methods that are inherited from the Sensor class which would be called getHumidity, getLowHumidity, getHighHumidity. The method getHumidity should return a number between 0-100, getLowHumidity between 0-50, getHighHumidity between 50-100. Finally a new class is created which will contain the main methods to run the application. The class is call SensorApp. The application will ask the user what season to simulate, (1) winter (2) spring (3) summer (4) fall (5) random (6) exit. Random will choose the season for the user. Then ask the user what type of humidity to simulate (1) full range (2) low (3) high (4) random (5) exit. Random will choose the type of humidity for the user. After, the application would ask how many simulation the user want to generate for each decision made for season and humidity.

Season: _________ (if random, then display Random:Summer for example)

 

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

 

Humidity Type: _________ (if random, then display Random:Full for example)

1-      First humidity reading generated

2-      Last humidity reading generated

3-      Lowest humidity reading generated

4-      Highest humidity reading generated

5-      Total sum of all humidity readings generated

6-      Average humidity reading

 

 

Source Code :

//Sensor class

import java.util.Random;

public class Sensor {

	static Random  generator = new Random();

	private static int reading;

	public static  int getReading(){
		return reading;
	}
	public void setReading(){

	}
	public void setReading(int u){
		this.reading=reading;
		reading=generator.nextInt(u);
	}
	public void setReading(int u, int i){
		this.reading=reading;
		reading=generator.nextInt(u)+i;
	}
}
 package lab_3;

//TemperatureSensor class

public class TemperatureSensor extends Sensor {

	public int getWinterTemp(){
		int winterTemp=0;
		super.setReading(21, 20);
		return winterTemp;
	}

	public int getSpringTemp(){
		int springTemp=0;
		super.setReading(31, 40);
		return springTemp;
	}

	public int getSummerTemp(){
		int summerTemp=0;
		super.setReading(21, 70);
		return summerTemp;
	}

	public int getFallTemp(){
		int fallTemp=0;
		super.setReading(21, 40);
		return fallTemp;
	}

}
 package lab_3;
//HumiditySensor class

public class HumiditySensor extends Sensor {

	public int getHumidiy(){ //method to set the values for the humidity
		int humidity=0;
		super.setReading(101); // set the super class reading parameter to 100 so it would read from 0-100
		return humidity;// return the variable humidity
	}

	public int getLowHumidiy(){//method to set the values for the low humidity
		int lowhumidity=0;
		super.setReading(51);// set the super class reading parameter to 50 so it would read from 0-50
		return lowhumidity;
	}

	public int getHighHumidiy(){
		int highhumidity=0;
		super.setReading(51, 50);// set the super class reading parameter to 50 and 100 so it would read from 50-100
		return highhumidity;
	}

}
//SensorApp Class
package lab_3;

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

public class SensorsStatsApp {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		TemperatureSensor ts = new TemperatureSensor();
		HumiditySensor rs= new HumiditySensor();
		int Temperature;
		int reading; 

		Random  generator = new Random();

		Scanner input = new Scanner( System.in ); //Allows the USER to be able to read the user's input
		int RandomHumidity;
		int RandomTemperature;
		int firstTemp;
		int lastTemp;
		int lowestTemp;
		int highestTemp;
		int sumTemp ; //variable for the sum of the first season
		int averageTemp = 0;//variable for the average of the season
		int season; //initialize the variable season
		int simulation;//initialize the variable for the simulation
		int time=0;
		int humidity;
		int Humidity;
		int firstHumidity;
		int lastHumidity;
		int lowestHumidity;
		int highestHumidity;
		int sumHumidity ; //variable for the sum of the first season
		int averageHumidity = 0;
		try{
			while(time<5)//infinity loop 			{ 				System.out.println("What is the season?");//display the message 				System.out.println("Press 1 for Winter.");//display the message 				System.out.println("Press 2 for Spring.");//display the message 				System.out.println("Press 3 for Summer.");//display the message 				System.out.println("Press 4 for Fall.");//display the message 				System.out.println("Press 5 for random.");//display the message 				System.out.println("Press 6 to exit out of the program"); 				season = input.nextInt(); //reads the input from the user 				if(season>6) {
					System.err.println("You have to enter numbers from 1 to 6");
					continue; // goes back to the top of the loop
				}
				if(season == 6)
				{
					System.out.println("Thank you for using the Program");
					System.out.println("Good Bye!!");
					System.exit(0);// exits the program if the user enter 5
				}
				System.out.println("What type of humidity should be simulated?");//display the message
				System.out.println("Press 1 for full range.");//display the message
				System.out.println("Press 2 for low humidity.");//display the message
				System.out.println("Press 3 for high humidity.");//display the message
				System.out.println("Press 4 for random.");//display the message
				System.out.println("Press 5 to exit out of the program");
				humidity = input.nextInt();

				if(humidity>5) {
					System.err.println("You have to enter numbers from 1 to 5");
					continue; // goes back to the top of the loop
				}

				if(humidity == 5)
				{
					System.out.println("Thank you for using the Program");
					System.out.println("Good Bye!!");
					System.exit(0);// exits the program if the user enter 5
				}

				else//loop for the season
				{

					System.out.println("How many simulations?");//display the message
					simulation = input.nextInt(); //reads the input from the user

					firstTemp=generator.nextInt(20) + 40;
					RandomHumidity=generator.nextInt(3)+1;
					RandomTemperature=generator.nextInt(4)+1;
					firstHumidity=generator.nextInt(100);
					lastHumidity=firstHumidity;
					lowestHumidity=100;
					highestHumidity=0;
					sumHumidity = 0;
					lastTemp=firstTemp;
					lowestTemp=100;
					highestTemp=0;
					sumTemp = 0;

					//loop for different amount of simulations

					for(int counter=1 ; counter<=simulation; counter++)  					{ 						//starts the switch statement for each season 						switch( season )  						{ 						case (1): //first case 							Temperature = ts.getWinterTemp(); 						reading=Sensor.getReading();  						if(counter==1) 						{ 							firstTemp=reading ; 						} 						if(counter==simulation) 						{  							lastTemp=reading; 						} 						//Generating the highest and the lowest temperature 						if(reading>highestTemp)
						{
							highestTemp=reading;
						}
						if(reading<lowestTemp) 						{ 							lowestTemp=reading; 						} 						sumTemp =sumTemp + reading; //adds all the generated temps 						break; 						case (2)://second case 							//Generating the highest and the lowest temperature 							Temperature = ts.getSpringTemp(); 						reading=Sensor.getReading();  						if(counter==1) 						{ 							firstTemp=reading ; 						} 						if(counter==simulation) 						{  							lastTemp=reading; 						} 						if(reading>highestTemp)
						{
							highestTemp=reading;
						}
						if(reading<lowestTemp) 						{ 							lowestTemp=reading; 						} 						sumTemp =sumTemp + reading; //adds all the generated temps 						break; 						case (3)://third case 							Temperature = ts.getSummerTemp(); 						reading=Sensor.getReading();  						if(counter==1) 						{ 							firstTemp=reading ; 						} 						if(counter==simulation) 						{  							lastTemp=reading; 						} 						if(reading>highestTemp)
						{
							highestTemp=reading;
						}
						if(reading<lowestTemp) 						{ 							lowestTemp=reading; 						} 						sumTemp =sumTemp + reading;//adds all the generated temps 						break; 						case (4)://forth case 							Temperature = ts.getFallTemp(); 						reading=Sensor.getReading();  						if(counter==1) 						{ 							firstTemp=reading ; 						} 						if(counter==simulation) 						{  							lastTemp=reading; 						} 						if(reading>highestTemp)
						{
							highestTemp=reading;
						}
						if(reading<lowestTemp) 						{ 							lowestTemp=reading; 						} 						sumTemp =sumTemp + reading;//adds all the generated temps 						break; 						case (5)://second case 							//Generating the highest and the lowest temperature 							if (RandomTemperature==1) 							{ 								Temperature = ts.getWinterTemp(); 							} 							else if (RandomTemperature==2) 							{ 								Temperature = ts.getSpringTemp(); 							} 							else if (RandomTemperature==3){ 								Temperature = ts.getSummerTemp(); 							} 							else if (RandomTemperature==4) 							{ 								Temperature = ts.getFallTemp(); 							} 						reading=Sensor.getReading();  						if(counter==1) 						{ 							firstTemp=reading ; 						} 						if(counter==simulation) 						{  							lastTemp=reading; 						} 						if(reading>highestTemp)
						{
							highestTemp=reading;
						}
						if(reading<lowestTemp) 						{ 							lowestTemp=reading; 						} 						sumTemp =sumTemp + reading; //adds all the generated temps 						break; 						} 						//Switch case for the Humidity 						switch(humidity) 						{ 						case(1): 							Humidity = rs.getHumidiy(); 						reading=Sensor.getReading();  						if(counter==1) 						{ 							firstHumidity=reading ; 						} 						if(counter==simulation) 						{  							lastHumidity=reading; 						} 						if(reading>highestHumidity)
						{
							highestHumidity=reading;
						}
						if(reading<lowestHumidity) 						{ 							lowestHumidity=reading; 						} 						sumHumidity =sumHumidity + reading;//adds all the generated temps 						break;	 						case(2): 							Humidity = rs.getLowHumidiy(); 						reading=Sensor.getReading();  						if(counter==1) 						{ 							firstHumidity=reading ; 						} 						if(counter==simulation) 						{  							lastHumidity=reading; 						} 						if(reading>highestHumidity)
						{
							highestHumidity=reading;
						}
						if(reading<lowestHumidity) 						{ 							lowestHumidity=reading; 						} 						sumHumidity =sumHumidity + reading;//adds all the generated temps 						break;	 						case(3): 							Humidity = rs.getHighHumidiy(); 						reading=Sensor.getReading(); 						if(counter==1) 						{ 							firstHumidity=reading ; 						} 						else if(counter==simulation) 						{  							lastHumidity=reading; 						} 						if(reading>highestHumidity)
						{
							highestHumidity=reading;
						}
						else if(reading<lowestHumidity) 						{ 							lowestHumidity=reading; 						} 						sumHumidity =sumHumidity + reading;//adds all the generated temps 						break;	 						case(4): 							if (RandomHumidity==1) 							{ 								Humidity = rs.getHumidiy(); 							} 							else if (RandomHumidity==2) 							{ 								Humidity = rs.getLowHumidiy(); 							} 							else if (RandomHumidity==3){ 								Humidity = rs.getHighHumidiy(); 							} 							else if (RandomTemperature==4) 							{ 								Temperature = ts.getFallTemp(); 							} 						reading=Sensor.getReading(); 						if(counter==1) 						{ 							firstHumidity=reading ; 						} 						else if(counter==simulation) 						{  							lastHumidity=reading; 						} 						if(reading>highestHumidity)
						{
							highestHumidity=reading;
						}
						else if(reading<lowestHumidity)
						{
							lowestHumidity=reading;
						}

						sumHumidity =sumHumidity + reading;//adds all the generated temps

						break;	

						}

					}	

					//Generating the sum and the average for each season

					if (season==1)
					{

						averageTemp = sumTemp/simulation;
						System.out.println("Season: Winter" + "\n");
						System.out.printf("The first temp is %d\n", firstTemp);
						System.out.printf("The last temp is %d\n", lastTemp);
						System.out.printf("The Highest temp is %d\n", highestTemp);
						System.out.printf("The Lowest temp is %d\n", lowestTemp);
						System.out.println("Total sum of all temperatures generated: " + sumTemp+"\n" +
								"Average for the season: " + averageTemp+"\n" );
					}
					if (season==2)
					{
						averageTemp = sumTemp/simulation;
						System.out.println("Season: Spring" + "\n");
						System.out.printf("The first temp is %d\n", firstTemp);
						System.out.printf("The last temp is %d\n", lastTemp);
						System.out.printf("The Highest temp is %d\n", highestTemp);
						System.out.printf("The Lowest temp is %d\n", lowestTemp);
						System.out.println("Total sum of all temperatures generated: " + sumTemp+"\n" +
								"Average for the season: " + averageTemp+"\n" );
					}

					if (season==3)
					{
						averageTemp = sumTemp/simulation;
						System.out.println("Season: Summer" + "\n");
						System.out.printf("The first temp is %d\n", firstTemp);
						System.out.printf("The last temp is %d\n", lastTemp);
						System.out.printf("The Highest temp is %d\n", highestTemp);
						System.out.printf("The Lowest temp is %d\n", lowestTemp);
						System.out.println("Total sum of all temperatures generated: " + sumTemp+"\n" +
								"Average for the season: " + averageTemp+"\n" );
					}

					if (season==4)
					{
						averageTemp = sumTemp/simulation;
						System.out.println("Season: Fall" + "\n");
						System.out.printf("The first temp is %d\n", firstTemp);
						System.out.printf("The last temp is %d\n", lastTemp);
						System.out.printf("The Highest temp is %d\n", highestTemp);
						System.out.printf("The Lowest temp is %d\n", lowestTemp);
						System.out.println("Total sum of all temperatures generated: " + sumTemp+"\n" +
								"Average for the season: " + averageTemp+"\n" );
					}

					if (season==5)
					{

						averageTemp = sumTemp/simulation;
						if (RandomTemperature==1)
						{
							System.out.println("Random: Winter" + "\n");
						}
						if (RandomTemperature==2)
						{
							System.out.println("Random: Spring" + "\n");
						}
						if (RandomTemperature==3)
						{
							System.out.println("Random: Summer" + "\n");
						}
						if (RandomTemperature==4)
						{
							System.out.println("Random: Fall" + "\n");
						}
						System.out.printf("The first temp is %d\n", firstTemp);
						System.out.printf("The last temp is %d\n", lastTemp);
						System.out.printf("The Highest temp is %d\n", highestTemp);
						System.out.printf("The Lowest temp is %d\n", lowestTemp);
						System.out.println("Total sum of all temperatures generated: " + sumTemp+"\n" +
								"Average for the season: " + averageTemp+"\n" );
					}

					//Generating the sum and the average for each season

					if (humidity==1)
					{

						averageHumidity = sumHumidity/simulation;
						System.out.println("Humidity: Full Humidity" + "\n");
						System.out.printf("The First Humidity generated is %d\n", firstHumidity);
						System.out.printf("The Last humidity generated is %d\n", lastHumidity);
						System.out.printf("The Highest Humidity generated is %d\n", highestHumidity);
						System.out.printf("The Lowest Humidity generated is %d\n", lowestHumidity);
						System.out.println("Total sum of all humidity generated: " + sumHumidity+"\n" +
								"Average humidity reading: " + averageHumidity+"\n" );
					}
					if (humidity==2)
					{
						averageHumidity = sumHumidity/simulation;
						System.out.println("Humidity: Low Humidity" + "\n");
						System.out.printf("The First Humidity generated is %d\n", firstHumidity);
						System.out.printf("The Last humidity generated is %d\n", lastHumidity);
						System.out.printf("The Highest Humidity generated is %d\n", highestHumidity);
						System.out.printf("The Lowest Humidity generated is %d\n", lowestHumidity);
						System.out.println("Total sum of all temperatures generated: " + sumHumidity+"\n" +
								"Average for the season: " + averageHumidity+"\n" );
					}

					if (humidity==3)
					{
						averageHumidity = sumHumidity/simulation;
						System.out.println("Humidity: High Humidity" + "\n");
						System.out.printf("The First Humidity generated is %d\n", firstHumidity);
						System.out.printf("The Last humidity generated is %d\n", lastHumidity);
						System.out.printf("The Highest Humidity generated is %d\n", highestHumidity);
						System.out.printf("The Lowest Humidity generated is %d\n", lowestHumidity);
						System.out.println("Total sum of all temperatures generated: " + sumHumidity+"\n" +
								"Average for the season: " + averageHumidity+"\n" );
					}
					if (humidity==4)
					{
						averageHumidity = sumHumidity/simulation;
						if (RandomHumidity==1)
						{
							System.out.println("Random: Full" + "\n");
						}
						if (RandomHumidity==2)
						{
							System.out.println("Random: Low" + "\n");
						}
						if (RandomHumidity==3)
						{
							System.out.println("Random: High" + "\n");
						}

						System.out.printf("The First Humidity generated is %d\n", firstHumidity);
						System.out.printf("The Last humidity generated is %d\n", lastHumidity);
						System.out.printf("The Highest Humidity generated is %d\n", highestHumidity);
						System.out.printf("The Lowest Humidity generated is %d\n", lowestHumidity);
						System.out.println("Total sum of all temperatures generated: " + sumHumidity+"\n" +
								"Average for the season: " + averageHumidity+"\n" );
					}

				}
			}
		}catch(Exception e){
			System.err.println("Try again, you must press numbers from 1 to 6 for decision... simulation must be an interger.");
			return;

		}

	}
}

 

Screen shot:

lab 3-2 ss

lab 3 ss

 

Leave a Reply

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