A City Tech OpenLab ePortfolio

LAB 1 – INTRO TO JAVA

LAB 1 – Intro to Java

Lab Description:

The purpose of this lab is to create ad temperature sensor simulator from our knowledge obtained in the lecture. The program will first display a menu to the user and ask what season would like to be simulated or if the program should exit. If a season is selected, then it asks the user the number of simulations that should be run. Each simulation will generate a random set of numbers on a range dependent on the season. For winter the numbers range from 20 – 40, for spring numbers range from 40 – 70, for summer numbers range from 70 – 90, and for fall numbers range from 40 – 60.

For each simulation, there are six things that need to be keep track off and displayed to the user. 1) First temperature generated, 2) Last temperature generated, 3) Lowest temperature generated, 4) Highest temperature generated, 5) Sum of all temperatures generated, and 6) Average for the season. After the results are displayed, the program asks the user if they would like to run the simulation again or quit.

Code:

import java.util.Scanner;

public class LAB1 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner scan = new Scanner(System.in);

		int Season;
		int counter;
		int maxValue = 0;
		int minValue = 0;
		int sumValue = 0;
		int aveValue = 0;

		int val = 1;
		while(val == 1)
		{	
		System.out.println("Select a season");
		System.out.println("1.Winter 2.Spring 3.Summer 4.Fall 5.Exit");

		Season = scan.nextInt();

		if( Season == 5)
		break;

		System.out.println("How many times you want to simulate?");
		counter = scan.nextInt();

		int temp[] = new int[counter];

			if (Season == 2)
			{
				int rand;
				for(int i = 0; i<counter; i++)
				{
					rand = 40 + (int)(Math.random() * ((70 - 40) + 1));
					temp[i] = rand;
					System.out.println(rand);
				}
					maxValue = temp[0];  
					for(int i=1;i < counter; i++)
 					{
   						if(temp[i] > maxValue)
						{  
							maxValue = temp[i];

						}

					}

					minValue = temp[0];  
					for(int i=1;i < counter; i++)
					{  
						if(temp[i] < minValue)
						{  
							minValue = temp[i];

						}

					}

					sumValue = temp[0];
					for(int i=1 ;i < counter; i++)
					{  
						sumValue = sumValue + temp[i];
					}

					aveValue = sumValue/counter;

			}

			else if (Season == 3)
			{
				int rand;
				for(int i = 0; i<counter; i++)
				{
					rand = 70 + (int)(Math.random() * ((90 - 70) + 1));
					temp[i] = rand;
					System.out.println(rand);
				}
					maxValue = temp[0];  
					for(int i=1;i < counter; i++)
 					{
   						if(temp[i] > maxValue)
						{  
							maxValue = temp[i];

						}

					}

					minValue = temp[0];  
					for(int i=1;i < counter; i++)
					{  
						if(temp[i] < minValue)
						{  
							minValue = temp[i];

						}

					}

					sumValue = temp[0];
					for(int i=1 ;i < counter; i++)
					{  
						sumValue = sumValue + temp[i];
					}

					aveValue = sumValue/counter;

			}

			else if (Season == 4)
			{
				int rand;
				for(int i = 0; i<counter; i++)
				{
					rand = 40 + (int)(Math.random() * ((60 - 40) + 1));
					temp[i] = rand;
					System.out.println(rand);
				}
					maxValue = temp[0];  
					for(int i=1;i < counter; i++)
 					{
   						if(temp[i] > maxValue)
						{  
							maxValue = temp[i];

						}

					}

					minValue = temp[0];  
					for(int i=1;i < counter; i++)
					{  
						if(temp[i] < minValue)
						{  
							minValue = temp[i];

						}

					}

					sumValue = temp[0];
					for(int i=1 ;i < counter; i++)
					{  
						sumValue = sumValue + temp[i];
					}

					aveValue = sumValue/counter;

			}

			else if (Season == 1)
			{
				int rand;
				for(int i = 0; i<counter; i++)
				{
					rand = 20 + (int)(Math.random() * ((40 - 20) + 1));
					temp[i] = rand;
					System.out.println(rand);
				}
					maxValue = temp[0];  
					for(int i=1;i < counter; i++)
 					{
   						if(temp[i] > maxValue)
						{  
							maxValue = temp[i];

						}

					}

					minValue = temp[0];  
					for(int i=1;i < counter; i++)
					{  
						if(temp[i] < minValue)
						{  
							minValue = temp[i];

						}

					}

					sumValue = temp[0];
					for(int i=1 ;i < counter; i++)
					{  
						sumValue = sumValue + temp[i];
					}

					aveValue = sumValue/counter;

			}

			System.out.println("First Temp Generated");
			System.out.println(temp[0]);
			System.out.println("Last Temp Generated");
			System.out.println(temp[counter-1]);
			System.out.println("Highest Temp Generated");
			System.out.println(maxValue);
			System.out.println("Lowest Temp Generated");
			System.out.println(minValue);
			System.out.println("Sum of Temp Generated");
			System.out.println(sumValue);
			System.out.println("Average of Temp Generated");
			System.out.println(aveValue);
			System.out.println("Would you like to run the simuluation again? (1 = Yes 0 = No)");
			val = scan.nextInt();
			System.out.println("");

		}

	}	
}

Screenshot:

pic

Leave a Reply

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