Lab 1

Description:

In Lab 1, a program was to be created that will prompt the user to enter numbers in order to compute several statistics. The user can enter as many numbers as he or she wants; could be negative or positive. The second lecture for course CET 3640 was an introduction on how to begin writing the most simplest line of code by using java. Furthermore, the lecture also introduces us to arithmetic application in java coding where variables are to be declared in order for a program to, for example, find the sum or average of numbers. Lab 1 required the output of 7 different solutions: (1) First number, (2) Second Number, (3) Count of number entered, (4)Lowest two numbers, (5)Highest two numbers, (6)Sum, and (7)Average. Each were to be solved for every number a user will enter. Variables were declared for each of these outputs. Repetition was taken care of with a do-while loop statement and each different solution was taken care of with if-else statements. Repetition was necessary in order for the user to enter as many numbers as he or she wanted. But more importantly, what was within the do-while loop is what calculated for the outputs. In other words, the code represents a similar function as of a calculator, except it is limited to only calculating 7 different solutions.

Code:

import java.util.Scanner;

public class ComputingStatistics {

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

		Scanner input = new Scanner(System.in);

		int count = 1;

		int number = 0;
		int numbers;
		int firstnum = 0;
		int sum = 0;
		int avg = 0;
		int choice;

		System.out.println("Please choose 1 to enter a number or 2 to exit: ");
		choice = input.nextInt();

		if (choice == 2)
			{
			System.exit(0);
			}

		System.out.println("Please enter the amount of numbers you will be entering: ");
		numbers = input.nextInt();

		System.out.println("Please enter your first number: ");
		int num1 = input.nextInt();

		int min1 = num1;
		int min2 = num1;

		int max1 = num1;
		int max2 = num1;

	do	

		if (choice == 1)

		{		

			System.out.println("Please enter your number: ");
			number = input.nextInt();

	// Minimum two numbers
		if (number <= min1) 
		{
            min2 = min1;
            min1 = number;
        } 

		else if (number < min2)  		 		{              			min2 = number;           		 		 		} 		 		else if (min1 == min2)    		 		{              			min2 = number; 		 	                 	   		 		} 		 	// Maximum two numbers 		 		 		if (number >= max1) 
		{
            max2 = max1;
            max1 = number;
        } 

		else if (number > max2) 
		{
            max2 = number;
        } 

		else if (max1 == max2)   
		{
            max2 = number;
		}

		// First Number
		if (count == 1)
		{
			firstnum = num1;
		}

		// Sum
			sum += number;

		// Average
			avg = sum / count;

		    count++;  

	}

	while (count < numbers);

		// sum
		sum = sum + num1;

		// Average
		avg = sum / count;

		System.out.println("The first number entered was: " + firstnum);
		System.out.println("The last number entered was: " + number);
		System.out.println("The count of numbers entered is: " + count);
		System.out.println("The two smallest numbers are " + min1 + " and " + min2);
		System.out.println("The two highest numbers are " + max2 + " and " + max1);
		System.out.println("The total sum of your numbers is: " + sum);
		System.out.println("The average of your numbers is: " + avg);

	}

}

Screenshots

lab1

lab1screenshot

Leave a Reply

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