Revised_Lab_2

Lab Description:
In this lab, the students have to write a program that is very similar to Lab1. The only difference between lab 1 and lab 2 is that we have to find the total number of floats and integers that a user will input and also writing an application that will check all the methods.
……………………………………………………………………………………………………….
NumberStats.java :

package Labs;

public class NumberStats {
	
	private int count = 0;
	private float firstNumber;
	private float lastNumber;
	private int floatCount = 0;
	private int intCount = 0;
	private float min;
	private float max;
	private float sum;
	private float average;
	
	public void setNewNumber(float newNumber){
		int temp = (int) Math.ceil(newNumber);
		int temp1 = (int) Math.floor(newNumber);
		
		sum += newNumber;
		
		if(count == 0){
			firstNumber = newNumber;
			min = newNumber;
			max = newNumber;
		}
		
		if (newNumbertemp1) {
			floatCount++;
		}
		else{
			intCount++;
		}
		
		if (newNumber  max) {
			max = newNumber;
		}
		
		lastNumber = newNumber;
		
		count++;
	}
	
	public float getFirstNumber(){
		return this.firstNumber;
	}
	
	public float getLastNumber(){
		return this.lastNumber;
	}
	
	public int getCount(){
		return this.count;
	}
	
	public float getSum(){
		return this.sum;
	}
	
	public float getAverage(){
		average = sum / count;
		return this.average;
	}
	
	public float getMax(){
		return this.max;
	}
	
	public float getMin(){
		return this.min;
	}
	
	public int getFloatCount(){
		return this.floatCount;
	}
	
	public int getIntCount(){
		return this.intCount;
	}
}

……………………………………………………………………………………………………….
NumberStatsApp.java :

package Labs;
import java.util.Scanner;

public class NumberStatsApp {

	public static void main(String[] args) {

		Scanner input = new Scanner(System.in);

		NumberStats myNumberStats = new NumberStats();

		int choice = 0;
		float number = 0;

		do {
			System.out.println("(1) Enter a number");
			System.out.println("(2) Exit");
			choice = input.nextInt();

			if(choice == 1){
				System.out.println("Enter a number:");
				number = input.nextFloat();

				myNumberStats.setNewNumber(number);
				}

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

		} while (choice != 'x');

		System.out.printf("First Number : %.2f\n", myNumberStats.getFirstNumber());
		System.out.printf("Last Number : %.2f\n", myNumberStats.getLastNumber());
		System.out.printf("Count of Numbers : %d\n", myNumberStats.getCount());
		System.out.printf("Count of Integers : %d\n", myNumberStats.getIntCount());
		System.out.printf("Count of Floating points : %d\n", myNumberStats.getFloatCount());
		System.out.printf("Maximum : %.2f\n", myNumberStats.getMax());
		System.out.printf("Minumum : %.2f\n", myNumberStats.getMin());
		System.out.printf("Total Sum : %.2f\n", myNumberStats.getSum());
		System.out.printf("Average : %.2f\n", myNumberStats.getAverage());
	}

}

……………………………………………………………………………………………………….
OUTPUT Example :
lab2_1

Leave a Reply

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