• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can I start the following project  RSS feed

 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 56584
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago, I wrote:You don't appear to have shown the static code . . .
I was mistaken about that. Sorry. I hadn't read your code properly But the solution remains unchanged.
 
saeid jamali
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A little administrative side 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: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!