File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes java problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "java problem" Watch "java problem" New topic
Author

java problem

peterx peter
Greenhorn

Joined: Nov 02, 2004
Posts: 19
Hi!
A parking charge is 2 dollor for up to three houres. After 3 houres the charge increase with 0.5$ every houre.
if a person parks his car 5.3 houres.
He must pay 2$ for first 3 houres and 1.5 dollor for every houre 2+1.5=3.5$

because for 2 hourse we pay 1$ and for 0,3 houre we should pay 0.5 dollor more.



the above codes result is:
2+(0.5*(5.3-3))=3.15$
but it must be 3.5$
because for 2 houres 1$ and for 0.3 houre 0.5$ dollor more.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 16687
    
  19

You need to round the number of hours up to the next half hour first.

If you already know this, (this was not clear in your question), and just want a suggestion on how to do it. I guess one trick is to double the value, round it up to the nearest whole number with the Math class, and half it back.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jerry Olsson
Greenhorn

Joined: Mar 24, 2004
Posts: 15
You can round up the last hour. Round up 0.3 to 1 with Math.ceil();

charge=2+(0.5*(Math.ceil(houre-3)));
Thomas Bigbee
Ranch Hand

Joined: Nov 29, 2001
Posts: 48
Sounds like a homework question to me.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: java problem
 
Similar Threads
Rational choice theory
Inputfile and getting information out
GridBagLayout
Math.round() a Tricky Question
rint() and round()