# Thread: Anyway I could "clean up" or make this simple program more efficient/better?

1. Member
Join Date
Oct 2012
Posts
50
Rep Power
0

## Anyway I could "clean up" or make this simple program more efficient/better?

I wrote an application (which is based on a practice problem in this book I have) that prompts for and reads a double value representing a monetary amount. Then it determines the fewest number of each bill and coin needed to represent that amount, starting with the highest (assuming that a ten dollar bill is the maximum size needed). For example, if the value entered is 47.63, then the program should print the equivalent amount as:
4 ten dollar bills
1 five dollar bills
2 one dollar bills
2 quarters
1 dimes
0 nickles
3 pennies

Java Code:
```//PP 2.9
import java.util.Scanner;
public class NumberOfBills
{
public static void main (String[] args)
{
int tens, fives, ones, dimes, nickles, pennies, quarters;
double amount;
Scanner scan = new Scanner (System.in);
amount = scan.nextDouble();
//Multiply amount and each divisor by 100
amount *= 100;
tens = (int) amount / 1000;
amount = (int) amount % 1000;
fives = (int) amount / 500;
amount = (int) amount % 500;
ones = (int) amount / 100;
amount %= 100;
quarters = (int) amount / 25;
amount = (int) amount % 25;
dimes = (int) amount / 10;
amount = (int) amount % 10;
nickles = (int) amount / 5;
amount = (int) amount % 5;
pennies = (int) amount;
System.out.println (tens + " ten dollar bills");
System.out.println (fives + " five dollar bills");
System.out.println (ones + " one dollar bills");
System.out.println (quarters + " quarters");
System.out.println (dimes + " dimes");
System.out.println (nickles + " nickles");
System.out.println (pennies + " pennies");
}
}```
Everything seems to work out okay except for a few things. The first being that if the user inputs a large amount such as 1,000,000,000. The programs outputs that there are 2147483 tens, 1 five, 1 one, 1 quarter, 2 dimes, 0 nickles, and 2 pennies rather than just saying there are 1000000 tens, and 0 of everything else which should be correct. Also, for line 9, Eclipse is saying, "Resource leak: 'scan' is never closed." What does that mean?
Other than those two things are there any other ways I can make this program better/more efficient or clean it up a little without using any advanced techniques? Thanks!
Last edited by psx2514; 03-10-2013 at 05:42 AM.

2. Senior Member
Join Date
Jun 2007
Location
Bali, Indonesia
Posts
753
Rep Power
7

## Re: Anyway I could "clean up" or make this simple program more efficient/better?

Originally Posted by psx2514
Everything seems to work out okay except for a few things. The first being that if the user inputs a large amount such as 1,000,000,000. The programs outputs that there are 2147483 tens, 1 five, 1 one, 1 quarter, 2 dimes, 0 nickles, and 2 pennies rather than just saying there are 1000000 tens, and 0 of everything else which should be correct. Also, for line 9, Eclipse is saying, "Resource leak: 'scan' is never closed." What does that mean?
As we know that the maximum value can be stored by an int is Integer.MAX_VALUE = 2147483647. When you supply 1,000,000,000, and as can be seen in your snippet that you've multiply this value by 100 it will exceed this value. But when you do something like tens = (int) amount / 1000. The cast (int) amount only give you the maximum value an integer can store, which is 2147483647.

3. Moderator
Join Date
Apr 2009
Posts
11,302
Rep Power
18

## Re: Anyway I could "clean up" or make this simple program more efficient/better?

You can ignore the resource leak in this case.
It's a warning for you if you were, for example, opening a Scanner against a File. If you didn't close it then your code could leak resources, especially on systems where there you can have a finite number of files open at once.

In this case it doesn't matter as the underlying stream is the standard (console) in stream. You don't really want to close that one off.

4. Member
Join Date
Oct 2012
Posts
50
Rep Power
0

## Re: Anyway I could "clean up" or make this simple program more efficient/better?

As we know that the maximum value can be stored by an int is Integer.MAX_VALUE = 2147483647. When you supply 1,000,000,000, and as can be seen in your snippet that you've multiply this value by 100 it will exceed this value. But when you do something like tens = (int) amount / 1000. The cast (int) amount only give you the maximum value an integer can store, which is 2147483647.
Yeah, I figured that much. Is there a way I can make this program work without multiplying the amount and each divisor by 100, or is that the most efficient way to do things here?

5. ## Re: Anyway I could "clean up" or make this simple program more efficient/better?

Suppose there were an alien civilization with hundreds of different bills in their monetary system (and they have trumpet noses too!); you'd have to add a lot of similiar statements in your program ...

kind regards,

Jos

6. Member
Join Date
Oct 2012
Posts
50
Rep Power
0

## Re: Anyway I could "clean up" or make this simple program more efficient/better?

So, my program is good as it is? There's no way to make it better?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•