Lab Report 2 – Description
For lab 2, we created a class called numberstats. We included several methods that computes statistics such as First number, Last number, Count of numbers, Count of numbers, Count of floating numbers, the Minimum, the Maximum, the Total Sum, and the Average. I created an overloaded method within the class that supports floating numbers and integers. The class also contains a setter and getter method. The setter method accepts a new number, and the getter methods are for each one of the statistics that is computed. I created an application class called numbstatstest that tests all the methods in numberstats class. The application instantiate the object of sumberstats and displays a menu that asks the user to (1) enter a number or (2) exit. The user enters as many numbers as they wish (positives or negatives), and can exit at any time they wish.
Code:
package LAB_2; import java.lang.Math; public class numberstat { String welcome; double numbrs; double lstnumbr; double frstnumbr; double maximum; double minimum; double sum; double countdecValue = 0; int count = 0; int countup = 0; int countdown = 0; int countintValue = 0; public numberstat(String numbs) { welcome = numbs; } public String getwelcome() { return welcome; } public void msgdisplay() { System.out.printf("Welcome to Washington Sarmiento's Second Program\n%s!\n\n", getwelcome()); } public void setnumberstatnums(double numbrs) { this.numbrs = numbrs; countup = (int) Math.ceil(numbrs); countdown = (int) Math.floor(numbrs); lstnumbr = numbrs; sum += numbrs; count++; if (numbrs < countup && numbrs > countdown) { countdecValue++; } else { countintValue++; } if (numbrs > maximum) { maximum = numbrs; } if (numbrs < minimum) { minimum = numbrs; } if ( count == 1 ) { frstnumbr = numbrs; maximum = numbrs; minimum = numbrs; } } public double getlstnumbr() { return this.lstnumbr; } public double getcountdecValue() { return this.countdecValue; } public int getcountintValue() { return this.countintValue; } public double getfrstnumbr() { return this.frstnumbr; } public double getmaximum() { return this.maximum; } public double getminimum() { return this.minimum; } public double getsum() { return this.sum; } public double getcount() { return this.count; } void count(double a) { a = getcount(); System.out.printf("TOTAL NUMBERS INSERTED: %f\n", a); } double count(double b, double c) { b = getcountintValue(); System.out.printf("TOTAL WHOLE NUMBERS: %f\n", b); c = getcountdecValue(); System.out.printf("TOTAL DECIMAL NUMBERS: %f\n", c); return 0; } public void displayrslts () { System.out.printf("FIRST NUMBER: %f\n",getfrstnumbr()); System.out.printf("LAST NUMBER: %f\n",getlstnumbr()); System.out.printf("MINIMUM NUMBER: %f\n", getminimum()); System.out.printf("MAXIMUM NUMBER: %f\n", getmaximum()); System.out.printf("TOTAL SUM: %f\n", getsum()); System.out.printf("AVERAGE: %f\n", getsum()/getcount()); } } package LAB_2; import java.util.Scanner; public class numbstatstest { public static void main(String[] args){ numberstat usernumberstats = new numberstat("CET3640f2013 LAB 2: Classes and Methods"); usernumberstats.msgdisplay(); float num; Scanner switchy = new Scanner( System.in ); Scanner numbstats = new Scanner( System.in ); System.out.print("Enter '1' to run the program or '0' to exit: \n"); int choice =switchy.nextInt(); while(choice != 0) { System.out.println("Enter a number:\n"); double numbers = numbstats.nextDouble(); usernumberstats.setnumberstatnums(numbers); System.out.println("Press 0 to exit program or 1 to continue entering numbers:\n1 will not be part of the group of numbers"); int numbr = numbstats.nextInt(); if ( numbr == 0 ) { System.out.println("\nLife is better LIVED as a U.S. Marine! \n"); usernumberstats.displayrslts(); usernumberstats.count(numbers); usernumberstats.count(numbers, numbers); break; } } if ( choice == 0 ) { System.out.println("Life is better LIVED as a U.S. Marine! \n"); } } }