I've started learning Java and I have some homework that I need help with.
I'm making a program where it calculates the cost to paint walls and I need to call some methods from a constructor method.
I've looked at this article but it didn't really help.
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);
}
}
When I tried running the program I got an error message (after I entered 1
or 2
) that read: at RenovationProjectManager.<init>(RenovationProjectManager.java:63)
.
If you would like some more information/background just ask; thanks!
You’re creating two instances of the same object menuOption1
, once in the main
method and once in the constructor. Remove the following line from the main
method. You only need to use your constructor to call the method.
RenovationProjectManager menuOption1 = new RenovationProjectManager();
Also, remove the object creation from the constructor. You can call the method directly.
Inside your constructor just use
menuOption1Scenario();