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: Question on my math

  1. #1
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Exclamation 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

    import java.io.*;
    public class Temp {
     
        public static void main(String[] args) throws IOException {
        	BufferedReader input= new BufferedReader(new InputStreamReader(System.in));
     
            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)"); 
            minutesstayed=Integer.parseInt(input.readLine()); 
            int result=minutesstayed/60;
            double result2=result+0.75;
            double charge=0.75*(minutesstayed/60)+result2;
            for(result=1;result<=10;result2++)
     
            System.out.println("Your Parking Fee is:"+" "+charge);
     
     
        }  
    }


  2. #2
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Question on my math

    Have you ever used the modulus operator? It returns the remainder of a division.

    So...
    int a = 4 % 2;
    System.out.println(a)
    Prints: 0

    and...
    int b = 12 % 7;
    System.out.println(b);
    Prints: 5

    That is 12 / 7 = 1 with a remainder of 5
    Last edited by helloworld922; October 25th, 2009 at 05:41 PM.

  3. #3
    Junior Member
    Join Date
    Oct 2009
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Question on my math

    yeah ive used modulus. I didnt think i needed it. Where would i use it?

  4. #4
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Question on my math

    int minutes = Integer.parseInt(in.readLine());
    int hours = minutes / 60;
    if (minutes % 60 > 0)
        hours++;

    If minutes modulo 60 is greater than zero, it adds another hour to the bill.

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default 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

Similar Threads

  1. math quiz program
    By hope.knykcah in forum Collections and Generics
    Replies: 1
    Last Post: October 23rd, 2009, 09:53 AM
  2. Math in Java?
    By [Kyle] in forum Java Theory & Questions
    Replies: 3
    Last Post: September 23rd, 2009, 12:21 PM
  3. Regex Question
    By igniteflow in forum Java SE APIs
    Replies: 1
    Last Post: August 28th, 2009, 11:46 AM
  4. [SOLVED] Difference in the code on changing logical operators
    By lotus in forum Loops & Control Statements
    Replies: 3
    Last Post: June 20th, 2009, 03:30 AM