Lab1

Description:

create a temperature sensor simulator. First, display a menu that will ask the user what season to simulate (1) winter (2) spring (3) summer (4) fall or (5) to exit. Once the user selects the season the program should ask the user how many simulations to generate. Each simulation will produce a random numbers as follows: for winter a number between 20-40, for spring between 40-70, for summer between 70-90, and for fall between 40-60. For each iteration keep track of the statistics.

Code:

import java.util.Scanner;

public class TemperatureDemo {

	/**
	 * @param args
	 */
	private int [] arr;

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		TemperatureDemo td=new TemperatureDemo();
		td.showTemperature();
	}

	private void showTemperature(){
		Scanner sc=new Scanner(System.in);
		while(true){
		System.out.println("Please select your choice:");
		System.out.println(" (1)winter ----(2)spring---- (3)summer---- (4) fall ----(5)exit");

		switch (sc.nextInt()) {
		case 1:
			System.out.println("how many simulations to generate?");
			int a=sc.nextInt();

			arr=new int[a];
			for(int i=0;i<a;i++){
				int aa=getNum()*2;
				arr[i]=aa+20;
			}

			showShuju(arr);
			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++){
				int aa=getNum()*3;
				arr[i]=aa+40;
			}

			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++){
				int aa=getNum()*2;
				arr[i]=aa+70;
			}

			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];
			for(int i=0;i<d;i++){
				int aa=getNum()*2;
				arr[i]=aa+40;
			}

			showShuju(arr);
	break;
		case 5:
			System.out.println("Quit");
			System.exit(0);
			break;
		default:
			break;
		}
	}
}

	private void showShuju(int []arr){
		int length=arr.length;
		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]<arr[j+1]){	
					temp = arr[j];	
					arr[j]=arr[j+1];					
					arr[j+1]=temp;	
					}			
				}		
			}
		System.out.println(" Lowest temperature generated is:"+arr[length-1]);
		System.out.println("Highest temperature generated:"+arr[0]);

		for(int ii:arr){
			sum+=ii;
		}
		System.out.println("Total sum of all temperatures generated is:"+sum);
		System.out.println("Average for the season is:"+sum/length);
		 }

	public int getNum(){
		double a=Math.random()*10;
		int b=(int)a;
		return b;
	}
}

demo

Leave a Reply

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