Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Trying to calculate hours by the hour

  1. #1
    Member
    Join Date
    Aug 2014
    Posts
    38
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Trying to calculate hours by the hour

    Hidy ho all, I have a homework assignment where I have to calculate the amount of hours someone parked in a parking lot.

    For every hour (or part of an hour) parked greater then 2 I need to charge them a bit more.

    now I can do that math easily enough. trouble is, I don't know if its possible to round up a double.

    my code has to involve a class. And, I think I know how to do that. My trouble is, I don't know how to round up doubles. Can you round up doubles?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Trying to calculate hours by the hour

    Look at the Math class. It has some useful methods.
    Also rounding can be done by adding 0.5 and casting to an int.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Aug 2014
    Posts
    38
    My Mood
    Confused
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Trying to calculate hours by the hour

    ok, so I have to take the amount of hours. And if they parked more then 3 hours, then add 50 cents on top of the flat rate to begin with. so if they parked 3.1 hours I need charge then 3.50.

    My math will be, if hours parked < 3 just charge them 2.00. If the hours parked > 3 and < 22 then minus 3, and then multiply the remainder (whatever that remainder is) by .5. what I need is to take something like .1-.4 and turn it into a whole number of 1 for this to work. The java.math.* library can do this?

    Also, I'm messing around with array lists in this program for the first time. I created a class called calcuateCharges (thats what the instructions say to call it) that stores a persons first name, last name, hours parked and the total to charge them. The above math is in that total figuring part.

    So far, when I compile this program I'm getting no errors. So, I'm fairly certain the syntax is right up to this point.
    Heres what I put:
    ArrayList<calculateCharges> Customers = new ArrayList<>();
    . I'd like to use a for loop to store parts of the customers in, while using a control variable to determine how many times the for loop runs. My question is, how to get access to individual parts of the array list?

  4. #4
    Junior Member
    Join Date
    Oct 2017
    Location
    Chandigarh, India
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Trying to calculate hours by the hour

    You can try Math.ceil() method.

    See JavaDoc link: https://docs.oracle.com/javase/8/doc...l#ceil-double-

    From the docs:

    ceil

  5. #5
    Member
    Join Date
    Dec 2013
    Location
    Honolulu
    Posts
    83
    Thanks
    1
    Thanked 4 Times in 2 Posts

    Default Re: Trying to calculate hours by the hour

    This looks to be a good program problem. It would not be on the parking meter though. It would be on a computer at a DOT. Department of Transportation. Thanks for the word problem. It helped.

Similar Threads

  1. Rush Hour Game
    By Viola123 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 3rd, 2014, 06:41 AM
  2. Replies: 1
    Last Post: April 21st, 2014, 04:39 PM
  3. Problem to get hour and minute
    By sassa44000 in forum Android Development
    Replies: 9
    Last Post: March 30th, 2014, 09:56 AM
  4. URGENT! Due in one hour. Please help!
    By imanoob in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 10th, 2013, 11:25 AM
  5. Rush hour game
    By zerocos in forum What's Wrong With My Code?
    Replies: 46
    Last Post: January 25th, 2013, 06:05 PM