Lab #3

Lab Description:

This Lab was by far the most intricate as it incorporated the previous two labs and required the utilization of inheritance. The lab required making 3 classes, 2 of which inherited the 1st class. Afterwards a main method was supposed to implement the methods from the above mentioned classes and create a program which as the previous labs printed out statistical data regarding Temperature and a new Statistical data similar to the temperature statistical data structure called humidity. Additionally a feature for the user was added to allow random selection. This lab was very troublesome to accomplish to its length and confusing inheritance requirement.

Code:

//Yevgeniy Babkin CET 3640 Lab#3 Inheritance


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


public class SensorsStatsApp {

    //objects
    static TemperatureSensor tempsensor = new TemperatureSensor();
    static HumiditySensor humiditysensor = new HumiditySensor();
    private static Scanner userinput;
    //main executable statement
    public static void main(String[] args) {

        int seasonType;
        int humdityType;

        do {
            displayTempMenu();
            seasonType = getInput(1);
            displayHumidityMenu();
            humdityType = getInput(2);
            triggerSimulation(seasonType, humdityType);
        } while (true);

    }
    // cleaner better code possible due to this extra method
    static void triggerSimulation(int seasonType, int humdityType){

        int n,temperature=0,humidity=0;
        int totalTemp=0, firstTemp=0, lowTemp=100, highTemp=0, lastTemp=0;
        int totalHumidity=0, firstHumidity=0, lowHumidity=100, highHumidity=0,  lastHumidity=0;
        double  avgTemp=0, avgHumidity=0;
        boolean seasonRandom = false, humidityRandom = false;

        Random r = new Random();
        if(seasonType ==5){
            seasonType = r.nextInt(4 + 1) + 1;
            seasonRandom = true; 
        }

        if(humdityType ==4){
            humdityType = r.nextInt(3 + 1) + 1;
            humidityRandom = true; 
        }

        System.out.println("How many simulations would you like to generate? : ");
        n=getInput(3);


        //For loop - to generate random numbers and statistics of those numbers
        for(int i = 0;i<n;i++){
            temperature = getSeasonTemp(seasonType);
            humidity = getHumidity(humdityType);



            //Calculating Statistics for Temperature

            if (i == 0) { //on the first iteration
                firstTemp = temperature; //first equals temp
            }
            if (temperature < lowTemp) { //if temp is less than lowest (pass the first time since lowest equals 999)
                lowTemp = temperature; //lowest equals temp
            }
            if (temperature > highTemp) { 
                highTemp = temperature; 
            }
            lastTemp = temperature;
            totalTemp = totalTemp + temperature; 
            avgTemp = totalTemp/n; //calculate current average

            //Calculating statistics for humidity
            if (i == 0) { 
                firstHumidity = humidity; 
            }
            if (humidity < lowTemp) { 
                lowHumidity = humidity; 
            }
            if (humidity > highTemp) { 
                highHumidity = humidity; 
            }
            lastHumidity = humidity;
            totalHumidity = totalHumidity + humidity; 
            avgHumidity = totalHumidity/n; //calculate current average
            n++;
            i++;

        } 


        System.out.println("\n\n---------------Statistics---------------");
        System.out.print("\nSeason: ");
        if(seasonRandom) System.out.print("Random:");
        System.out.println(getSeasonName(seasonType));
        System.out.println("1 First temperature generated: "+firstTemp);
        System.out.println("2 Last temperature generated: "+lastTemp);
        System.out.println("3 Lowest temperature generated: "+lowTemp);
        System.out.println("4 Highest temperature generated: "+highTemp);
        System.out.println("5 Total sum of all temperatures generated: "+totalTemp);
        System.out.println("6 Average for the season: "+avgTemp);

        System.out.print("\nHumidity Type: ");
        if(humidityRandom) System.out.print("Random:");
        System.out.println(getHumidityName(humdityType));
        System.out.println("1 First humidity reading generated: "+firstHumidity);
        System.out.println("2 Last humidity reading generated: "+lastHumidity);
        System.out.println("3 Lowest humidity reading generated: "+lowHumidity);
        System.out.println("4 Highest humidity reading generated: "+highHumidity);
        System.out.println("5 Total sum of all humidity readings generated: "+totalHumidity);
        System.out.println("6 Average humidity reading: "+avgHumidity);
        System.out.println("\n");
    }

