This Java program should be two separate files

Hello everyone I am taking a Java class and this is my first assignment involving object oriented programming. I am getting problems with the SimpleCalc part and I am wondering if my work should be two separate files or if I am missing a component that allows the StatCalc part and the SimpleCalc part to talk to each other. Please keep in mind that I'm new to Java, so I might need to write a little more of this than I've seen stack by thread in the past at times, however, I would appreciate any help so thanks in advance. Here is my code:

package tutorial; 
/*   
* An object of class StatCalc can be used to compute several simple statistics  
* for a set of numbers.  Numbers are entered into the dataset using  
* the enter(double) method.  Methods are provided to return the following  
* statistics for the set of numbers that have been entered: The number  
* of items, the sum of the items, the average, the standard deviation,  
* the maximum, and the minimum.  
*/ public class StatCalc {

           private int count;   // Number of numbers that have been entered.
           private double sum;  // The sum of all the items that have been entered.
           private double squareSum;  // The sum of the squares of all the items.        
           private double max = Double.NEGATIVE_INFINITY;                               private double min = Double.POSITIVE_INFINITY;
           /**
            * Add a number to the dataset.  The statistics will be computed for all
            * the numbers that have been added to the dataset using this method.
            */
           public void enter(double num) {
                  count++;
                  sum += num;
                  squareSum += num*num;               
                  if (count == 1){
                      max = num;
                      min = num;
                  }
                  else {
                      if (num > max)
                          max = num;
                      if (num < min)
                          min = num;
                  }     
           }
           /**

            * Return the number of items that have been entered into the dataset.

            */

           public int getCount() {

                  return count;
           }
           /**

            * Return the sum of all the numbers that have been entered.

            */

           public double getSum() {

                  return sum;
           }         
           /**

            * Return the average of all the items that have been entered.

            * The return value is Double.NaN if no numbers have been entered.

           */

           public double getMean() {

                  return sum / count;
           }     

           /**

            * Return the standard deviation of all the items that have been entered.

            * The return value is Double.NaN if no numbers have been entered.

            */

           public double getStandardDeviation() {

                  double mean = getMean();

                  return Math.sqrt( squareSum/count - mean*mean );
           }

           public double getMin(){
               return min;
           }
           public double getMax(){
               return max;
           }        }// end class StatCalc

public class SimpleCalc {
    public static void main(String[]args){
        Scanner in = new Scanner(System.in);
        SimpleCalc calc;
        calc = new SimpleCalc();

        double item;
        System.out.println("Enter numbers here. Enter 0 to stop.");       
        System.out.println();

        do{ 
            System.out.print("? ");
            item = in.nextDouble();

            if (item != 0)
                calc.enter(item);
        }while (item != 0);

        System.out.println("\nStatistics about your calc:\n");   
        System.out.println(Count: "+calc.getCount"()); 
        System.out.println(Sum: "+calc.getSum"());   
        System.out.println("Minimum: "+calc.getMin());  
        System.out.println("Maximum: "+calc.getMax());  
        System.out.println("Average: "+calc.getMean());     
        System.out.println("Standard Deviation: "+calc.getStandardDeviation());     
    }// end main
}//end SimpleCalc

      

+3


source to share


2 answers


Yes, it needs two files.



The Java contract is that each public "top-level" class requires its own file.

0


source


In Java, a public class must be in a file with the same name as the class. Since you have a public class named StatCalc, then the filename should be StatCalc.java. Likewise, the second class is also public, so it must be in its own file.



+1


source







All Articles