Lab 2

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 several statistics just like lab 1 except the statistics are a little be different this time. The statistics will be the following: total count of number entered, total count of integer numbers entered, total count of floating points entered, first number, last number, highest number and lowest number, the sum of all numbers, and the average. It is doing pretty much the same as lab 1, but this one will have two class files. One kept all the methods for all statistic while other one use as an application to call those methods out and use it.

Code:

//Name: Guan Hong Chen
//Date: Oct. 07 2013
//CET 3640 Fall 2013
//Lab 2 (NumberStats.java)

public class NumberStats
{
	private int TotalCount = 0;
	private int IntCount = 0;
	private int FloatCount = 0;
	private float sum = 0;
	private float firstnum;
	private float lastnum;
	private float minimum = Float.MAX_VALUE;
	private float maximum = Float.MIN_VALUE;
	private float average = 0;

	public int getTotalCount()
	{
		return TotalCount;
	}
	public int getIntCount()
	{
		return IntCount;
	}
	public int getFloatCount()
	{
		return FloatCount;
	}
	public float getFirst_number()
	{
		return firstnum;
	}

	public float getLast_number()
	{
		return lastnum;
	}
	public float getSum()
	{
		return sum;
	}
	public float getMaximum()
	{
		return maximum;
	}
	public float getMinimum()
	{
		return minimum;
	}
	public float getAverage()
	{
		return average;
	}

	public void Statistic(int input)
	{
		TotalCount++;
		IntCount++;

		if(TotalCount == 1)
		{
			firstnum = input;
		}

		if(input>maximum)
		{
			maximum = input;
		}

		if(input<minimum) 		{ 			minimum = input; 		} 		 		sum = sum + input;	 		 		lastnum = input; 		 		average = sum/TotalCount; 	} 	 	public void Statistic(float input) 	{ 		TotalCount++; 		FloatCount++; 	 		if(TotalCount == 1) 		{ 			firstnum = input; 		} 	 		if(input>maximum)
		{
			maximum = input;
		}

		if(input<minimum)
		{
			minimum = input;
		}

		sum = sum + input;

		lastnum = input;

		average = sum/TotalCount;

	}

}
//Name: Guan Hong Chen
//Date: Oct. 07 2013
//CET 3640 Fall 2013
//Lab 2(NumberStatsApp.java)

import java.util.Scanner;

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

		System.out.print("Begin number statistics? (y/n): ");
		char 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);
		}

		System.out.print("For you convenience, how many numbers you want to enter? : ");
		int limited = input.nextInt();

		while(yn == 'y' || yn == 'Y' )
		{

			System.out.print("Enter a number: ");

			float input_number = input.nextFloat();

			if(input_number == (int)input_number)
			{
				NumberStats.Statistic((int)input_number);
			}
			else
			{
				NumberStats.Statistic(input_number);
			}

			if(NumberStats.getTotalCount()==limited)
			{
				System.out.print("Continue? (y/n): ");
				yn = input.next().charAt(0);
				if(yn == 'y' || yn == 'Y')
				{
					System.out.print("For you convenience, how many numbers you want to enter? : ");
					limited = input.nextInt();
				}
			}

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

		System.out.println("Total numbers entered: " + NumberStats.getTotalCount());
		System.out.println("Total integer numbers entered: " + NumberStats.getIntCount());
		System.out.println("Total floating point numbers entered: " + NumberStats.getFloatCount());
		System.out.println("The first number is: " + NumberStats.getFirst_number());
		System.out.println("The last number is: " + NumberStats.getLast_number());			
		System.out.println("The lowest number is: " + NumberStats.getMinimum());
		System.out.println("The highest number is: " + NumberStats.getMaximum());
		System.out.println("The total sum is: " + NumberStats.getSum());			
		System.out.println("The average number is: " + NumberStats.getAverage());	

	}
}

Screenshot:

Screen Shot 2013-10-07 at 3.21.20 PM

Leave a Reply

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