Revised_Lab_1

Lab Description:
In this lab we are writing a program that will tell the user many things, such as, the 2 highest number and the lowest two numbers that the user had inputted, the user could also see the very first number and the very last number that he/she had inputted, the user could also see the total sum of all the numbers, and finally the average of all the numbers. The objective of this lab is to get use to the format of java and getting to know to know the basics.
…………………………………………………………………………………………………………
Code :

import java.util.Scanner;

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

    	boolean isInteger = true;	
    	float first = 0;
    	float number = 0;
    	float[] high = new float[2];
    	float[] low = new float[2];
    	float count = 0;
    	float sum = 0;
    	int Number = 0;

        System.out.println("Enter 1 to run:");
        System.out.println("Enter 2 to quit");
        Number = scan.nextInt();
        switch (Number) {

        case 1:

        	System.out.println("Welcome.\nInput a charcter 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) // this represents the first number that you have entered
    		{
    			first = number;
    		}

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

    		if((number <= low[0]) || (count <= low.length))
    		{
    			low[1] = low[0];
    			low[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: "+ high[0] +" and "+ high[1]);
    		System.out.println("The lowest two values inputted are: "+ low[0] +" and "+ low[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.exit(1);

		break;

       case 2:       
           System.out.println("You have quitted the program");
           break;

        default: System.out.println( "Invaid Choice");
        break;
        }}}

………………………………………………………………………………………………………
OUTPUT :
lab1_1

Leave a Reply

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