Lab 2

In this lab we create a java program to test all methods in the NumberStats class. We used Scanner in this code which allowed the number to be either positive or negative. for the second part of the lab we used the constructor to allow the program to receive the declare object within the private in order to set and get methods. in all this lab was a bit difficult at certain times but with the some help was able to figure it out.

package Lab2;

public class NumberStats
{

private double firstnum;
private double lastnum;
private double minium;
private double maxium;
private double sum;
private double avgerage;

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

public NumberStats()
{
firstnum = 0;
lastnum = 0;
minium = 0;
maxium = 0;
sum = 0;
intnum = 0;
floatnum = 0;
countnum = 0;
}

public void setNumberStats(double newnumber, int count)
{

this.lastnum = newnumber;
sum += newnumber;

if (count == 1) {

minium = newnumber;
maxium = newnumber;
firstnum = newnumber;
}

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

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

this.avgerage = 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 minium;
}

public double getmax() {
return maxium;
}

public double getavg() {
return avgerage;
}

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 Lab2;

import java.util.Scanner;
public class NumberStatsTest {

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

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

{

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

do
{

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

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

break;
}

count++;
}
while(true);

}

}
}

Lab2

Leave a Reply

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