A City Tech OpenLab ePortfolio

Lab 1

Lab Description:

In this lab, me and German Calle(lab partner) are asked to create a temperature sensor simulator that will simulate the temperature in 4 kind of season which are Winter, Spring, Summer, and Fall. First the program should ask the user to select the season they would like to simulate. After that, the program should prompt and ask the user how many times they would like the program to simulate the selected season temperature for them. Beside that, for each simulation, the program needs to keep track of what it generated such as the first temperature generated, last temperature generated, lowest temperature generated, highest temperature generated, total sum of all the temperature generated and average of the temperate in that season.

Code:


import java.util.Scanner;

public class Simulation {

	private static Scanner scan;

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

		scan = new Scanner(System.in);

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

		int val = 1;
		while(val == 1)
		{	
			System.out.println("Please select a season to simulate");
			System.out.println("1.Winter 2.Spring 3.Summer 4.Fall");

			Season = scan.nextInt();

			while (true)
			{
				if (Season > 4 | Season < 1)
				{
					System.out.println("Invalid input. Pelase select your season again.");
					System.out.println("1.Winter 2.Spring 3.Summer 4.Fall");
					Season = scan.nextInt();
				}
				else 
				{
					break;
				}
			}

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

			int temp[] = new int[counter];

			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  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];
				}

				avgValue = sumValue/counter;

			}

			else 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  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];
				}

				avgValue = 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  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];
				}

				avgValue = 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  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  1)
				{
					System.out.println("Would you like to run the simulation again? (1 = Yes 0 = No)");
					val = scan.nextInt();
				}
				else 
				{ 
					break;
				}
			}

		}
	}	
}

Screenshot:
Lab 1

Leave a Reply

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