Jump to content

Help Needed with a program i am writing for Revision Practical

- - - - -

  • Please log in to reply
6 replies to this topic

#1
Gman

Gman

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
Hi all



i need some help with a Question i have been giving in a Revision Practical, tell me where i am going wrong PLEASE. Go easy on me i have only started learning java 5 weeks now im doing a B.Sc. (Hons) in Applied Computing.

The question is: Write a program that will accept a monetary amount in euro as input and will compute and display the number of each coin needed to make up this amount using the least number of coins possible. Only the following coins can be used 2 euro, 1 euro, 50 cent, 20 cent, 10 cent, 5 cent, 2 cent and 1 cent.

Sample output:
€2.99 is made up of
1 x 2euro + 0 x 1euro + 1 x 50 cent + 2 x 20 cent + 0 x 10 cent + 1 x 5 cent + 2 x 2 cent + 0 x 1 cent

here is what i have done so far and i cant get it to go beyond 1 euro...



//  Gman

// 26/10/2011

// euro to coin's program 


import java.util.Scanner;


	public class EuroToCoinSolve


	{


			public static void main(String[] args)

		

			{

				//create instance of scanner

				Scanner kb = new Scanner(System.in);

				// declare variable

				int twoEuro, oneEuro, twoEuroR, oneEuroR, amountN; 

				int fiftyCent, twentyCent, tenCent; 

				int fiftyCentR, twentyCentR, tenCentR; 

				double amount, fiveCent, twoCent, oneCent, fiveCentR, twoCentR, oneCentR;

				//get user input

				System.out.print("Enter euro to change in euro coins: ");

				amount = kb.nextDouble();

				

				amountN = (int)amount * 100;

				

				twoEuro = amountN / 200; 

				twoEuroR =  amountN % 200; 

				

				oneEuro = twoEuroR / 100;  

				oneEuroR = twoEuroR % 100;

				

				fiftyCent = oneEuroR / 50;

				fiftyCentR = oneEuroR % 50;

				

				twentyCent = fiftyCentR / 20;

				twentyCentR = fiftyCentR % 20;	

				

				tenCent = twentyCentR / 10;

				tenCentR = 	twentyCentR % 10;

				

				fiveCent =  tenCentR / 5;

				fiveCentR = tenCentR % 5;

				

				twoCent =  fiveCentR / 2;

				twoCentR = fiveCentR % 2;

				

				oneCent = twoCentR / 1;

					

				

				System.out.println(amount + " is made up of \n" + twoEuro + " x 2Euro " + oneEuro + " x 1Euro "+ fiftyCent + " x 50 Cent " + twentyCent + " x 20 Cent " + tenCent + " x 10 Cent " + fiveCent + " x 5 Cent " +  twoCent + " x 2 Cent " +  oneCent + " x 1 Cent " );

		

			}


	}



#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
amountN = (int)amount * 100;

The cast happens before the *100

#3
Gman

Gman

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
Hi wim DC can you explain a little more to me only learned a small bit of cast this week

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
if amount = 2.51, after casting it to an int (which doesn't hold decimals) it will just drop the decimals, thus become 2.
Since java will do the cast before doing the math, you will actually do this:
amount = 2.51;
amountN = (int) amount * 100;

==

amountN = 2 * 100

What you want is: first do the math, then cast it to an int, so just add brackets
amountN = (int) (amount * 100);


#5
Gman

Gman

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
Thanks for great help wim DC

have it working brill, havei done it the long way " is there a easier way " or is that the way it would be done... :lol:

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
There is a cleaner way -read: less repetitive code - , not sure if it's easier.


    public static void main(String[] args){

        Scanner scanner = new Scanner(System.in);

        Map<Integer, Integer> moneyBag = new TreeMap<Integer, Integer>(Collections.reverseOrder());



        moneyBag.put(200, 0);

        moneyBag.put(100, 0);

        moneyBag.put(50, 0);

        moneyBag.put(20, 0);

        moneyBag.put(10, 0);

        moneyBag.put(5, 0);

        moneyBag.put(2, 0);

        moneyBag.put(1, 0);



        System.out.println("Enter amount: ");

        int amount = (int) (scanner.nextDouble()*100);



        for (Integer coinValue : moneyBag.keySet()) {

            moneyBag.put(coinValue, amount / coinValue);

            amount = amount % coinValue;

        }



        for (Map.Entry<Integer, Integer> coin : moneyBag.entrySet()) {

            System.out.print(coin.getValue() + " x " + coin.getKey()/100.0 + "\u20AC | ");

        }

    }


As you see, initializing the map first takes some lines, but once that is finished, it's much quicker to get the rest of the code done.
I am not sure if you've seen collections yet, a map is just a key-value thing, It stores values based on a key, and you can get stuff back out using that key( 200, 100, 50 etc are the keys here).
The value in the map represent the amount of coins.

Key must be unique. I used a TreeMap which sorts the Keys (and values stick with the keys of course).
Meaning if I would have inserted in a different order, 200 would still come out first because I gave Collections.reverseOrder() as parameter, otherwise it sorts ascending.

For this it's crucial to have the order of the keys set or else, if 0.01€ would come out first, it would simply give me 145 cents if I entered 1.45€.


Good job on using Integer(int) for money by the way. Don't ever be temped to work with currency and use doubles. Doubles are not accurate enough when working with money.


Final note: "\u20AC" is the euro symbol.

#7
Gman

Gman

    Learning Programmer

  • Members
  • PipPipPip
  • 49 posts
yes haven't done collections yet but thanks for heads up and its looks like less code will look into them. i am going to run your code and and thanks for the (int) in currency phrase...

life saver man was 2 to 3 hour trying to find out why my code didn't work and it was down to two brackets :)

Gman




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users