LAB 3

Objective:

The objective of this lab is to learn how to break down a program into classes, and methods and get more familiar with the concept of overloading, and object instantiation. This lab assumes student is familiar with a text editor and the Java compiler or is using an IDE such as Eclipse. It also assumes that the student completed the previous labs.

Lab Analysis and Implementation:

For this laboratory experiment, we had to add some components to the first experiment which will allow the user to calculate some extra statistics.  For instance in this lab experiment, we had to used douuble in order to get decimals numbers. Moreover the main focus of this lab was to learn how to use th over loading methods, which we had to split the program into two classes and combine them in the same program.  We also had to use the do loop, if loop and the while loop to get the program to work properly. finally at the end, the program should be able to display the final statistics .

 

Source Code 1

public class NumberStats {
	private double first;
	private double last;
	private double minimum;
	private double maximum;
	private double average;
	private int count;
	private double sum;

	public NumberStats(){
		this.count = 0;
		this.sum = 0;
	}

	public void setNewNumber(double newNumber){
		last = newNumber;
		count++;
		sum += newNumber;
		average = sum/count; 
		if(count == 1){
			minimum = newNumber;
			maximum = newNumber;
			first = newNumber;
		}
		if(newNumber < minimum){ 			minimum = newNumber; 		} 		if(newNumber > maximum){
			maximum = newNumber;
		}

		}
	public double getFirst(){
		return first;
	}
	public double getLast(){
		return last;
	}
	public double getMinimum(){
		return minimum;
	}
	public double getMaximum(){
		return maximum;
	}
	public double getAverage(){
		return average;
	}
	public int getCount(){
		return count;
	}
	public double getSum(){
		return sum;
	}
	}
 SOURCE CODE 2
import java.util.Scanner;

public class NumberStat2 {

	public static void main(String[] args) {
		System.out.println("Please Enter 5 numbers Or -1 to quit");
		NumberStats myNumber = new NumberStats();
		double newNumber = 0.0;
		do {
			@SuppressWarnings("resource")
			Scanner input = new Scanner(System.in);
			newNumber = input.nextDouble();
			myNumber.setNewNumber(newNumber);
			if(myNumber.getCount()%5 == 0){
				System.out.println("First: " + myNumber.getFirst());
				System.out.println("Last: " + myNumber.getLast());
				System.out.println("Minimum: " + myNumber.getMinimum());
				System.out.println("Maximum: " + myNumber.getMaximum());
				System.out.println("Count : " + myNumber.getCount());
				System.out.println("Average: " + myNumber.getAverage());
				System.out.println("Total: " + myNumber.getSum());		

			}
		}
		while(newNumber != -1);

		System.out.println("First: " + myNumber.getFirst());
		System.out.println("Last: " + myNumber.getLast());
		System.out.println("Minimum: " + myNumber.getMinimum());
		System.out.println("Maximum: " + myNumber.getMaximum());
		System.out.println("Count : " + myNumber.getCount());
		System.out.println("Average: " + myNumber.getAverage());
		System.out.println("Total: " + myNumber.getSum());	

	}
}