Lab
Description:
In this lab, our class have to write a program that computes the following:
First number, Last Number, Total number entered, Total count of Integers/ floats, Min/Max, Sum/Avg.
This lab is similar to the first lab that we did. The difference between both of the lab is that in this lab we are using overloading to support floating numbers as well as integers.
……………………………………………………………………………………………………….
package Labs;
public class NumberStats {
private float newNumber = 0;
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){
this.newNumber = 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;
}
}
......................................................................................................................
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());
}