2015-05-14 54 views
3

大家好,我正在學習一個Java類,這是我的第一個涉及面向對象編程的任務。我遇到了SimpleCalc部分的問題,並且想知道我的工作應該是兩個單獨的文件還是缺少允許StatCalc部分和SimpleCalc部分彼此交談的組件。請記住,我是Java的新手,所以我可能需要這個更清楚一點,然後我在過去有時會看到堆棧溢出,但是,我會很感激任何幫助,所以請提前致謝。這裏是我的代碼:這個Java程序是否需要兩個單獨的文件

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 
+0

對不起,我將在未來記住這一點。 – DeviousPenguin

回答

0

是的,它需要兩個文件。

Java的合同是每個「頂級」公共類都需要它自己的文件。

+0

但像'Rectangle2D.Double'這樣的東西有內部公共類... – MadProgrammer

+0

這就是「頂級」類和內部類之間的區別。內部類不遵循相同的規則! –

+0

是的,但'StatCalc'可能是'SimpleCalc'的內部類。我認爲你需要澄清「頂級」。而最後一次我檢查,你可以用'public static void main'封裝私有類,它仍然可以工作... – MadProgrammer

1

在Java中,公共類必須位於與該類名稱相同的文件中。所以既然你有一個名爲StatCalc的公共類,那麼文件名必須是StatCalc.java。同樣,第二類也是公開的,因此它必須在它自己的文件中。

+0

但像'Rectangle2D.Double'這樣的東西有內部的'公共'類... – MadProgrammer

+0

啊,這是完全有道理的。所以我嘗試了兩個單獨的文件,但在網上看到我會使用掃描儀爲了讓這兩個人能夠識別對方,並且當我按照步驟來看時,我仍然無法解決問題。讓兩個文件互相看到的正確方法是什麼?我不確定我的老師是否曾經正確地解釋它,也不知道這兩個文件是否可以放在同一個軟件包,相同的src等文件中。 – DeviousPenguin

+0

@MadProgrammer讓OP不會太挑剔,因爲OP似乎正在爲某些基本語法而掙扎。你引用的例子涉及嵌套的靜態類。雖然絕對正確,但我認爲這對這個問題沒有幫助。 –

相關問題