instructions wrote:A button to calculate total with shipping
A button to calculate total without shipping
A large textPane to display your results.
When the user presses one of the two calculate buttons, your program should display a table for the cost of 1 unit, 100 units, 200 units, up to 1000 units. It should have one column for the quantity, and one column for the cost. The cost should may or may not including shipping costs, depending on which button was clicked.
Lexi Turgeon wrote:Yes, I knew that part. Was just unsure of how do I code the two buttons for more than one unit?
Campbell Ritchie wrote:Why are you using applets, which nobody else uses?
I presume that you can add a listener to each button, and you can do that easily with a λ (Java8 only).
myButton.addActionListener(evt -> someMethodOrOther());
Now create that method, with void return type and (if in the same class) private access. If you need to update two components, put both of them into the method.
At least that is how you would do it in Swing.
You still have the problem about what you are returning in that method which calculates the prices.
Campbell Ritchie wrote:Did you have any success with the listeners? Have you sorted out the method that calculates prices?
Henry Wong wrote:
From the fact that you never showed us the GUI setup code, and your code has a "do not modify" section, I am going to speculate that this question isn't a GUI question, as your instructor already did all the GUI the work.
I am going to speculate that the question is how to implement the button listener in your code? Is it the implementation of the button listener whose outline has already been created, but is just empty?
Is that what you are asking?
Henry Wong wrote:... then it should be straightforward. The button callbacks just need to set some sort of state (probably via a variable) that "with" or "without" has been pressed.
The tedious part is the rest of the applet. It has to be modified to behave correctly based on this saved state.
Lexi Turgeon wrote:
What do you mean via a variable? Do I have to add a new variable to my method? How would I do that?
But the no‑arguments calculateCost method still looks incorrect to me. How much will you charge for a computer with an extra keyboard (do you really charge $30?) and a mouse?
Lexi Turgeon wrote:. . . Yes, this is my updated code. . . .
Campbell Ritchie wrote:But the no‑arguments calculateCost method still looks incorrect to me. How much will you charge for a computer with an extra keyboard (do you really charge $30?) and a mouse?
The calculateCost method with arguments uses == true. That is not only bad style, but also very error‑prone. Every now and again we see somebody write = instead of ==.
Not if (b == true) ...
if (b) ...
Not if (b == false) ...
if (!b) ...
No. I have told you a circumstance where you shouldn't use == at all.
Lexi Turgeon wrote:. . . Should I be using == instead of +=?
Liar, liar, pants on fire! refreshing plug:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koophttps://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton