In this lab, we learn how to separate out one program into two, and get the same result as one single file does.
import java.util.Scanner; public class TrainStats { public static void main(String[] args) { // TODO Auto-generated method stub NumberStats mygrade = new NumberStats(); Scanner input = new Scanner(System.in); double number; do{ System.out.println("Please enter your number(0 to end entering): "); number = input.nextDouble(); if (number == 0) break; mygrade.setNewNumber(number); } while (true); System.out.println("Max is :" + mygrade.getMax()); System.out.println("Min is :" + mygrade.getMin()); System.out.println("First Number is:" + mygrade.getFirstNumber()); System.out.println("Last Number is :" + mygrade.getLastNumber()); System.out.println("Sum is:" + mygrade.getSum()); System.out.println("Average is: " + mygrade.getAverage()); System.out.println("Nummber OF Count is : " + mygrade.getNumberCount()); ================================================================================== public class NumberStats { private double firstNum; private double lastNum; private double min; private double max; private double sum; private double average; private int numberCount=0; public void setNewNumber(double number) { numberCount++; if (numberCount==1){ firstNum=number; lastNum=number; min=number; max=number; } else{ if(min > number){ min = number; } if(max < number){ max = number; } } lastNum=number; sum+=number; average=sum/numberCount; } public void setNewnumber(int number){ setNewNumber((double)number); } public double getFirstNumber(){ return firstNum; } public double getLastNumber(){ return lastNum; } public double getNumberCount(){ return numberCount; } public double getMin(){ return min; } public double getMax(){ return max; } public double getSum(){ return sum; } public double getAverage(){ return average; } }