Lab 1

Lab Description:

Lab 1 was basically about creating a menu that asks you which season  you would like to choose. The user will have the option to choose winter, spring, fall, and summer. This menu will keep on repeating until the user inputs the “0”. This is done by using the do and while loop. The application than asks the user how many times do you want to do the simulation. The user than input a number and the application will simulate random numbers up to the input of the user.  I do this by using the random generator and specifying the range by for example if the winter is from 20-40 than I will do 40-20+1 and than the output plus 20. I than also make sure that I use array because the output the application needs to print out needs to be put in array so that I can find the first, last, mean, average, and so on. Mostly to find the mean and highest, lowest and the other output that needs to be print out.

Code:

package Lab1;

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

public class temperaturesensor {
	public static void main(String[] args)
	{
		int season;
		int simu=0;
		int sum = 0;
		int average=0;
		Scanner reader = new Scanner(System.in);

		do { // the do command will create a loop for the menu to appear until the user enter0 

		System.out.println("Select Season");
		System.out.println("(1) Winter (2) Summer (3) Fall (4) Spring (0) Exit");
		season=reader.nextInt();

		if (season==1)
		{	
			System.out.println("How many simulation do you want?");
			simu=reader.nextInt();
			Random random =new Random();
			int myarray[] = new int[simu]; // creating an array 
			//random generator
		for(int i =0; i<simu; i++){
		  myarray[i]=random.nextInt(21)+20;
		  System.out.println(" temperature generated"+ myarray[i]);     

		 }
		System.out.println("First temperature generated"+ myarray[0]);
		//finds the mean 
		for(int i=0; i<simu; i++)
		{
		sum += myarray[i];	
		}
		average= sum/simu;
		//finds the highest temperature
		for(int i=0; i<simu; i++) 		{ 			if(myarray[i]> myarray[0] )
			{	
				myarray[0]= myarray[i];	
			}	
		}
		//find the lowest temperature

		    System.out.println("Last temperature generated"+ myarray[simu-1]);
		    System.out.println("Total sum of all temperatures generated"+ sum);
		    System.out.println("Average for the season"+ average);
		    System.out.println("Highest temperature generated"+ myarray[0]);
	myarray[0]=myarray[0];		
	for(int i=0; i<simu; i++)
	{
		if(myarray[i]< myarray[0] )
		{	
			myarray[0]= myarray[i];	
		}		
	}
	 System.out.println("Lowest temperature generated"+ myarray[0]);	
		}
		else if(season==2)
		{

			System.out.println("How many simulation do you want?");
			simu=reader.nextInt();
			Random random =new Random();
			int myarray[] = new int[simu];
			//random generator
		for(int i =0; i<simu; i++){
		  myarray[i]=random.nextInt(21)+70;
		  System.out.println(" temperature generated"+ myarray[i]);     
		 }
		System.out.println("First temperature generated"+ myarray[0]);
		//finds the mean
		for(int i=0; i<simu; i++)
		{
		sum += myarray[i];	
		}
		average= sum/simu;
		//finds the highest temperature
		for(int i=0; i<simu; i++) 		{ 			if(myarray[i]> myarray[0] )
			{	
				myarray[0]= myarray[i];	
			}	
		}
		//find the lowest temperature
		    System.out.println("Last temperature generated"+ myarray[simu-1]);
		    System.out.println("Total sum of all temperatures generated"+ sum);
		    System.out.println("Average for the season"+ average);
		    System.out.println("Highest temperature generated"+ myarray[0]);
	myarray[0]=myarray[0];		
	for(int i=0; i<simu; i++)
	{
		if(myarray[i]< myarray[0] )
		{	
			myarray[0]= myarray[i];	
		}		
	}
	 System.out.println("Lowest temperature generated"+ myarray[0]);		

		}
		else if (season==3)
		{
			System.out.println("How many simulation do you want?");
			simu=reader.nextInt();
			Random random =new Random();
			int myarray[] = new int[simu];
			//random generator
		for(int i =0; i<simu; i++){
		  myarray[i]=random.nextInt(21)+40;
		  System.out.println(" temperature generated"+ myarray[i]);     
		 }
		System.out.println("First temperature generated"+ myarray[0]);
		//finds the mean
		for(int i=0; i<simu; i++)
		{
		sum += myarray[i];	
		}
		average= sum/simu;
		//finds the highest temperature
		for(int i=0; i<simu; i++) 		{ 			if(myarray[i]> myarray[0] )
			{	
				myarray[0]= myarray[i];	
			}	
		}
		//find the lowest temperature
		    System.out.println("Last temperature generated"+ myarray[simu-1]);
		    System.out.println("Total sum of all temperatures generated"+ sum);
		    System.out.println("Average for the season"+ average);
		    System.out.println("Highest temperature generated"+ myarray[0]);
	myarray[0]=myarray[0];		
	for(int i=0; i<simu; i++)
	{
		if(myarray[i]< myarray[0] )
		{	
			myarray[0]= myarray[i];	
		}		
	}
	 System.out.println("Lowest temperature generated"+ myarray[0]);	
		}
		else if(season==4)
		{

			System.out.println("How many simulation do you want?");
			simu=reader.nextInt();
			Random random =new Random();
			int myarray[] = new int[simu];
			//random generator
		for(int i =0; i<simu; i++){
		  myarray[i]=random.nextInt(31)+40;
		  System.out.println(" temperature generated"+ myarray[i]);     
		 }
		System.out.println("First temperature generated"+ myarray[0]);
		//finds the mean
		for(int i=0; i<simu; i++)
		{
		sum += myarray[i];	
		}
		average= sum/simu;
		//finds the highest temperature
		for(int i=0; i<simu; i++) 		{ 			if(myarray[i]> myarray[0] )
			{	
				myarray[0]= myarray[i];	
			}	
		}
		//find the lowest temperature	  
		    System.out.println("Last temperature generated"+ myarray[simu-1]);
		    System.out.println("Total sum of all temperatures generated"+ sum);
		    System.out.println("Average for the season"+ average);
		    System.out.println("Highest temperature generated"+ myarray[0]);
	myarray[0]=myarray[0];		
	for(int i=0; i<simu; i++)
	{
		if(myarray[i]< myarray[0] )
		{	
			myarray[0]= myarray[i];	
		}		
	}
	 System.out.println("Lowest temperature generated"+ myarray[0]);	
	 myarray[0]=0;		
		}
		}
		while(season!=0);
	}
}

Screenshot
lab1image

Leave a Reply