Lab Description:
In this lab, I have recreated lab #1 and separate it into classes of “Temperaturesensor” : the class is supposed to get the temperature for 4 seasons and using method to return an expected number.And “TemperatureSensorStats”: the class is supposed to similiar to lab 1 which is to select the option user enter and run the stimulation as user disired, in addition, the simulation will call the corresponding method from TemperatureSensor class.
Code:
import java.util.Scanner; public class TemperatureSensorStats { /** * @param args */ private int [] arr; private TemperatureSensor tt=new TemperatureSensor();/*create Temperature sensor name tt*/ public static void main(String[] args) { // TODO Auto-generated method stub TemperatureSensorStats td=new TemperatureSensorStats(); td.showTemperature();/*using showTemperature method*/ } /*method to display temperature*/ private void showTemperature(){ Scanner sc=new Scanner(System.in); while(true){ System.out.println("Please select your choice:"); /*output the menu*/ System.out.println(" (1)winter ----(2)spring---- (3)summer---- (4) fall ----(5)exit"); switch (sc.nextInt()) {/*get data from keyboard*/ case 1: System.out.println("how many simulations to generate?"); int a=sc.nextInt();/*value enter in keyboard*/ arr=new int[a];/*create new integer call a*/ for(int i=0;i<a;i++){ arr[i]=tt.getWinterTemp();/*add value as for loop goes*/ } showShuju(arr);/*show data*/ break; case 2: System.out.println("how many simulations to generate?"); int b=sc.nextInt();// arr=new int[b]; for(int i=0;i<b;i++){ arr[i]=tt.getSpringTemp(); } showShuju(arr); break; case 3: System.out.println("how many simulations to generate?"); int c=sc.nextInt(); arr=new int[c]; for(int i=0;i<c;i++){ arr[i]=tt.getSummerTemp(); } showShuju(arr); System.out.println("-----------------------"); for(int i:arr){ System.out.println(i); } break; case 4: System.out.println("how many simulations to generate?"); int d=sc.nextInt(); arr=new int[d];/*create a new integer call d*/ for(int i=0;i<d;i++){ arr[i]=tt.getFallTemp();/*add value as loop goes*/ } showShuju(arr);/*show data*/ break; case 5: System.out.println("Quit"); System.exit(0); break; default: break; } } } /*get the data from stimulation and combine*/ private void showShuju(int []arr){ int length=arr.length;/*get data*/ int temp; int sum=0; System.out.println("First temperature generated is:"+arr[0]); System.out.println("Last temperature generated is:"+arr[length-1]); for(int i=0;i<arr.length-1;i++){ for(int j=0;j<arr.length-i-1;j++){ if(arr[j]0.5) return (b+1); else return b; } } class TemperatureSensor { /*get temperature for winter*/ public int getWinterTemp(){ int a=getNum()*2; return a+20; } /*get temperature for Spring*/ public int getSpringTemp(){ int a=getNum()*3; return a+40; } /*get temperature for Summer*/ public int getSummerTemp(){ int a=getNum()*2; return a+70; } /*get temperature for Fall*/ public int getFallTemp(){ int a=getNum()*2; return a+40; } /*get data*/ public int getNum(){ double a=Math.random()*10;/*enter random number between 1 to 10*/ int b=(int)a; return b; } }