    static int getSeasonTemp(int seasonType) {

        switch (seasonType) {
        case 1:
            return tempsensor.getWinterTemp();
        case 2:
            return tempsensor.getSpringTemp();
        case 3:
            return tempsensor.getSummerTemp();
        case 4:
            return tempsensor.getFallTemp();
        }
        return 0;

    }

    static int getHumidity(int humdityType) {

        switch (humdityType) {
        case 1:
            return humiditysensor.getHumidity();
        case 2:
            return humiditysensor.getLowHumidity();
        case 3:
            return humiditysensor.getHighHumidity();
        }
        return 0;
    }

    static String getSeasonName(int seasonType) {

        switch (seasonType) {
        case 1:
            return "Winter";
        case 2:
            return "Spring";
        case 3:
            return "Summer";
        case 4:
            return "Fall";
        }
        return null;
    }

    static String getHumidityName(int humdityType) {

        switch (humdityType) {
        case 1:
            return "Full";
        case 2:
            return "Low";
        case 3:
            return "High";
        }
        return null;
    }

    static void displayTempMenu() {

        System.out.println("Choose a Season to simulate:");
        System.out.println("(1) Winter");
        System.out.println("(2) Spring");
        System.out.println("(3) Summer");
        System.out.println("(4) Fall");
        System.out.println("(5) Random");
        System.out.println("(6) Exit");

    }

    static void displayHumidityMenu() {

        System.out.println("Choose Humdity range:");
        System.out.println("(1) Full Range");
        System.out.println("(2) Low Range");
        System.out.println("(3) High Range");
        System.out.println("(4) Random");
        System.out.println("(5) Exit");

    }

    static int getInput(int menu) {
        int option = 0;
        boolean inputError = true;
        userinput = new Scanner(System.in);

        do {
            try {
                option = Integer.parseInt(userinput.nextLine());
                if (option > 6) {
                    System.out.println("Number should be between 1 - 6");
                    throw new Exception();
                } else if (menu==2 & option>5) {
                    System.out.println("Number should be between 1 - 5");
                    throw new Exception();
                } else{
                    inputError = false; 
                }

            } catch (Exception e) {
                System.out.println("Invalid Entry! Please Enter Integer from 1-6");
                userinput.reset();
            }
        } while (inputError);

        // Exit if user chooses option 6
        if( (menu==1 & option >6) ||(menu ==2 & option >5)){
            System.out.println("Exiting, Thank You for using this program!");
            System.exit(0);
        } 

        return option;
    }

}


//Sensor Class:

import java.util.Random;

public class Sensor {

	private int temperature;

	Random r = new Random();

	public int getTemperature() {
		return temperature;
	}

	public void setTemperature() {
		temperature = r.nextInt(100 + 1);
	}

	public void setTemperature(int u) {
		temperature = r.nextInt(u + 1);
	}

	public void setTemperature(int l, int u) {
		temperature = 1+ r.nextInt(u - 1);
	}

}

//Temperature Sensor Class:
public class TemperatureSensor extends Sensor {

	public int getWinterTemp(){
		super.setTemperature(20, 40);
		return super.getTemperature();
	}

	public int getSpringTemp(){
		super.setTemperature(40, 70);
		return super.getTemperature();
	}

	public int getSummerTemp(){
		super.setTemperature(70, 90);
		return super.getTemperature();
	}

	public int getFallTemp(){
		super.setTemperature(40, 60);
		return super.getTemperature();
	}

}

//Humidity Sensor Class:

public class HumiditySensor extends Sensor {

	public int getHumidity(){
		super.setTemperature();
		return super.getTemperature();
	}

	public int getLowHumidity(){
		super.setTemperature(50);
		return super.getTemperature();
	}

	public int getHighHumidity(){
		super.setTemperature(50, 100);
		return super.getTemperature();
	}

}

Screenshot:

lab#3 java screenshot

 

Leave a Reply

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