Lab 2

In the second lab, we took all the code from last week’s lab and stuffed them into a separate class file. Each variable was made private, so as to keep them from being tampered with unintentionally by other parts of the code. These attributes were made accessible by a multitude of well-organized setter and getter methods, and for the most part, that was all there was to it.

A couple of quirks:

  • Extra methods had to be written to check the data type being inputted. This is no different from the last lab, but now after the data types are checked, different methods aare invoked according to their respective data type [setNumType(int), setNumType(float)]. those methods are private.
/* 
    CET 3640 
    Lab 2 - NumberStats 
    Eric Tung 
    9/27/2013 
*/

public class Lab_2 
{ 
    public static void main(String[] args) 
    {
        // Create a new NumberStats Object.
        NumberStats stats = new NumberStats(); 

        System.out.println("Welcome.");
        System.out.println("Input a non-numerical character to quit at any time."); 

        /* Loop checks for an integer or float (in method stats.CheckNumber). If
        neither, the loop breaks and has NumberStats calculate the results. */
        do
        { 
            System.out.println("\nEnter a number: "); 

            if(stats.CheckNumber() == false) 
            { 
                continue; 
            } 

            stats.SetNumber(); 
            stats.SetSum(); 

            if(stats.GetCount() == 1) 
            { 
                stats.SetFirst(); 
            } 

            /* Since all integers and floats are initialized to 0, if we enter all 
            numbers higher than zero, lowestNumber will never actually reflect the
            lowest number. So we set the first number as the lowestNumber, and now
            it behaves correctly.*/
            if((stats.GetCount() == 1) || (stats.GetHighest() < stats.GetNumber())) 
            { 
                stats.SetLowest(); 
            } 

        }while(stats.CheckNumber()); 

        if(stats.GetCount() != 0) 
        { 
            System.out.print("The first number entered was: ");
            System.out.println(stats.GetFirst());
            System.out.print("The last number entered was: ");
            System.out.println(stats.GetNumber());
            System.out.print("The total amount of numbers entered was: ");
            System.out.println(stats.GetCount());
            System.out.print("The total amount of integers entered was: ");
            System.out.println(stats.GetIntCount());
            System.out.print("The total amount of floating-point numbers entered was: ");
            System.out.println(stats.GetFloatCount());
            System.out.print("The highest value inputted is: ");
            System.out.println(stats.GetHighest());
            System.out.print("The lowest value inputted is: ");
            System.out.println(stats.GetLowest());
            System.out.print("The sum of all inputted numbers is: ");
            System.out.println(stats.GetSum());
            System.out.print("The average of all inputted numbers is: ");
            System.out.println(stats.GetAverage()); 
        } 
    } 
}
import java.util.Scanner; 

public class NumberStats 
{ 
    private boolean isInteger, isFloat; 
    int integer; 
    float floating; 
    private float first; 
    private float number; 
    private float highNumber, lowNumber; 
    private float sum; 
    private int count, intCount, floatCount; 
    Scanner scan = new Scanner(System.in); 

    public NumberStats() 
    { 
        isInteger = false; 
        isFloat = false; 
        first = 0; 
        sum = 0; 
        count = 0; 
        intCount = 0; 
        floatCount = 0; 
    } 

    // Checks for a float or int.
    public boolean CheckNumber() 
    { 
        isInteger = scan.hasNextInt(); 
        isFloat = scan.hasNextFloat(); 

        boolean check; 
        check = (isInteger || isFloat); 

        return check; 
    } 

    public void SetNumber() 
    { 
        if(isInteger) 
        { 
            number = SetNumType(integer); 
            count++; 
        } 

        else if(isFloat) 
        { 
            number = SetNumType(floating); 
            count++; 
        } 
    } 

    private int SetNumType(int anInt) 
    { 
        integer = scan.nextInt(); 
        intCount++; 
        return integer; 
    } 

    private float SetNumType(float aFloat) 
    { 
        floating = scan.nextFloat(); 
        floatCount++; 
        return floating; 
    } 

    public float GetNumber() 
    { 
        return number; 
    } 

    public void SetSum() 
    { 
        sum = sum + GetNumber(); 
    } 

    public float GetSum() 
    { 
        return sum; 
    } 

    public int GetCount() 
    { 
        return count; 
    } 

    public int GetIntCount() 
    { 
        return intCount; 
    } 

    public int GetFloatCount() 
    { 
        return floatCount; 
    } 

    public void SetFirst() 
    { 
        first = number; 
    } 

    public float GetFirst() 
    { 
        return first; 
    } 

    public void SetHighest() 
    { 
        highNumber = GetNumber(); 
    } 

    public float GetHighest() 
    { 
        return highNumber; 
    } 

    public void SetLowest() 
    { 
        lowNumber = GetNumber(); 
    } 

    public float GetLowest() 
    { 
        return lowNumber; 
    } 

    public float GetAverage() 
    { 
        return sum/count; 
    } 
}

Leave a Reply

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