473,398 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Money program

KoreyAusTex
I am having a very hard time making this program work right, I am sort of lost and I was wondering if I might get some advice.

This is what I have so far but I have a rounding error when I use the amount 10.03:

Expand|Select|Wrap|Line Numbers
  1. public class Money
  2.     public static void main(String[] args)
  3.     { 
  4.         double TOTAL_AMOUNT = 10.03;
  5.  
  6.         int money = (int)(TOTAL_AMOUNT * 100);
  7.  
  8.         System.out.println("Your money amount " + TOTAL_AMOUNT + " consists of");
  9.  
  10.         System.out.println("\t" + (money / 100) + " dollars");
  11.  
  12.         money = money % 100;
  13.  
  14.         System.out.println("\t" + (money / 25) + " quarters");
  15.         money = money % 25;
  16.  
  17.         System.out.println("\t" + (money / 10) + " dimes");
  18.         money = money % 10;
  19.  
  20.         System.out.println("\t" + (money / 5) + " nickels");
  21.         money = money % 5;
  22.  
  23.         System.out.println("\t" + (money / 1) + " pennies");
  24.     }
  25. }
I know Math.round() is a useful class but can't figure how to put it in my program
Feb 5 '08 #1
7 2964
Ganon11
3,652 Expert 2GB
Do you need to use a floating point (decimal) number to hold the money? I see you immediately convert it to an integer - maybe you should just start with an integer.
Feb 5 '08 #2
You're problems is that you're not using Math.round.

just replace:
int money = (int)(TOTAL_AMOUNT * 100);
with:
int money = (int)Math.round(TOTAL_AMOUNT * 100);

it should round a lot more accurately now, try it out.

hope it helps :)

I am having a very hard time making this program work right, I am sort of lost and I was wondering if I might get some advice.

This is what I have so far but I have a rounding error when I use the amount 10.03:

Expand|Select|Wrap|Line Numbers
  1. public class Money
  2.     public static void main(String[] args)
  3.     { 
  4.         double TOTAL_AMOUNT = 10.03;
  5.  
  6.         int money = (int)(TOTAL_AMOUNT * 100);
  7.  
  8.         System.out.println("Your money amount " + TOTAL_AMOUNT + " consists of");
  9.  
  10.         System.out.println("\t" + (money / 100) + " dollars");
  11.  
  12.         money = money % 100;
  13.  
  14.         System.out.println("\t" + (money / 25) + " quarters");
  15.         money = money % 25;
  16.  
  17.         System.out.println("\t" + (money / 10) + " dimes");
  18.         money = money % 10;
  19.  
  20.         System.out.println("\t" + (money / 5) + " nickels");
  21.         money = money % 5;
  22.  
  23.         System.out.println("\t" + (money / 1) + " pennies");
  24.     }
  25. }
I know Math.round() is a useful class but can't figure how to put it in my program
Feb 5 '08 #3
BigDaddyLH
1,216 Expert 1GB
Loose the doubles and use ints -- cents.
Feb 5 '08 #4
Loose the doubles and use ints -- cents.
My professor wanted it in the format of 10.03 or 15.34. She didn't let us break it up, if I understand you correctly?
Feb 9 '08 #5
BigDaddyLH
1,216 Expert 1GB
My professor wanted it in the format of 10.03 or 15.34. She didn't let us break it up, if I understand you correctly?
Well, I'm not sure to say since I don't know the whole story, but if I were writing the program for my own benefit, I would work with ints, and merely format the output to look "15.34".

If you have time, ask your professor to clarify things for you.
Feb 9 '08 #6
This is very helpful. But what if the output is 0 and you don't want it to show in the results?
Jul 9 '13 #7
Nepomuk
3,112 Expert 2GB
You can always catch specific cases with an if branch. When working with integers, you'd simply put something like this:
Expand|Select|Wrap|Line Numbers
  1. if(money / 25 > 0) {
  2.     System.out.println("\t" + (money / 25) + " quarters");
  3. }
You could even use an if-else structure to give a more natural response:
Expand|Select|Wrap|Line Numbers
  1. if(money / 25 > 0) {
  2.     System.out.println("\t" + (money / 25) + " quarters");
  3. } else {
  4.     System.out.println("\tNo quarters");
  5. }
There are shorter ways of doing this, but this is the easiest to understand.
Jul 9 '13 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: =?Utf-8?B?R29vZFRyb3VibGU=?= | last post by:
Anyone know if / how it is possible to either disable, or reset the "trended items" in Microsoft Money 2004?? It is very frustrating, I know the program would work if I could clear out all the old...
0
by: =?Utf-8?B?dHJvdWJsZWQgTVMgTW9uZXkgMjAwMiB1c2Vy?= | last post by:
All of the sudden I seem to have trouble signing into my Microsoft Money 2002 account. When I fire up the program and enter my password I get this message: “Money is unable to verify your...
4
by: ejack | last post by:
Hello! I am having issues with my code. My program is supposed to take a given amount of money (1.33) and tell how many quarters, dimes, nickels and pennies would make up this sum of money. ...
1
by: =?Utf-8?B?bW9uZXkgdXNlciBrc2E=?= | last post by:
Can not start the program from my data file (3yrs data), or from the backup file. Also no "Repair file" option when selecting "File" as stated in HELP. Help appreciated. Thank you
1
by: progman417 | last post by:
My Shareware program (or rather Commerical Program with a time limited demo) used to have sales of around $2,000 per month around 2002, but then I got busy at work and didn't have time to maintain...
2
by: compdude | last post by:
Hello everyone, i need some help on my programming in python i am trying to make a program that can ask for information and document it on a excel spreadsheet. Im a beginner programmer and i am...
1
by: rockchicx23 | last post by:
i have to develop this snack machine program that makes you chose a snack put money in and give change here is my code so far, what do i use so the program compare the price and money deposited, I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.