Lab 2

Lab Description:

In lab 2, we are creating a class NumberStats that will provide methods for the main function to call. Overall, this lab is very similar to lab 1, all we need to do is separate the file in lab 1 into two files and still make it working together.

Code:

File for class NumberStats:

//Create by Kawa Tsang
public class NumberStats {
    //set all the variables
    private double firstNum;
    private double lastNum;
    private static int countNum = 0;
    private static int countInt = 0;
    private static int countFloat = 0;  
    private double minNum = Double.POSITIVE_INFINITY;
    private double maxNum = Double.NEGATIVE_INFINITY;
    private static double sum;
    private double aveg;

    //create a constructor
    public NumberStats() {

    }
    //method call when the input is integer
    public void numberStats(int x){
        sum = sum + x;

        if (minNum > x){
            minNum = x;
        }

        if (maxNum < x){             maxNum = x;         }                    if (countNum == 0){              firstNum = x;         }                    countNum++;         countInt++;         lastNum = x;     }     //method call when the input is float     public void numberStats(double x){         sum = sum +x;                    if (minNum > x){
            minNum = x;
        }

        if (maxNum < x){
            maxNum = x;
        }

        if (countNum == 0){ 
            firstNum = x;
        }

        countNum++;
        countFloat++;
        lastNum = x;
    }

    // Below is all the get methods     
    public double getFirst(){
        return firstNum;
    }

    public double getLast(){
        return lastNum;
    }

    public int getCount(){
        return countNum;
    }

    public int getInt(){
        return countInt;
    }

    public int getFloat(){
        return countFloat;
    }

    public double getMin(){
        return minNum;
    }

    public double getMax(){
        return maxNum;
    }

    public double getSum(){
        return sum;
    }

    public double getAvg(){
        aveg = sum/countNum;
        return aveg;
    }

}

The main file:

 
//Create by Kawa Tsang
import java.util.Scanner;

public class Lab02 {

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

        int choice; 

        NumberStats number = new NumberStats();     //make a new object using other class
        Scanner input = new Scanner(System.in);

        System.out.println("Welcome to My program , Please enter your chioce, (1) to start number stats, (2) to exit."); //ask user what they want
        choice = input.nextInt();

        switch (choice) {
        case 1: {                      //user choose start the program
            boolean again = true;     
            while (again){            //loop the user to enter numbers until they want to stop

                int counter = number.getCount() + 1;        
                System.out.println("Please enter the " + counter + " number: "); //ask user the enter the n'th numbers
                double x;
                x = input.nextDouble();
                if (x == (int) x){             //check if the input is integer or float
                    number.numberStats((int)x);        //input is integer, call methods using integer
                }
                else{
                    number.numberStats(x);      //input is float, call methods using double
                }

                System.out.println("Coutinue entering?(Y to continue, any other key to stop entering number)"); //ask the user if they want to continue
                char in = input.next().charAt(0);

                if(in == 'y' || in == 'Y'){         //user want to continue
                    again = true;
                }
                else {                          //user want to exit
                    again = false;
                }
            }
            //Output all the number statics
            System.out.println("First number entered: " + number.getFirst());
            System.out.println("Last number entered: " + number.getLast());
            System.out.println("Count of number entered: " + number.getCount());
            System.out.println("Count of integer entered: " + number.getInt());
            System.out.println("Count of float number entered: " + number.getFloat());
            System.out.println("Minimum: " + number.getMin());
            System.out.println("Maximum: " + number.getMax());
            System.out.println("Total Sum: " + number.getSum());
            System.out.println("Average: " + number.getAvg());

            input.close();
            return;
        }

        case 2: {   //Exit
            input.close();
            return;
        }

        default: {  //unexpected input
            System.out.println("Invalid choice, Program exit");
            input.close();  
            return;
            }
        }

    }
}

 

Screenshot

lab2-1lab2-2

Leave a Reply

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