Lab 1

In this first lab, a program was written to calculate various parameters from an input of numbers. This included the sum, average, count of inputted numbers, the highest and lowest values entered, and the first and last numbers entered.

Although mostly straightforward and like C, there were a few lingering quirks that needed to be figured out:

  • Java handles integers, floats, and doubles in completely different ways, such that you have to explicitly cast variables [(int), (float), (double)] to convert to and fro, which made inputs slightly tricky to deal with. This was ameliorated with the use of type-checking methods in the Scanner class, such as Scanner.hasNextInt().
  • In C, when comparing the lowest number to the current input, if the variables being compared were uninitialized, you would get an error during run time. In Java, this doesn’t happen since all variables are initialized to zero. The problem then is that the lowest number is not tracked accurately, especially if all inputs were greater than zero.
  • The same issue popped up if all inputs were less than zero or negative, as the highest number would then just default to zero. Both problems were solved by assigning the highest and lowest numbers to the very first input.
  • If no numbers were input, then the standard output was suppressed and the program just quits.
/* 
    CET 3640 
    Lab 1 
    Eric Tung 
    9/17/2013 
*/

import java.util.Scanner; 

public class Lab_1 
{ 
    public static void main(String args[]) 
    { 
        Scanner scan = new Scanner(System.in); 

        boolean isInteger = true; 
        float first = 0; 
        float number = 0; 
        float[] highNumber = new float[2]; 
        float[] lowNumber = new float[2]; 
        float count = 0; 
        float sum = 0; 

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

        do
        { 
            System.out.println("\nEnter a number: "); 

            isInteger = scan.hasNextFloat(); 
            if(!isInteger) 
            { 
                System.out.println(" "); 
                continue; 
            } 

            number = scan.nextFloat(); 

            sum += number; 
            count++; 

            if(count == 1) 
            { 
                first = number; 
            } 

            if(number >= highNumber[0] || (count <= lowNumber.length)) 
            { 
                highNumber[1] = highNumber[0]; 
                highNumber[0] = number; 
            } 

            if((number <= lowNumber[0]) || (count <= lowNumber.length)) 
            { 
                lowNumber[1] = lowNumber[0]; 
                lowNumber[0] = number; 
            } 

        }while(isInteger); 

        if(count != 0) 
        { 
            System.out.println("The first number entered was: "+ first); 
            System.out.println("The last number entered was: "+ number); 
            System.out.println("The total amount of numbers entered was: "+ count); 
            System.out.println("The highest two values inputted are: "+ highNumber[0] +" and "+ highNumber[1]); 
            System.out.println("The lowest two values inputted are: "+ lowNumber[0] +" and "+ lowNumber[1]); 
            System.out.println("The sum of all inputted numbers is: "+ sum); 
            System.out.println("The average of all inputted numbers is: "+ sum/count); 
        } 

        scan.close(); 

        System.out.println("\nEnd of Program."); 

        System.exit(1); 
    } 
}

Leave a Reply

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