Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 29th, 2013, 07:11 PM
jworkman5985 jworkman5985 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 12 jworkman5985 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #2  
Old January 30th, 2013, 06:14 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,956 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 56 m 11 sec
Reputation Power: 345
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.

Reply With Quote
  #3  
Old January 30th, 2013, 12:42 PM
jworkman5985 jworkman5985 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 12 jworkman5985 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #4  
Old January 30th, 2013, 01:17 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,956 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 56 m 11 sec
Reputation Power: 345
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.

Reply With Quote
  #5  
Old January 30th, 2013, 01:23 PM
jworkman5985 jworkman5985 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 12 jworkman5985 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #6  
Old January 30th, 2013, 01:29 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,956 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 56 m 11 sec
Reputation Power: 345
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.

Reply With Quote
  #7  
Old January 30th, 2013, 01:30 PM
jworkman5985 jworkman5985 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 12 jworkman5985 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #8  
Old January 30th, 2013, 01:39 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,956 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 56 m 11 sec
Reputation Power: 345
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>

Reply With Quote
  #9  
Old January 30th, 2013, 01:50 PM
jworkman5985 jworkman5985 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 12 jworkman5985 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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 )?

Reply With Quote
  #10  
Old January 30th, 2013, 02:07 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,956 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 56 m 11 sec
Reputation Power: 345
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?

Reply With Quote
  #11  
Old January 30th, 2013, 02:23 PM
jworkman5985 jworkman5985 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 12 jworkman5985 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #12  
Old January 30th, 2013, 02:33 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,956 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 56 m 11 sec
Reputation Power: 345
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.

Reply With Quote
  #13  
Old January 30th, 2013, 02:41 PM
jworkman5985 jworkman5985 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 12 jworkman5985 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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:
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?

Reply With Quote
  #14  
Old January 30th, 2013, 02:45 PM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,956 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 56 m 11 sec
Reputation Power: 345
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?

Reply With Quote
  #15  
Old January 30th, 2013, 03:01 PM
jworkman5985 jworkman5985 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 12 jworkman5985 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;//?
	}

	

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Need help with a class that can be called to do math

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap