1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

JAVA calling methods

  1. Mar 23, 2012 #1
    dfjchjd
     
    Last edited: Mar 23, 2012
  2. jcsd
  3. Mar 23, 2012 #2

    Borg

    User Avatar
    Gold Member

    I'm not sure what you're trying to figure out. Calling another method in the same class isn't difficult. Could you post what you have so far?

    As far as the correct thread, programming problems usually go in the Engineering, Comp Sci, & Technology section.
     
  4. Mar 23, 2012 #3
    fhgfj
     
    Last edited: Mar 23, 2012
  5. Mar 23, 2012 #4
    Not sure why it's centred that thread, hope it's still readable
     
  6. Mar 23, 2012 #5

    Borg

    User Avatar
    Gold Member

    It would be better if you code post the code. Use the code tags when you post and it will stay indented.
     
  7. Mar 23, 2012 #6
    yjfj
     
    Last edited: Mar 23, 2012
  8. Mar 23, 2012 #7

    Borg

    User Avatar
    Gold Member

    All that you have to do is to put the method call in where you've indicated.
    You don't need to call it from the ParkingTest class also. I'm not sure about other parts such as why you are dividing by 100 and multiplying by 60. Are your inputs intended to be minutes or hours?

    Code (Text):

    // Parking.java
    // Class to calculate parking fee
    import java.util.Scanner; // import Scanner

    public class Parking
    {
         int charge; // charge for parking
         int receipt; // receipt total of all customers
         int customer; // number of customers
         int entryTime; // user inputs entry time
         int exitTime; // user inputs exit time
         int timeDiffMin; // time difference in minutes
         int timeDifference; // time difference
         
         public void beginCharging()
         {
                Scanner input = new Scanner ( System.in ); // create new Scanner
             
                customer = 1; // initialize customer count
               
                System.out.println( "\nHello and welcome!" );
                System.out.println( "This is a parking fee program." );
           
                while ( customer <= 9)
                {      
                        System.out.printf(
                                "Please enter entry and exit time for customer %d\n",
                                customer ); // prompt
                        System.out.print( "Entry Time: " ); // input entry time
                        entryTime = input.nextInt();
                        System.out.print( "Exit time: " ); // input exit time
                        exitTime = input.nextInt();
                       
                        [B][COLOR="Blue"]// This is all that you need.
                        calculateCharges();[/COLOR][/B]
                       
                       
                        System.out.printf( "Thank you, your charge is $%d\n\n",
                                charge ); // display charge
                        customer = customer + 1;
                        receipt = receipt + charge;
                } // end while statement
                System.out.println( "Thank you for using this program" );
                System.out.printf( "Total of all charges: $%d", receipt );
                System.out.println( "Have a nice day!" );
               
         } // end method beginCharging
               
         public void calculateCharges()
         {
                timeDiffMin = (((exitTime/100)*60)+(exitTime%100))
                              -(((entryTime/100)*60)+(entryTime%100));
                timeDifference = ((timeDiffMin/60)*100)+(timeDiffMin%60);
     
                if ( timeDifference > 800 )
                    charge = 20;
                else if ( timeDifference > 700 )
                    charge = 18;
                else if ( timeDifference > 600 )
                    charge = 16;
                else if ( timeDifference > 500 )
                    charge = 14;
                else if ( timeDifference > 400 )
                    charge = 12;
                else if ( timeDifference > 300 )
                    charge = 10;
                else if ( timeDifference > 200 )
                    charge = 8;
                else if ( timeDifference > 100 )
                    charge = 6;
                else if ( timeDifference > 30 )
                    charge = 4;
                else if ( timeDifference > 15 )
                    charge = 2;
                else charge = 0;
             
           } // end method calculateCharges
    } // end class Parking
     
     
  9. Mar 23, 2012 #8
    dthg
     
    Last edited: Mar 23, 2012
  10. Mar 23, 2012 #9

    Borg

    User Avatar
    Gold Member

    You're welcome. The military time makes sense now - it just wasn't jumping out at me. One thing that I always recommend is to put in more error handling. For example, if you enter a decimal point or alphabetical character, the program blows up and ends. The best test is entering things that don't make sense - because that is exactly what the users will eventually do.

    BTW, your Java code is very good for someone who has only been doing it for three weeks.
     
    Last edited: Mar 23, 2012
  11. Mar 23, 2012 #10
    Havnt really done any error handling yet. so i just put in the welcome messages a message about how to enter the time correctly haha :)
    thanks!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: JAVA calling methods
  1. Is Java good to learn ? (Replies: 13)

  2. Java Problem (Replies: 0)

  3. Java help (Replies: 1)

Loading...