Lab 1

Lab description:

This program is to allow user to input as much number as the user wants. When the user finish, the program will display the following statistics: count of number entered, first number, last number, highest number and lowest number, second highest number and lowest number, the sum of all numbers, and the average. There will two while loops in the program. The first one is to make sure only input y/Y or n/N is accepted to begin or terminate the program. Therefore, the program will not crash if user type any letter other than y/Y or n/N by mistake. Second loop is to have user input the number and do all the calculation while the user is able to terminate the program after each number entered. 

Code:

//Name: Guan Hong Chen
//Date: Sep. 30 2013
//CET 3640 Fall 2013

import java.util.Scanner;

public class NumberStatistics 
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);

		char yn;
		int count=0;
		double inputnum;
		double firstnum=0;
		double lastnum=0;
		double highestnum = Double.MIN_VALUE;
		double highernum = Double.MIN_VALUE;
		double lowestnum = Double.MAX_VALUE;
		double lowernum = Double.MAX_VALUE;
		double sum=0;
		double average;

		System.out.print("Begin number statistics? (y/n): ");
		yn = input.next().charAt(0);

		while(yn != 'y' && yn != 'Y')
		{	
			if(yn == 'n' || yn == 'N')
			{
				System.out.print("The program is now terminated!!!");
				System.exit(0);
			}
			System.out.print("Invalid input! Please enter yes(y) or no(n): ");
			yn = input.next().charAt(0);
		}

		while(yn == 'y' || yn == 'Y' )
		{
			count++;

			System.out.printf("Enter a number: ");
			inputnum = input.nextDouble();

			if(count == 1)
			{
				firstnum = inputnum;
			}

			if(inputnum>highestnum&&inputnum>highernum)
			{
				highernum = highestnum;
				highestnum = inputnum;
			}
			else if(inputnum<highestnum&&inputnum>highernum)
			{
				highernum = inputnum;
			}

			if(inputnum<lowestnum&&inputnum<lowernum) 			{ 				lowernum = lowestnum; 				lowestnum = inputnum; 			} 			else if(inputnum>lowestnum&&inputnum<lowernum)
			{
				lowernum = inputnum;
			}

			sum = sum + inputnum;

			System.out.print("Continue? (y/n): ");
			yn = input.next().charAt(0);

			if(yn == 'n' || yn == 'N')
			{
				lastnum = inputnum;
				break;
			}
			else if(yn != 'y' && yn != 'Y')
			{
				System.out.print("Invalid input! Please enter yes(y) or no(n): ");
				yn = input.next().charAt(0);
			}

		}

		average = sum/count;

		System.out.printf("\nTotal numbers entered: %d", count);
		System.out.printf("\nThe first number is: %f", firstnum);
		System.out.printf("\nThe last number is: %f", lastnum);		
		System.out.printf("\nThe lowest number is: %f", lowestnum);
		System.out.printf("\nThe second lowest number is: %f", lowernum);
		System.out.printf("\nThe highest number is: %f", highestnum);
		System.out.printf("\nThe second highest number is: %f", highernum);
		System.out.printf("\nThe total sum is: %f", sum);			
		System.out.printf("\nThe average number is: %f", average);			

	}

}

Screenshots:

Screen Shot 2013-09-30 at 2.26.34 PM

 

Leave a Reply

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