从构造方法调用方法-Java

时间:2019-07-14 13:27:52

标签: java constructor

我已经开始学习Java,并且需要一些家庭作业。

我正在编写一个程序,该程序可以计算油漆墙的成本,我需要从构造函数方法中调用某些方法。

我看过this文章,但并没有真正帮助。

import javax.swing.JOptionPane;

public class RenovationProjectManager { 

    public static void main(String[] args) {

        int selection;
        String tempInput;

        String menu = "Menu: (type 1 or 2 to continue)\n";
        menu += "1. Calculate paint required for a wall\n";
        menu += "2. Calculate paint required for project";
        tempInput = JOptionPane.showInputDialog(menu);

        while (tempInput != null) {
            selection = Integer.parseInt(tempInput);

            if (selection == 1) {

                RenovationProjectManager menuOption1;
                menuOption1 = new RenovationProjectManager();

            } else if (selection == 2) {

                RenovationProjectManager menuOption2;
                menuOption2 = new RenovationProjectManager();
                menuOption2.menuOption2Scenario();

            } else {
                JOptionPane.showMessageDialog(null, "Invalid choice!");
            }
            tempInput = JOptionPane.showInputDialog(menu);

        }

    }


    public RenovationProjectManager() {
        menuOption1Scenario();
    }

    public void menuOption1Scenario() {

        double wallArea = 0, cost, height, length, costPerSqm;
        String tempInput;

        costPerSqm = Double.parseDouble(JOptionPane.showInputDialog("Enter cost per sq.m ($)"));
        tempInput = JOptionPane.showInputDialog("Enter a wall name");
        height = Double.parseDouble(JOptionPane.showInputDialog("Enter " + tempInput + " wall height (m)"));
        length = Double.parseDouble(JOptionPane.showInputDialog("Enter " + tempInput + " wall length (m)"));

        wallArea = height * length;

        cost = wallArea * costPerSqm;

        JOptionPane.showMessageDialog(null,"Cost to paint " + tempInput + " wall of " + wallArea + " sq.m. is $" + cost);

    }


    public void menuOption2Scenario() {

        double wallArea = 0, cost, height, length, costPerSqm;
        String tempInput, wallNames;

        costPerSqm = Double.parseDouble(JOptionPane.showInputDialog("Enter cost per sq.m ($)"));
        wallNames = "";
        wallArea = 0;
        cost = 0;
        tempInput = JOptionPane.showInputDialog("Enter a wall name (cancel to finish)");
        while (tempInput != null) {

            height = Double.parseDouble(JOptionPane.showInputDialog("Enter " + tempInput + " wall height (m)"));
            length = Double.parseDouble(JOptionPane.showInputDialog("Enter " + tempInput + " wall length (m)"));

            wallArea += height * length;
            wallNames += tempInput + ", ";
            tempInput = JOptionPane.showInputDialog("Enter a wall name (cancel to finish)");
        }

            cost = wallArea * costPerSqm;

            JOptionPane.showMessageDialog(null,"Cost to paint " + wallNames + "wall(s) of " + wallArea + " sq.m. is $" + cost);
    }

}

当我尝试运行程序时,收到一条错误消息(输入12后),内容为:at RenovationProjectManager.<init>(RenovationProjectManager.java:63)

如果您想了解更多信息/背景,请询问;谢谢!

1 个答案:

答案 0 :(得分:1)

您要创建同一对象menuOption1的两个实例,一次是在main方法中,一次是在构造函数中。从main方法中删除以下行。您只需要使用构造函数来调用该方法。

RenovationProjectManager menuOption1 = new RenovationProjectManager();

此外,从构造函数中删除对象创建。您可以直接调用该方法。

在构造函数内部只需使用

menuOption1Scenario();