Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-16-2007, 06:48 PM
Member
 
Join Date: Jul 2007
Posts: 26
paul is on a distinguished road
how to properly express adding...
I want to be able to allow for an increase in cents, but obviously I don't want people to be able to have 15dollars and 190cents because that's just not how it is supposed to be. Can anyone give me some tips on how to properly express adding to dollars and subtracting to cents when my cents > 99?

Also eclipse is giving me an error when i try and add fixedCents in my toString method. How do i write it so that I get the fixedCents variable from the add method?


Code:
public class USDollars { private int dollars; private int cents; private int increase; public USDollars(int dollars, int cents, int increase){ this.dollars = dollars; this.cents = cents; this.increase = increase; } public int add(){ fixedCents = cents + increase; if (fixedCents > 99){ dollars + 1; fixedCents-100; } } public String toString(){return dollars+"."+fixedCents;} }
Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-07-2007, 06:08 AM
Member
 
Join Date: Jul 2007
Posts: 39
coco is on a distinguished road
You are getting an error about the fixedcents variable in your toString method because fixedcents no longer exists.

Fixedcents is declared in your add() method. Any variable that is declared in a method is freed/garbage collected when that method terminates. Hence why you have dollars, cents, and increase declared at the top of your file outside of any method. If you want fixedCents to exist for all your methods declare it with dollars, cents and increase.

I should mention that at second glance, I don't see where you've declared FixedCents at all... Thus, my above comment may or may not be correct.

Why don't you just change every instance of fixedCents to cents?
Example

Code:
public int add(){ cents = cents + increase; if (cents > 99){ dollars + 1; cents-100; } } public String toString(){ return dollars+"."+cents; }
Greetings.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Log4j not working properly.... prakash_dev Advanced Java 0 03-17-2008 01:13 PM
Adding taglibs in JSP Java Tip Java Tips 0 01-14-2008 12:43 AM
Adding Tabs to JTabbedPane blackstone AWT / Swing 2 11-14-2007 04:15 PM
problem with viewing .java files when Visual J# 2005 Express Edition exist unhurt New To Java 3 11-03-2007 02:58 PM
Adding a Restock Fee Nexcompac New To Java 2 07-31-2007 03:46 PM


All times are GMT +3. The time now is 09:50 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org