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 4 of 4

Thread: Calculation error...very New to Java..please be kind.

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Calculation error...very New to Java..please be kind.

    Hello, I am doing the famous Parking ticket simulator. I have the whole thing compiling and it works! however, I am out on my calculations. My vehicle is parked for 125 mins, and I paid for 60 minutes. I am being fined for 65 minutes of illegal parking.

    The first hour is $25 base fine, and after 120 minutes, $10 should be added. I have something wrong here, as when I run the code, it is adding $25 for the base rate, $20 for time parked. It is either looking at the total time parked as illegal, and charging me as such, or it is charging me $35 (25 for base fine + hourly fine)for the first hour of illegal parking, and $10 for the part hour...Please help, as I have gone through this again and again.

    Here is the code: (just the class that has the calculations, I can put the whole thing up, but it is quite large.)


    import java.text.DecimalFormat;
     
    public class ParkingTicket
    {
        private ParkedCar car;
        private PoliceOfficer officer;
        private double fine;
        private int minutes;
        final double BASE_FINE = 25.0;
        final double HOURLY_FINE = 10.0;
     
        public ParkingTicket(ParkedCar car, PoliceOfficer officer, int minutes) 
        {
            this.car = car;
            this.officer = officer;
            this.minutes = minutes;
        }
     
        public ParkingTicket(ParkingTicket ticket2)
        {
            car = ticket2.car;
            officer = ticket2.officer;
            fine = ticket2.fine;
            minutes = ticket2.minutes;
        }
     
        public void calculateFine()
        {
            if(car.getMinutesParked() >= 60)
            {
                if(car.getMinutesParked() - ParkingMeter.getMinutesPurchased() <= 60)
                {   fine = BASE_FINE;
                }
                else
                {
                    fine = BASE_FINE + (HOURLY_FINE * ((car.getMinutesParked() - ParkingMeter.getMinutesPurchased()) /60));
                }
            }
        }
     
        DecimalFormat decimalformat = new DecimalFormat("#0.00");
     
        public double getFine()
        {
            return fine;
        }
     
        public String toString()
        {
            String string = car + "\n" + officer + "\n" + "The fine is: \n $" +  decimalformat.format(fine) ;
            return string;
        } 
    }
    Last edited by Freaky Chris; November 2nd, 2011 at 04:44 AM. Reason: Added code highlighting


  2. #2
    Member
    Join Date
    Oct 2011
    Posts
    35
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Calculation error...very New to Java..please be kind.

    First off you should indent your code and use code tags.
    What is happening at:

    if(car.getMinutesParked() >= 60)

    Should you not have some type of calculation here?
    Last edited by csharp100; November 1st, 2011 at 09:24 PM.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculation error...very New to Java..please be kind.

    Sorry about the formatting...it got lost in the c&p..

    As far as that line..

    it is stating that if the car is parked for longer than 60 minutes, then issue a ticket.

    The .getMinutes Parked is from the Parked Car class.

    public int getMinutesParked()
    {
    return minutesParked;
    }
    Last edited by medicinaluser; November 1st, 2011 at 09:59 PM. Reason: trying to get it to format right

  4. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Calculation error...very New to Java..please be kind.

    I fixed it..it was not an error here..boy, feel kinda stupid..lol

    I missed a this statement, and after going through all files, I found it, and all is good.

    Essentiually, I was missing the new parking meter being created.

Similar Threads

  1. [SOLVED] What kind of object instantiation is this?
    By Lord Voldemort in forum Object Oriented Programming
    Replies: 3
    Last Post: July 7th, 2011, 07:00 AM
  2. I need calculation
    By Swiss518 in forum Loops & Control Statements
    Replies: 7
    Last Post: January 27th, 2011, 01:26 PM
  3. RPM Calculation
    By fobos3 in forum Algorithms & Recursion
    Replies: 2
    Last Post: September 21st, 2010, 08:53 AM
  4. sync two diffrent kind of threads?
    By adamruss in forum Threads
    Replies: 1
    Last Post: January 10th, 2010, 08:59 PM
  5. Which collection is best to do mathematical operation on it?
    By Sterzerkmode in forum Java Theory & Questions
    Replies: 1
    Last Post: May 7th, 2009, 04:48 AM