Yep!

Lab 2

Lab 2

Lab 2 procedures:

In this lab you will create a class NumberStats that will provide several methods to compute the following statistics:

  1. First number entered
  2. Last number entered
  3. Count of number entered
  4. Count of integers
  5. Count of floating points
  6. Minimum
  7. Maximum
  8.  Total Sum
  9.  Average

The methods should be overloaded to support floating point numbers as well as integers. The class should have one setter method that will accept a new number, and getter methods for each one of the statistics.

You will also create an application that will test all the methods in the NumberStats class. This application will instantiate an object of NumberStats and display a menu similar to Lab #1 that will ask the user to (1) enter a number or (2) exit. The user should be able to enter as many numbers as wished (positives or negatives). When the user decides to exit, the different statistics should be displayed using the appropriate methods.

Overview:

this Lab asked us to repeat lab 1, but instead use multiple classes to run each operations. I was able to do this with out any large issues.

Code:

 

package Lab2;

public class NumberStats {
	@SuppressWarnings("unused")
	private float newNumber = 0;
	private int count = 0;
	private float firstNum;
	private float lastNum;
	private int floatcount;
	private int intcount;
	private float minNum;
	private float maxNum;
	private float sum;
	private float average;

	public void setNewNumber(float newNumber) {
		this.newNumber = newNumber;

		int x = (int) Math.ceil(newNumber);
		int y = (int) Math.floor(newNumber);

		sum = sum + newNumber;

		if(count == 0)
		{
			firstNum = newNumber;
			minNum = newNumber;
			maxNum = newNumber;
		}

		if(newNumber < x && newNumber > y)
		{
			floatcount++;
		}
		else
		{
			intcount++;
		}
		if (newNumber < minNum) 		{ 			minNum = newNumber; 		} 		if (newNumber > maxNum)
		{
			maxNum = newNumber;
		}

		lastNum = newNumber;

		count++;
		average = sum/count;
	}

	public float getFirstNum()
	{
		return this.firstNum;
	}
	public float getLastNum()
	{
		return this.lastNum;
	}
	public int getCount()
	{
		return this.count;
	}
	public int getFloatCount()
	{
		return this.floatcount;
	}
	public float getSumNum()
	{
		return this.sum;
	}
	public float getAverage()
	{
		return this.average;
	}
	public float getMaxNumber(){
		return this.maxNum;
	}
	public float getMinNumber(){
		return this.minNum;
	}
	public int getIntCount()
	{
		return this.intcount;
	}

}

package Lab2;

import java.util.Scanner;

public class NumberStatsApp 
{
	public static void main(String[] args) 
	{

		@SuppressWarnings("resource")
		Scanner input = new Scanner(System.in);

		NumberStats myNumberStats = new NumberStats();

		int choice = 0;
		float number = 0;

		do 
		{
			System.out.println("What would you like to do next? \n(1) Enter a number \n(2) Exit");
			choice = input.nextInt();

			if(choice == 1)
			{
				System.out.println("Enter a number either decimal or whole #:");
				number = input.nextFloat();

				myNumberStats.setNewNumber(number);
			}

			else if(choice == 2)
				choice = 'x';

		} while (choice != 'x');

		System.out.printf("The first number entered was, %f and ", myNumberStats.getFirstNum());
		System.out.printf("the last number entered was, %f.\n", myNumberStats.getLastNum());
		System.out.printf("The number of enteries was, %d of which ", myNumberStats.getCount());
		System.out.printf("%d of the entered numbers were whole numbers and\n ", myNumberStats.getIntCount());
		System.out.printf("%d of the entered numbers where decimal numbers \n", myNumberStats.getFloatCount());
		System.out.printf("The maximum value entered was %f and ", myNumberStats.getMaxNumber());
		System.out.printf("the minimum vlaue entered was %f\n", myNumberStats.getMinNumber());
		System.out.printf("Your total sum is %f\n", myNumberStats.getSumNum());
		System.out.printf("Your average was %f\n", myNumberStats.getAverage());
		}
}

Screenshots:

Lab2

 

Leave a Reply

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