Lab 2

Description
The purpose of this lab is to create class NumberStats that will provide methods to compute the statistics such as first and last number entered, total number entered, number of integers, number of floating points, minimum and maximum number, the total sum and the average.
The method must be overloaded to support floating point numbers, and integers. The setter and getter methods are used to accept new numbers, and for each of the stats. We also created an application that will test all the methods in the NumberStats class. The program should able to accept positive and negative numbers, and the application should instantiate object of numberStats and display menu like previous lab.


Code

package lab2;

import java.util.Scanner;
public class lab2 {

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

		System.out.print("Enter any amount of numbers: "+"\n");
		System.out.println("press 0 to exit");

		NumberStats myNumber = new NumberStats();
		float newNumber = 0.0f;
		Scanner input = new Scanner(System.in);

		while(true) {



			newNumber = input.nextFloat();
			if 
			(newNumber== 0)

				break;

			myNumber.setNewNumber(newNumber);

		}
		System.out.println("First: " + myNumber.getFirst());
		System.out.println("Last: " + myNumber.getLast());
		System.out.println("Minimum: " + myNumber.getMinimum());
		System.out.println("Maximum: " + myNumber.getMaximum());
		System.out.println("Average: " + myNumber.getAverage());
		System.out.println("Total Sum: " + myNumber.getTotal());
		System.out.println("Total integers: " + myNumber.getInt());
		System.out.println("Total floating points: " + myNumber.getFloat());
		System.out.println("Total number entered:" + myNumber.getCount());
		input.close();
	}

}
package lab2;

public class NumberStats {
	private float first;
	private float last ;
	private float minimum;
	private float maximum;
	private float average;
	private float total;
	private int count;
	private float sum;
	private int floatCount;
	private int intCount;
	private float min2;

	public NumberStats(){
		this.count = 0;
		this.sum = 0;
	}

	public void setNewNumber(float newNumber){

		count++;
		sum += newNumber;
		average = sum/count; 

		if(count == 1){

			maximum = newNumber;
			first = newNumber;
		}
		if(newNumber >= 0.0){
			last = newNumber;
		}
		if(newNumber > maximum){
			maximum = newNumber;
		}

		if ((newNumber < minimum) && (newNumber != 0)){
			minimum = newNumber;
			min2 = minimum;
		}
		else if (newNumber < min2) {
			minimum = newNumber;
		}

		total = newNumber + total;

		if (newNumber == Math.round(newNumber)) {
			intCount++;
		} 
		else {
			floatCount++;
		}
	}

	public int getFloat(){
		return floatCount;
	}
	public int getInt(){
		return intCount;
	}
	public float getFirst(){
		return first;
	}
	public float getLast(){
		return last;
	}
	public float getMinimum(){
		return minimum;
	}
	public float getMaximum(){
		return maximum;
	}
	public float getAverage(){
		return average;
	}
	public int getCount(){
		return count;
	}
	public float getTotal() {
		return total;
	}
}

Screenshots
1

2

3

4