Lab-2

Lab Description:

This lab is to create a class that ask users to type as many numbers as they want. This program will display Min,Max,Average, Last number entered ,First number entered,sum and total number entered. To do this program, I use while loop to get the user data. inside the while loop, i use couple if loops for display the min, max ,last number entered and last number entered. To display how many the total number entered i use counter, when while run once this counter will increase by one. To find sum and average is pretty easy, just use simple addition and division. To stop entering number is by typing zero. after users type in zero. this program will break out the while loop and display all the features.

Code:

 

package lab2;

package lab2;

import java.util.Scanner;

public class Display {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		double average = 0;
		int numberOfCount = 0;
		int sum = 0;
		int score =1;
		int max = 0;
		int min = 2147483647;
		int first = 0;
		int last = 0;

		while(score!=0)  {
			System.out.print("Enter Numbers: ");
			score = input.nextInt();
			if(score==0)break;
			if (score >= max) {
				max = score;
			}


			if (score <= min) {
				min = score;
			}

			numberOfCount++;

			if (numberOfCount == 1) {
				first = score;
			}

			
			if (numberOfCount > 0) {
				last = score;
			}
			

			sum = score + sum;
			average = sum / numberOfCount;
		}
			
 			
		 	System.out.println("Sum is:"+ sum);
			System.out.println("Average is:" + average);
			System.out.println("Count of number entered is: "+ numberOfCount);
			System.out.println("Max is:"+ max);
			System.out.println("Min is: "+ min);
			System.out.println("First Number entered is:" +first);
			System.out.println("Last Number entered is:"+ last);	
		}
			
		
	
}

Screenshots: