programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Liutauras Vilda
• Tim Cooke
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Devaka Cooray
• Ron McLeod
• paul wheaton
Saloon Keepers:
• Tim Moores
• Piet Souris
• Tim Holloway
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Frits Walraven
• Scott Selikoff

# How can I start the following project

Ranch Hand
Posts: 76
• Number of slices to send:
Optional 'thank-you' note:
Hi everybody

I'm very new to java and my knowledge is very limited. Anyways I have to the little following project :

Write a Java program that reads in an integer representing the number of pennies and prints out the  best way (i.e. the fewest number of coins) to make change using British coins (50p, 20p, 10p, 5p, 2p  and 1p only)1.  For example, if the number of pennies entered is 73, then print out:
1x50p
1X20p
1x2p
1x1p

it must be as simple as possible.I was gonna make a condition, if the input is greater than 50 divide by 50 until it is less than 50p( I could not figure out how to make "UNTIL IT IS LESS THAN50p" part of condition)
then I started the following cod which gives me error in line 20.

Can sombody please tell me why i get this error and how I can do this in a very simple way using modular?

Thank you:)

Ranch Hand
Posts: 115
11
• 1
• Number of slices to send:
Optional 'thank-you' note:

saeid jamali wrote:
then I started the following cod which gives me error in line 20. Can sombody please tell me why i get this error...

What's the error you are getting?

Marshal
Posts: 76845
366
• 1
• Number of slices to send:
Optional 'thank-you' note:
Welcome to the Ranch

Find out how to use a Scanner for keyboard input. It is much easier than a buffered reader.
You have made the mistake of writing code before you worked out what to do. Start by writing down with a pencil (you will need a rubber to destroy the evidence when you get it wrong) exactly how you would calculate the change. Start with 73p as before then try it with other amounts e.g. 146p 219p (which are multiples of 73). Don't use floating point arithmetic; only use integer arithmetic. Don't use floating point numbers at all. Otherwise things will go wrong if you enter 0.73 and it turns out the nearest floating point number is 0.72999999999999...

saeid jamali
Ranch Hand
Posts: 76
• Number of slices to send:
Optional 'thank-you' note:

Jason Bullers wrote:

saeid jamali wrote:
then I started the following cod which gives me error in line 20. Can sombody please tell me why i get this error...

What's the error you are getting?

cannot make a static reference from a non-static field:(

Campbell Ritchie
Marshal
Posts: 76845
366
• 1
• Number of slices to send:
Optional 'thank-you' note:
You don't appear to have shown the static code with non‑static references in, but I shall guess that it is the main method. What most people do is add static to variables, but that is quite the wrong way to do things. The right way to do things is to move as much code as possible out of the main method.

Marshal
Posts: 8515
611
• 1
• Number of slices to send:
Optional 'thank-you' note:
You cannot use non static fields without instantiating the class. But it is not the main problem you need to solve now.
Read once again CR (Campbell Ritchie) post and concentrate on the technique has been mentioned in it.

Campbell Ritchie
Marshal
Posts: 76845
366
• 1
• Number of slices to send:
Optional 'thank-you' note:

Liutauras Vilda wrote:. . . But it is not the main problem you need to solve now. . . .

Agree. I don't think you can solve the real problem without going away from your computer.

Campbell Ritchie
Marshal
Posts: 76845
366
• 1
• Number of slices to send:
Optional 'thank-you' note:

A few minutes ago, I wrote:You don't appear to have shown the static code . . .

saeid jamali
Ranch Hand
Posts: 76
• Number of slices to send:
Optional 'thank-you' note:
This is my plan but I have problem with the operation part as it only works for 50p changes so far I couldn't add other changes to my result.

Campbell Ritchie
Marshal
Posts: 76845
366
• Number of slices to send:
Optional 'thank-you' note:

saeid jamali wrote:This is my plan but I have problem with the operation part as it only works for 50p changes so far I couldn't add other changes to my result.

A good start, but you are trying to do too much all at once. Forget about get the data. Any lines which look at all like Java® code must be struck out. So lines 5 6 and 13 go. I am sure you know how to get input and print the results anyway, so you can concentrate on the bit causing the problems.

What you are short of is how to convert 73p into 1×50p 1×20p 1×2p and 1×1p. And why there are no 10ps or 5ps in that output. You are simply guessing with %7. Where does 7 come from in the first place? That is the bit you have to work out, and everything else will be easy. Why is there exactly 1×50p? And if we doubled it up to £1.46 why would there be 2×50p?

Marshal
Posts: 5466
326
• Number of slices to send:
Optional 'thank-you' note:

We have rules about Cross Posting to other places on the internet. Please read our page -> BeForthrightWhenCrossPostingToOtherSites. The short version is, we don't mind if you cross post, but we do ask that you state that you have done so and cite where.

You have cross posted to java-forums.org

Campbell Ritchie
Marshal
Posts: 76845
366
• Number of slices to send:
Optional 'thank-you' note:

The other forum Tim noticed wrote:It is correct.
We've been decimalised for over 40 years...
Gone are the days of farthings, shillings, and groats.

When I was at school we had to do such arithmetic in our heads in shillings pence and farthings.