Lab #3

LAB DESCRIPTION:

In this experiment  i separated the program into  different classes with fields and methods. i created a class  named NumberStats that will compute several statistics  for the first number, last number, count, maximum, minimum, total and average from the data that was entered. I created another class called TrainStats that will  instantiate the NumberStats class.

CODE:

public class NumberStats {

	private int num=1 ;
	private int first_num=0;
	private int last_num=0;
	private int maximum=0;
	private int minimum=0;
	private int total=0;
	private int average=0;
	private int count=0;

	public void setNumber(int num){

		if (num == 0){
			System.out.println(" zero was entered the input sequence has ended !");

		}

		if(count == 0){
			first_num = num;
		}

		if(count > 0){
			last_num = num;
		}

		count++;

		if(num < minimum){ 			minimum = num; 		} 		if(num > maximum){
			maximum = num;
		}

		total = num + total;
		average = total / count;

	}

	public int getnum(){
		return num;
	}

	public int getfirst_num(){
		return first_num;

	}

	public int getlast_num(){
		return last_num;

	}

	public int getcount(){
		return count;

	}

	public int getmaximum(){
		return maximum;

	}

	public int getminimum(){
		return minimum;

	}

	public int gettotal(){
		return total;

	}

	public int getaverage(){
		return average;

	}

}
import java.util.Scanner;
public class TrainStats {

	/**
	 * @param args
	 */
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		NumberStats myNumber = new NumberStats();	

		int num;

		Scanner input = new Scanner(System.in);

		do{
			System.out.println("Enter numbers !"); 

			num = input.nextInt();
			myNumber.setNumber(num);

		} while ( num != 0);

				System.out.println("First number is:"+myNumber.getfirst_num());
				System.out.println("Last number is:"+myNumber.getlast_num());
				System.out.println("The count of numbers is:"+myNumber.getcount());
				System.out.println("The maximum number is:"+myNumber.getmaximum());
				System.out.println("The minimum number is:"+myNumber.getminimum());
				System.out.println("The total is:"+myNumber.gettotal());
				System.out.println("The average is:"+myNumber.getaverage());

		}

}

Screenshots: