【问题标题】:How do I change Procedural Programming into OOP..? (JAVA)如何将过程编程更改为 OOP ..? (JAVA)
【发布时间】:2018-05-29 02:17:39
【问题描述】:

我是一名初学 Java 程序员,并且为此辛勤工作了很长一段时间。我需要将下面的程序转换为 OOP 格式,并且无法正确编译。我想我会发布工作的非格式化程序,而不是我失败和不稳定的尝试。如果有人可以将以下程序转换为 OOP,将不胜感激。由于我是新手,请原谅任何效率低下或马虎。

感谢您的帮助:)

导入 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);

}

}

【问题讨论】:

  • JavaScript != Java,而 jQuery 是一个 JavaScript 库,而不是 Java。
  • idownvotedbecau.se/noattempt - 这个问题读起来很像一个家庭作业问题,你声称你已经尝试了很多没有用的东西似乎有点可疑,因为你没有提供任何细节。不管怎样,这个问题对于这个网站来说太宽泛了,你需要问一些具体的问题。人们不是来为你写代码的。
  • 你把事情搞反了:一定要发布一个更具体的问题。否则,这个问题看起来更像是一个过于宽泛的工作指令,而不是一个有效的问题。
  • 真的不清楚你在问什么。 OOP 是一种频谱。有些程序比其他程序更面向对象。你的不是很面向对象。但是没有“将程序转换为 OOP”之类的东西。

标签: java arrays oop


【解决方案1】:

我受到这个问题的启发,写了一篇漫画project。我试图使代码尽可能面向对象,但留在“领域”中,不要大量使用设计模式、框架等。此外,我尝试遵循测试优先的思想。你可以查看commits history,看看工作是如何一步一步进行的。

感谢 javacoder 提供的经验和乐趣。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    • 2019-02-16
    • 1970-01-01
    • 2019-03-14
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多