2017-12-14 7 views
-5

私は初心者のJavaプログラマであり、かなり長い間このことを頑張ってきました。私は以下のプログラムをOOP形式に変換する必要があり、エラーなくコンパイルすることはできません。私は、私の失敗した、不安定な試みではなく、動作していない形式のプログラムを投稿すると考えました。誰かが下のプログラムをOOPに変換できるなら、非常に感謝しています。私がこれに慣れていないので、非効率や汚れを許してください。 :)手続き型プログラミングをOOP ..に変更する方法を教えてください。 (JAVA)

インポートjava.util.Scannerを支援するための

おかげ。 パブリッククラスEstimatePi {

//Public static variables used because they are used throughout the different methods 
public static Scanner in = new Scanner(System.in); 

//2 * Math.random - 1 is used to guarentee that the max value is gonna be 1 and min is gonna be -1 
public static double x = (2 * Math.random() - 1); 
public static double y = (2 * Math.random() - 1); 
public static double radius = 1.0; 
public static double numOnBoard; 
public static double totalPi; 
public static int numThrows; 
public static int trials; 

public static int hits(double x, double y, int trials) { 
    numOnBoard = 0; 

    for (int i = 1; i < trials; i++) { 

     //Same Algorithm as above 
     x = (2 * Math.random() - 1); 
     y = (2 * Math.random() - 1); 

     //If x2 + y2 <= r2 then its a hit on the board. 
     if ((Math.pow(x, 2) + Math.pow(y, 2)) <= (Math.pow(radius, 2))) { 
      numOnBoard++; 
     } 

    } 

    //returns the num of hits on the board 
    return (int)numOnBoard; 
} 

//Method to calculate pi, and store that data in an array 
public static double[] piColumn(double numOnBoard, double numThrows) 
{ double []piColumn = new double[trials]; 
    for(int i = 0; i < piColumn.length;i++) 
    { 
     //Formula to calculate the pi 
     piColumn[i] = (4 * (numOnBoard)/numThrows); 
    } 
    return piColumn; 
} 

public static void main (String [ ] args) 
{ 

    //The number of darts thrown per trial is asked 
    System.out.println("How many times should the dart be thrown per trial?"); 
    numThrows = in.nextInt();  
    System.out.println(); 

    //The number of trials is asked 
    System.out.println("How many trials do you want to simulate?"); 
    trials = in.nextInt(); 
    System.out.println(); 

    //forloop to iterate the internal code while counter < trials 
    for (int counter = 0; counter < trials; counter++) { 

     //number of hits methods is declared as a integer 
     int hits = hits(x,y,numThrows); 

     //the calculation of pi is declared as a double 
     double []estimatedPi = piColumn(hits,numThrows); 

     //total = total + the estimatation of pi 
     for(int i = 0; i < trials; i++){ 
      totalPi += estimatedPi[i]; 
     } 

     //Formatting the output 
     System.out.printf("%s %d %s %s", "Trial [",(counter + 1),"]", ": pi = "); 
     System.out.printf("%1.5f\n",estimatedPi[counter]); 
    } 

    //The average pi is the total pi's divided by the number of trials the user enters 
    double averagePi = (totalPi/trials/trials); 

    System.out.printf("%s %1.5f\n", "Estimation of pi = ",averagePi); 

} 

}

+1

JavaScript!= Java、jQueryはJavaではなくJavaScriptライブラリです。 – rgettman

+3

http://idownvotedbecau.se/noattempt/ - この質問は宿題に関する質問とよく似ています。あなたが何か詳細を提示していないので、うまくいかないことをたくさん試したことが疑問に思えるようです。いずれにしても、この質問はこのサイトでは広すぎます。具体的な質問をする必要があります。人々はあなたのためにコードを書くためにここにいません。 – DaveyDaveDave

+0

あなたは物事を後ろに持っています:もっと具体的な質問で試行を投稿してください。それ以外の場合、質問は有効な質問よりも広範な作業指示のようになります。 –

答えて

0

私は質問に触発さと漫画projectを書きました。私は可能な限りオブジェクト指向のコードを作ろうとしましたが、 "ドメイン"にとどまり、デザインパターン、フレームワークなどを大量に使用しませんでした。また、テストイデオロギーに従いました。 commits historyを表示して、作業がステップごとにどのように進んでいるかを確認できます。

javacoderさん、ありがとうございました。

関連する問題