LAB 1 – Intro to Java

Lab Report 1 – Description

For LAB1 we need to create a program that will prompt the user to enter numbers to compute several statistics. We need to display a menu that will ask the user to (1) enter a number or (2) exit. The user should be able to enter as many numbers as wished (positives or negatives). When the user finishes the following statistics should be displayed:

First number entered, Last number entered, Count of number entered, Lowest two numbers, Highest two numbers, Total Sum, and Average.

I created a class which contains setMethods, getMethods, constructors,  and functions.  Later, I created another class containing public static void main where i create an object where I pass the name to constructor.  Once this is done i am able to call on my functions and just display the results of my functions.

 

Code:

 

/*First class*/
package LAB_1;

import java.util.Scanner;
/*Created a class for constructors, setmethods, getmethods, and such so I can call upon them in my main program*/
public class INPUTS {
	String welcome;
	int numbers = 0;
	int frstnum = 0;
	int lstnum = 0; 
	int lownum = 0; 
	int highnum = 0;
	int scdhighnum = 0;
	int scdlownum = 0;
	int count = 0;
	int sum = 0;

	public INPUTS(String Program)
	{
		welcome = Program;
	}

	public void setWELCOME(String Program)
	{
		welcome = Program;
	}

	public String getWELCOME()
	{
		return welcome;
	}

	public void MSGDISPLAY()
	{
		System.out.printf("Welcome to Washington Sarmiento's First Program\n%s!\n\n", getWELCOME());
	}

	public void INPUTNUMS()
	{

		Scanner switchy = new Scanner( System.in );
		Scanner INPUT = new Scanner( System.in );
		Scanner brk = new Scanner( System.in );
		System.out.print("Enter '1' to run the program or '2' to exit: \n");
		int choice =switchy.nextInt();

		while(choice != 2)
		{
			System.out.println("Enter 0 to continue ");
			System.out.println("To terminate program enter n/N after these statements:");
			//(n/ or N) char will terminate program after last number is entered
			String brk1 = brk.next();
			char brk2 = brk1.charAt(0);
			System.out.println("Enter a  set of numbers:\n");
			numbers = INPUT.nextInt();
			lstnum = numbers;
			sum += numbers;
			count++;

			if (numbers > highnum)
			{
				scdhighnum = highnum;
				highnum = numbers;
			} 

			else if (numbers > scdhighnum) 
			{
				scdhighnum = numbers;
			}

			if (numbers < lownum)
			{
				scdlownum = lownum;
				lownum = numbers;
			} 

			else if (numbers < scdlownum) 
			{
				scdlownum = numbers;
			}

			if ( count == 1 )
			{
				frstnum = numbers;
				highnum = numbers;
				lownum = numbers;
			}

			if ( brk2 == 'n' || brk2 == 'N' )
				//if(numbers == 0)
			{

				break;
			}
		}

	}

	public void DISPLAYRSLTS ()
	{
		System.out.println("FIRST NUMBER: " +frstnum);
		System.out.println("LAST NUMBER: " +lstnum);
		System.out.println("COUNT: "+count);
		System.out.println("LOWEST NUMBER: " +lownum);
		System.out.println("SECOND LOWEST NUMBER: " +scdlownum);
		System.out.println("HIGHEST NUMBER: " +highnum);
		System.out.println("SECOND HIGHEST NUMBER: " +scdhighnum);
		System.out.println("TOTAL SUM: " +sum);
		System.out.println("AVERAGE: " +sum/count);

	}
}

/*Second class*/
package LAB_1;
/*this class contains main which will run the program I and will call the setmethods, getmethods and others from inputs class*/
public class INPUTSTEST {

	public static void main(String[] args) {
		INPUTS userINPUTS = new INPUTS("CET3640f2013 LAB 1: Introduction to Java Programming");

		userINPUTS.MSGDISPLAY();
		userINPUTS.INPUTNUMS();
		userINPUTS.DISPLAYRSLTS();

	}

}

}

Screenshots:

LAB1_3640

Leave a Reply

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