Page 1 of 2 12 Last
  • Jump to page:
    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    12
    Rep Power
    0

    Need help with a class that can be called to do math


    I am supposed write a class that has 3 attributes: description, unitsOnHand, and price. The main method is supposed to create 3 retailItem objects and print total inventory values to a dialogue box. It is printing the text labels fine but it isn't returning any values. Any advice on what I am doing wrong?

    class:
    Code:
    public class RetailItem
    {
    	//private itemName for this RetailItem object
    	private String description;	
    	private int unitsOnHand;
    	private double price;
    	private double total;
    	String toString;
    
    	//default constructor
    	public RetailItem()
    	{
    	setDescription ( description );
    	setUnitsOnHand ( unitsOnHand );
    	setPrice ( price );
    	}
    
    	//non default constructor
    	public RetailItem( String description, int unitsOnHand, double price )
    	{
    	setDescription ( description );
    	setUnitsOnHand ( unitsOnHand );
    	setPrice ( price );
    	}
    		
    
    	
    
    	//sets and gets
    	public void setDescription ( String description )	
    	{
    		description = description;
    	}
    	
    	public void setUnitsOnHand ( int UnitsOnHand )	
    	{
    		unitsOnHand = unitsOnHand;
    	}
    
    	public void setPrice ( double price )	
    	{
    		price = price;
    	}
    	
    	public String getDescription()
    	{
    		return description;
    	}
    
    	public int getUnitsOnHand()
    	{
    		return unitsOnHand;
    	}
    
    	public double getPrice()
    	{
    		return price;
    	}
    
    	
    	//toString
    	public String toString()
    	{
    		return ( "%d" + (price * unitsOnHand) );
    	}
    
    }
    main method:

    Code:
    import javax.swing.JOptionPane;
    
    public class RetailItemTest
    {
    
    	public static void main( String args[] )
    	{
    
    	//create items
    	RetailItem jacket = new RetailItem( "Jacket", 12, 59.95 );
    	RetailItem designerJeans = new RetailItem( "Designer Jeans", 40, 34.95 );
    	RetailItem shirt = new RetailItem( "Shirt", 20, 24.95 );
    	double jacketTotal;
    	double designerJeansTotal;
    	double shirtTotal;
    	double grandTotal;	
    	String message;
    
    	//calculations
    	jacketTotal = jacket.getUnitsOnHand() * jacket.getPrice();
    	designerJeansTotal = designerJeans.getUnitsOnHand() * designerJeans.getPrice();
    	shirtTotal = shirt.getUnitsOnHand() * shirt.getPrice();
    	
    
    	//calculate grand total
    	grandTotal = jacketTotal + designerJeansTotal + shirtTotal;
    
    
    	//create message
    	message = String.format( "Jacket \t $", jacket.toString );
    	message = message + String.format( "\nDesigner Jeans \t $", designerJeans.toString );
    	message = message + String.format( "\nShirt \t $", shirt.toString );
    	message = message + String.format( "\nGrand Total: \t $", grandTotal );
    	
    
    	//print totals
    	JOptionPane.showMessageDialog(null, message );
    
    
    
    	}
    }
    Thanks!
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    isn't returning any values.
    What method are you talking about? Please post the program's output and explain what is wrong with it.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    12
    Rep Power
    0
    Originally Posted by NormR
    What method are you talking about? Please post the program's output and explain what is wrong with it.
    It's supposed to return a dialogue box that says:

    Jacket $(dollar value)
    Designer Jeans $(dollar value)
    Shirts $(dollar value)
    Grand Total $( dollar value)

    The dollar values are a result of multiplying the price and quantity variables from the objects attributes.

    It's not returning any dollar values. Just leaves blank space where they should be.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    I assumed that by return you meant a value being returned by a method using a return statement.
    What you are asking about is something that is displayed by the program.

    Are you asking about the contents of the message variable that is displayed by the JOptionPane method at the end of the program?


    Where do you assign any values to the toString variable in the RetailItem class?
    Last edited by NormR; January 30th, 2013 at 01:23 PM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    12
    Rep Power
    0
    Originally Posted by NormR
    I assumed that by return you meant a value being returned by a method using a return statement.
    What you are asking about is something that is displayed by the program.

    Are you asking about the contents of the message variable that is displayed by the JOptionPane method at the end of the program?
    Yes ... I can't figure out why it isn't returning a value. The values are assigned when I create the objects of the RetailItem class in the main method
  10. #6
  11. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    why it isn't returning a value
    What method are you talking about?

    Where does the code assign a value to the toString variable in the RetailItem class?
    I can NOT see where it does that.
    If you see where it is done, please copy the statement here that does it.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    12
    Rep Power
    0
    Originally Posted by NormR
    What method are you talking about?

    Where does the code assign a value to the toString variable in the RetailItem class?
    I can NOT see where it does that.
    In the main method where the three objects of the RetailItem class are declared
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    The variable: toString is not in the main() method. It can not be assigned a value in that method.

    Do you know what an assignment statement is? Here is an example:
    aVariable = aValue;

    Do you see any assignment statements in the code that assign a value to the variable: toString? The statements would look like this:
    toString = <SOME VALUE HERE>
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    12
    Rep Power
    0
    Originally Posted by NormR
    The variable: toString is not in the main() method. It can not be assigned a value in that method.

    Do you know what an assignment statement is? Here is an example:
    aVariable = aValue;

    Do you see any assignment statements in the code that assign a value to the variable: toString? The statements would look like this:
    toString = <SOME VALUE HERE>
    So it would be like

    ToString( description, unitsOnHand, price )?
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    There are several basic programming concepts you don't seem to understand. Here's a short list that I've seen so far. Can you give an example for each of the three questions?
    What is a variable?
    What is a method?
    How do you assign a value to a variable?
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    12
    Rep Power
    0
    Originally Posted by NormR
    There are several basic programming concepts you don't seem to understand. Here's a short list that I've seen so far. Can you give an example for each of the three questions?
    What is a variable?
    What is a method?
    How do you assign a value to a variable?
    I just started learning about this a couple weeks ago. So far have had only 5 class days. If I understand correctly a variable is a placeholder for some value and is defined by a statement like: variableName = vName. A method performs some process with the variables. I'm trying to create a class to hold data for a retail item on description, units on hand, and price. My main method should create 3 objects of this class and calculate inventory value and display the outbut in a window.
  22. #12
  23. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    If you know what a variable is
    and what an assignment statement is
    where in the code is there any value assigned to the toString variable in the RetailItem class?
    Here is its definition:
    Code:
    	String toString;
    Where is it assigned a value?

    This code gets the toString variable's value to build the message variable:
    Code:
    message = String.format( "Jacket \t $", jacket.toString );
    If toString has not been assigned a value then nothing will show.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    12
    Rep Power
    0
    Originally Posted by NormR
    If you know what a variable is
    and what an assignment statement is
    where in the code is there any value assigned to the toString variable in the RetailItem class?
    Here is its definition:
    Code:
    	String toString;
    Where is it assigned a value?

    This code gets the toString variable's value to build the message variable:
    Code:
    message = String.format( "Jacket \t $", jacket.toString );
    If toString has not been assigned a value then nothing will show.
    In my code I wrote something like:
    Code:
     
    public String toString()
    {
               return (price * unitsOnHand)
    }
    I guess the return statement doesn't assign it's value as price*unitsOnHand?
  26. #14
  27. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    That's a method with the same name. It is not the variable used here:
    Code:
    message = String.format( "Jacket \t $", jacket.toString );
    Where is that method called and where is its value saved?
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    12
    Rep Power
    0
    Originally Posted by NormR
    That's a method with the same name. It is not the variable used here:
    Code:
    message = String.format( "Jacket \t $", jacket.toString );
    Where is that method called and where is its value saved?
    Its called at the bottom of the first block of code
    Code:
    //toString
    	public String toString()
    	{
    		return ( "%d" + (price * unitsOnHand) );
    	}
    How would I go about storing it?
    Code:
    //toString
    	public String toString()
    	{
    		return ( "%d" + (price * unitsOnHand) );
    	}
    
            public void setToString ( String toString )	
    	{
    		toString = toString;//?
    	}
    
Page 1 of 2 12 Last
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo