Lab 2

“CLASS”

LAB DESCRIPTION:

This lab will take the same action like in lab 1, this will pick random temperature depending on season (winter, spring, summer or autumn), and then the program will pick the first, last and calculate the total, average temperature. However, this lab will not push all code in main function, we will create a class, which will content a function to get temperature. In main function, we only need to create an object with the key word “new” based on created class, and then we can call their function by “objectname.getWinterTemp”.

Class will cut off our long code in lab1 and the program will be easier to read and execute. When we create some class, it can also use in other main function so it is really useful to use class in program.

In addition, this lab also use the new key word, which is ” try and catch”, this method will try to execute some codes, if they catch some error they will execute the code in catch’s function.

CODE:

CLASS

//* Huy Ly
 * CET 3640 SecTion:D476
 * Date: Feb 16, 2014
 */
import java.util.Random;

public class TemperatureSensor1 
{
	//Random Method
	Random RanNum = new Random();

	//Getting Winter Temperature Class
	public int getWinterTemp()
	{
		int WinterTemp=RanNum.nextInt(21)+20;
		return WinterTemp;
	}

	//Getting Spring Temperature Class
	public int getSpringTemp()
	{
		int SpringTemp=RanNum.nextInt(31)+40;
		return SpringTemp;
	}

	//Getting Summer Temperature Class
	public int getSummerTemp()
	{
		int SummerTemp=RanNum.nextInt(21)+70;
		return SummerTemp;
	}

	//Getting Fall Temperature Class
	public int getFallTemp()
	{
		int FallTemp=RanNum.nextInt(21)+40;
		return FallTemp;
	}

	//Showing user the Output
	public void showOutput(double TotalTemp,double AveTemp, int FirstTemp,int LastTemp,int HighTemp, int LowTemp)
	{
		System.out.println("Total Temperature of the season is: " + TotalTemp + " Degree");
		System.out.println("Average Temperature of the season is: " + AveTemp + " Degree");
		System.out.println("First Temperature of the season is: " + FirstTemp + " Degree");
		System.out.println("Last Temperature of the season is: " + LastTemp + " Degree");
		System.out.println("Highest Temperature of the season is: " + HighTemp + " Degree");
		System.out.println("Lowest Temperature of the season is: " + LowTemp + " Degree");
	}
}

Main Body:
import java.util.InputMismatchException;
import java.util.Scanner;
//import java.io.IOException;

public class TemperatureSensorStart
{
	public static void main(String[] args) //throws IOException
	{
	Scanner keyboard = new Scanner(System.in);
	int season=0;
	int input=0;
	//Do while loop to re-do the simulation
	do
	{

	//Asking for season
	System.out.println("Please! Enter the season for simulating:");
	System.out.println("Season Code: Winter(1); Spring(2);Summer(3);Fall(4); (5) to Exit");

	//Try to catch for errors
	try
	{
                //Checking for Integer input
	        while(!keyboard.hasNextInt())
	        {
		      System.out.println("This is not an Integer Number");
		      keyboard.next();
	        }
		season = keyboard.nextInt();

                //Checking for valid Number
		if(season>5||season<=0)
		{
		      throw new InputMismatchException("Your input is invalid! Please try again!");
		}

		//Checking for exit system
		if(season==5)
			System.exit(0);

		//Asking for simulation
		System.out.println("How many time do you want to simulate?");
		input = keyboard.nextInt();
	}
	catch(InputMismatchException ex)
	{
		System.out.println(ex.getMessage());
		continue;
	}

	//Call for TemperatureSensor Object
	TemperatureSensor1 Temperature = new TemperatureSensor1();
	//Get new Temperature
	int newTemp=0;
	//First Temperature
	int FirstTemp=0;
	//Last Temperature
	int LastTemp=0;

	//High Temperature
	int HighTemp=0;
	//Low Temperature
	int LowTemp=0;

	//Total Temperature
	double TotalTemp=0;
	//Average Temperature
	double AveTemp;

	for ( int i=0; i<input; i++)
	{
		//Checking for season (using getTemperature)
		if(season==1)
			newTemp = Temperature.getWinterTemp();
		else if (season==2)
			newTemp = Temperature.getSpringTemp();
		else if (season==3)
			newTemp = Temperature.getSummerTemp();
		else if (season==4)
			newTemp = Temperature.getFallTemp();

		//Calculate Total Temperature		
		TotalTemp+=newTemp;
                System.out.println("Current Temperature is: "+newTemp + " Degree");
                //Assign for first, high and low temperature
		if (i==0)
		{
			FirstTemp=newTemp;
			HighTemp=newTemp;
			LowTemp=newTemp;

		}
		//Assign for last temperature
		if (i==(input-1))
		    LastTemp=newTemp;

		//Checking for high and low temperature
		if(newTemp>HighTemp)
		HighTemp=newTemp;
		if(newTemp<LowTemp)
		    LowTemp=newTemp;
	}	

	//Calculate Average Temperature
	AveTemp=TotalTemp/input;

	//Calling outputTemperature method
	Temperature.showOutput(TotalTemp,AveTemp,FirstTemp,LastTemp,HighTemp,LowTemp);

	//Asking for simulating again
	System.out.println("Do you want to simulate again? (1==Yes;5==No)");
	season = keyboard.nextInt();
	}while(season!=5);
}
}

SCREENSHOT: 

Lab2-1

Lab2-2