# Question on my math

• October 25th, 2009, 03:45 PM
SwEeTAcTioN
Question on my math
So in computer science we keep getting pounded with assignments and homework!!

And on one assignment its a parking garage program which asks you how long you parked (in minutes) then charges you 75 cents for each hour. i got that part but if you stay for part of an hour you have to pay for tha full hour. How would i do that? Im off by like 1 minute and have an infinite loop which freezes my computer.

All help is very much appreciated

Code :

import java.io.*;
public class Temp {

public static void main(String[] args) throws IOException {

int minutesstayed;
System.out.println("Welcome to NO DENTS PARKING GARAGE");
System.out.println();
System.out.println("How long did you park for (in minutes)");
int result=minutesstayed/60;
double result2=result+0.75;
double charge=0.75*(minutesstayed/60)+result2;
for(result=1;result<=10;result2++)

}
}

• October 25th, 2009, 04:36 PM
literallyjer
Re: Question on my math
Have you ever used the modulus operator? It returns the remainder of a division.

So...
Code :

int a = 4 % 2;
System.out.println(a)

Prints: 0

and...
Code :

int b = 12 % 7;
System.out.println(b);

Prints: 5

That is 12 / 7 = 1 with a remainder of 5
• October 25th, 2009, 04:38 PM
SwEeTAcTioN
Re: Question on my math
yeah ive used modulus. I didnt think i needed it. Where would i use it?
• October 25th, 2009, 04:52 PM
literallyjer
Re: Question on my math
Code :

int hours = minutes / 60;
if (minutes % 60 > 0)
hours++;

If minutes modulo 60 is greater than zero, it adds another hour to the bill.
• October 25th, 2009, 05:42 PM
helloworld922
Re: Question on my math
Convert the number of hours parked to a double, then use Math.ceil().

edit:

err, I just realized a flaw in this approach. Math.ceil() will still round up if you have 1.0 hrs (or some other "integer" hour value). Go with what literallyjer said :)