The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Java Help
|
Need help with a class that can be called to do math
Discuss Need help with a class that can be called to do math in the Java Help forum on Dev Shed. Need help with a class that can be called to do math Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 29th, 2013, 07:11 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 12
Time spent in forums: 2 h 39 m 38 sec
Reputation 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!
|

January 30th, 2013, 06:14 AM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
Quote: | isn't returning any values. |
What method are you talking about? Please post the program's output and explain what is wrong with it.
|

January 30th, 2013, 12:42 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 12
Time spent in forums: 2 h 39 m 38 sec
Reputation Power: 0
|
|
Quote: | 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.
|

January 30th, 2013, 01:17 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
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.
|

January 30th, 2013, 01:23 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 12
Time spent in forums: 2 h 39 m 38 sec
Reputation Power: 0
|
|
Quote: | 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
|

January 30th, 2013, 01:29 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
Quote: | 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.
|

January 30th, 2013, 01:30 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 12
Time spent in forums: 2 h 39 m 38 sec
Reputation Power: 0
|
|
Quote: | 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
|

January 30th, 2013, 01:39 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
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>
|

January 30th, 2013, 01:50 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 12
Time spent in forums: 2 h 39 m 38 sec
Reputation Power: 0
|
|
Quote: | 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 )?
|

January 30th, 2013, 02:07 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
|
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?
|

January 30th, 2013, 02:23 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 12
Time spent in forums: 2 h 39 m 38 sec
Reputation Power: 0
|
|
Quote: | 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.
|

January 30th, 2013, 02:33 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
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:
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.
|

January 30th, 2013, 02:41 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 12
Time spent in forums: 2 h 39 m 38 sec
Reputation Power: 0
|
|
Quote: | 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:
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?
|

January 30th, 2013, 02:45 PM
|
 |
Contributing User
|
|
Join Date: Aug 2010
Location: SW Missouri
|
|
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?
|

January 30th, 2013, 03:01 PM
|
|
Registered User
|
|
Join Date: Jan 2013
Posts: 12
Time spent in forums: 2 h 39 m 38 sec
Reputation Power: 0
|
|
Quote: | 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;//?
}
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|