Lab Report 2

Lab Description:

This time, the program will be a step up from the previous lab. The idea is similar, except this time there will be a class with all the methods, which will be called and executed through the main class. The program will once again have a menu of 2 choices, to enter a number or to exit, which will bring them to the statistics.

The statistics will have to cover these key points of the entered numbers:

  • First number entered
  • Last number entered
  • Count of number entered
  • Count of integers
  • Count of floating points
  • Minimum
  • Maximum
  • Total Sum
  • Average

 

Code:

NumberStats.java

 package Lab2;

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 (newNumber<temp && newNumber>temp1) {
            floatCount++;
        }
        else{
            intCount++;
        }

        if (newNumber < min) {
            min = newNumber;
        }

        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 Lab2;
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 : %f\n", myNumberStats.getFirstNumber());
        System.out.printf("Last Number : %f\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 : %f\n", myNumberStats.getMax());
        System.out.printf("Minumum : %f\n", myNumberStats.getMin());
        System.out.printf("Total Sum : %f\n", myNumberStats.getSum());
        System.out.printf("Average : %f\n", myNumberStats.getAverage());
    }

}

 

Screenshots:

Lab2

Leave a Reply

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