Lab 2

Description:
Based on the lecture, Chapter 3 introduced us to a bit more of what java is about as its classes, objects, methods and strings; Chapter 6 went a bit further into methods. Using this valuable information, each individual had to create a java program that will compute several statistics. Unlike Lab 1, we had to create two different classes for two different purposes; one, called NumberStats, that provides several methods to compute the several statistics and the other, the client of NumberStats, as an application. Thia application instantiates an object of NumberStats and displays a menu that will prompt the user to (1) enter a number or (2) exit the program. More importantly, when the user exits the program the different statistics will be displayed by using the appropriate methods.

Code:

package edu.cuny.citytech.CET3640.f13.Lab2;

public class NumberStats 
{

	private double firstnum;
	private double lastnum;
	private double min;
	private double max;
	private double sum;
	private double avg;

	private double countnum;
	private double intnum;
	private double floatnum;
	private double FloorValue;
	private double CeilingValue;

	public NumberStats() 
	{
		firstnum = 0;
		lastnum = 0;
		min = 0;
		max = 0;
		sum = 0;
	        intnum = 0;
	        floatnum = 0;
	        countnum = 0;
	}

	public void setNumberStats(double newnumber, int count)
	{

		this.lastnum = newnumber;
		sum += newnumber;

		if (count == 1) {

		        min = newnumber;
                        max = newnumber;
			firstnum = newnumber;
		}

		if (min >= newnumber) {
			min = newnumber;
		}

		if (max <= newnumber) {
			max = newnumber;
		}

		this.avg = sum/count;
		this.countnum = count;

		this.FloorValue = Math.floor(newnumber);
		this.CeilingValue = Math.ceil(newnumber);

	if (FloorValue <= newnumber && CeilingValue <= newnumber )
	{
		intnum++;
	}

	else
	{
		floatnum++;
	}

}
	public double getfirstnum() {
		return firstnum;
	}

	public double getlastnum() {
		return lastnum;
	}

	public double getmin() {
		return min;
	}

	public double getmax() {
		return max;
	}

	public double getavg() {
		return avg;
	}

	public double getcountnum(){
		return countnum;
	}

	public double getintnum(){
		return intnum;
	}

	public double getfloatnum(){
		return floatnum;
	}
	public double getFloorValue(){
		return FloorValue;
	}

	public double getCeilingValue(){
		return CeilingValue;
	}

	public void displayStatistics()
	{

		System.out.println("The First number entered is: " + getfirstnum());
		System.out.println("The Last number entered is: " + getlastnum());
		System.out.println("The count of numbers entered is: " + getcountnum());
		System.out.println("The count of integers is: " + getintnum());
		System.out.println("The count of floating points is: " + getfloatnum());
		System.out.println("The Minimum number entered is: " + getmin());
		System.out.println("The Maximum number entered is: " + getmax());
		System.out.println("The Average number is: " + getavg());

	}
}
package edu.cuny.citytech.CET3640.f13.Lab2;

import java.util.Scanner;
public class NumberStatsTest {

	public static int count = 1;
	private static Scanner scanner;

	public static void main(String[] args)
	{

		NumberStats StatsTest = new NumberStats();
		scanner = new Scanner(System.in);

	do 
	{

	        System.out.println("Please enter a number, to exit the computations enter 'Exit':");
		StatsTest.setNumberStats(scanner.nextDouble(), count);

		if(scanner.hasNext("Exit"))
		{	
			System.out.println("Exiting the computations, displaying final statistics: ");
			StatsTest.displayStatistics();
			break;
		}

                if(scanner.hasNext("exit"))
                { 
                        System.out.println("Exiting the computations, displaying final statistics: ");
                        StatsTest.displayStatistics();
                        break;
                }
		        count++;
		}
		while(true);

	}
}

Screenshots:

Lab2

Leave a Reply

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