Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By JosAH

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

  1. #1
    psx2514 is offline Member
    Join Date
    Oct 2012
    Posts
    50
    Rep Power
    0

    Default 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);
    		System.out.print ("Please enter amount: ");
    		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 04:42 AM.

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

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

    Quote Originally Posted by psx2514 View Post
    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. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,817
    Rep Power
    19

    Default 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.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    psx2514 is offline Member
    Join Date
    Oct 2012
    Posts
    50
    Rep Power
    0

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

    Quote Originally Posted by wsaryada View Post
    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. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,353
    Blog Entries
    7
    Rep Power
    20

    Default 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
    yellowledbet likes this.
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    psx2514 is offline Member
    Join Date
    Oct 2012
    Posts
    50
    Rep Power
    0

    Default 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?

Similar Threads

  1. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  2. Program skips "If" code and goes straight to "Else"
    By Logik22 in forum New To Java
    Replies: 12
    Last Post: 01-21-2012, 05:40 PM
  3. How to make a "real" console program?
    By Pojahn_M in forum New To Java
    Replies: 3
    Last Post: 12-26-2011, 01:15 AM
  4. Simple "Refresh" and "Forward" buttons
    By needSolution in forum AWT / Swing
    Replies: 3
    Last Post: 12-12-2011, 01:15 AM
  5. Need help with "a simple order entry program"
    By sentica in forum New To Java
    Replies: 6
    Last Post: 10-17-2008, 05:38 AM

Posting Permissions

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