• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Output a double value with JOPtionPane

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

dicecca112

Member
Joined
Feb 25, 2004
Location
MA, USA
I want this code

Code:
    public void setInvoiceAmount(double Amount)
    {
                
        if (getQuantity() < 0)
        {
            quantity = 0;
        }
        if (getPricePerItem() < 0)
        {
            pricePerItem = 0;
        }
        
        Amount = (getQuantity() * getPricePerItem()); 
        
    }

    public double getInvoiceAmount()//method to print out InvoiceAmount
    {
        
        return Amount;
        
    }
to output a GUI window like this code does


Code:
    public void setPricePerItem(int item)//set method for Price Per Item
    {
    
         String newItem =
            JOptionPane.showInputDialog("What is the price?");
         
             int pricePerItem = Integer.parseInt( newItem);
        
            item = pricePerItem;
    }

    public double getPricePerItem()//get method for Price Per Item
    {
        return pricePerItem;
    }
 
It's java.

This code is all odd. Why do you need this:

Code:
    public double getPricePerItem()//get method for Price Per Item
    {
        return pricePerItem;
    }

When you call it in a location in which the pricePerItem seems to be accessible. Same as quantity.
 
Knacko said:
It's proper OO programming.

I know what OO programming is. Please read a bit further into my statement next time, before making a condescending remark. I am saying its odd in the sense that he is making calls to variables in a function which he is accessing those same variables in. And, the variables themselves do not appear to be local, because they are not defined in that function, or (as far as can be seen) in the same class.
 
its how the teacher wanted it done. Irregardless she didn't care that the function with the double didn't output a JOptionPane Window. And thanks for that link Knacko
 
dicecca112 said:
its how the teacher wanted it done. Irregardless she didn't care that the function with the double didn't output a JOptionPane Window. And thanks for that link Knacko

The function with the double itself would not output a JOptionPane, as it is only returning a value, which is a double. That double in itself being a value called from some other far reaches of the code.
 
Midnight Dream said:
The function with the double itself would not output a JOptionPane, as it is only returning a value, which is a double. That double in itself being a value called from some other far reaches of the code.

Or in dicecca's case, called from a setter method of the same class.
BTW, don't be mean; the programming section is a safe zone for noobs and experts alike.
 
Midnight Dream said:
I know what OO programming is. Please read a bit further into my statement next time, before making a condescending remark. I am saying its odd in the sense that he is making calls to variables in a function which he is accessing those same variables in. And, the variables themselves do not appear to be local, because they are not defined in that function, or (as far as can be seen) in the same class.

I did not mean for the remark to sound condescending, and I apologize that it did.

In OO programming (which is meant for medium to large scale projects), you want your code to be reliable and maintainable. I agree that if there is a single class, there is no reason to write the code that way. But if the code were to include many classes, it is essential. Suppose more code was added in calculating the pricePerItem (taxes, sales, etc). You'd have to update the entire code, replacing the variable call with a call to the method (that you'd have to create) With the updated pricing instructions.

The idea is that all variables will stay private, while the methods are public. Writing the code like this will save huge re-writes when the code expands. Teaching it this way will better prepare one for real programming.
 
I would just convert it to a string to put it in the pane.

Code:
String output = Double.toString(pricePerItem);
 
Midnight Dream said:
I know what OO programming is. Please read a bit further into my statement next time, before making a condescending remark. I am saying its odd in the sense that he is making calls to variables in a function which he is accessing those same variables in. And, the variables themselves do not appear to be local, because they are not defined in that function, or (as far as can be seen) in the same class.

I dont see how he is condesending in any way:confused: . You only write a get'er(or a get method) for private data. I would assume "pricePerItem" is private data thus its very formal to code such a function and usualy the case for encapsulation purposes. :beer:

Immortal_Hero said:
I would just convert it to a string to put it in the pane.

Code:
String output = Double.toString(pricePerItem);
o_O well i guess that would work, but wouldnt it just be better to output it to a text field? instead of having to write an extra line of code?:rolleyes:

usually something like this is discussed/assigned when you get to awt or atleast i did <_< Keep things simple, but if your curious look up the java API reference and lookup if "JOptionPane.showOutputDialog();" exists - that'd be my guess.

EDIT: found it! zOMG i still knoes some java! (i havent done java for a bit :( ) showMessageDialog
 
Last edited:
You wonldn't even have to assign it to a String object you could just convert it to a string in the code for the JOptionPane as you created the pane.
 
Immortal_Hero said:
I would just convert it to a string to put it in the pane.

Code:
String output = Double.toString(pricePerItem);


Immortal_Hero said:
You wonldn't even have to assign it to a String object you could just convert it to a string in the code for the JOptionPane as you created the pane.

eh? Am I missing something? I think your contradicting yourself: :eh?:
Code:
String output = Double.toString(pricePerItem);

This converts pricePerItem to a string and assigns it to output and yet you say you dont have to - clear contradiction to the highlighted in red. In purple, its not required to convert it to a string if you wanna put it in a pane
 
I am saying just do the Double.toString(XXX) in the code for the JOptionPane.

Code:
JOptionPane.showMessageDialog(null, "alert", Double.toString(pricePerItem), JOptionPane.ERROR_MESSAGE);

Does this clear up what I was saying? Where the Double.toString(... is where you normally would put a message like "Hi I am Immortal_Hero!" I was saying you would not have to make a string then output the string like below:

Code:
String output = Double.toString(pricePerItem);
JOptionPane.showMessageDialog(null, "alert", output, JOptionPane.ERROR_MESSAGE);
 
Back