Results 1 to 8 of 8
  1. #1
    Kirstielol is offline Member
    Join Date
    Mar 2012
    Posts
    28
    Rep Power
    0

    Question Taxi Driver Assignment Help!

    Hello! I'm very new to Java and I need a bit of help. Below is a picture of the requirements for my assignment.

    Taxi Driver Assignment Help!-assignment.jpg

    I'm having a lot of trouble with the objects, and calling methods.. mine are probably completely wrong. Any help is appreciated :) This is what I have so far:

    Java Code:
        public class Cab {
    
            private double companyTotalFare = 0.0;
            private double rate = 1.95;
            private double taxiTotalFare;
            private double tripFare;
            private int tripCounter;
            private int cabID;
    
            public Cab(int cabID) {
                this.cabID = cabID;
            }
    
            public void pickUp(double weight, boolean front) {
                if (front == true && weight < 40) {
                    System.out.print("PASSENGER SEAT AIRBAG IS OFF IN CAB " + cabID);
                }
            }
    
            public void dropOff(int minutes) {
                tripFare = minutes * rate;
                taxiTotalFare = taxiTotalFare + tripFare;
                tripCounter++;
            }
    
            public void endOfShift() {
                companyTotalFare = companyTotalFare + taxiTotalFare;
            }
    
            public void displayStats() {
                System.out.print("Cab " + cabID + " had " + tripCounter + " trips and brought in $" + taxiTotalFare + " from the days $" + companyTotalFare);
            }
        }
    
        public class Passenger {
    
            private double weight;
            private boolean front;
    
            public Passenger(double weight, boolean front) {
                this.weight = weight;
                this.front = front;
            }
    
            public double getWeight() {
                return weight;
            }
    
            public boolean getFront() {
                return front;
            }
        }
    
        public static void main(String[] args) {
    
            Scanner input = new Scanner(System.in);
    
            Cab cab1 = new Cab();
            Cab cab2 = new Cab();
    
            while (true) {
                System.out.print("Cab ID (1111 or 2222): ");
                int cabID = input.nextInt();
                if (cabID != 1111 || cabID != 2222) {
                    break;
                }
    
                System.out.print("Passenger weight: ");
                int weight = input.nextInt();
    
                System.out.print("Sitting in front seat (1 = YES, 0 = NO): ");
                boolean front = input.nextBoolean();
                
                pickUp(weight, front);
    
                System.out.print("How many minutes: ");
                int minutes = input.nextInt();
                dropOff(minutes);
                break;
            }
    
            cab1.endOfShift();
            cab2.endOfShift();
        }
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    Kirstielol is offline Member
    Join Date
    Mar 2012
    Posts
    28
    Rep Power
    0

    Default Re: Taxi Driver Assignment Help!

    Quote Originally Posted by Fubarable View Post
    What are your specific questions?
    Well, my objects aren't working, so they're definitely wrong. What's wrong with them? Also, how do you call a method and pass values into them?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Taxi Driver Assignment Help!

    Quote Originally Posted by Kirstielol View Post
    Well, my objects aren't working, so they're definitely wrong. What's wrong with them?
    Aren't working how? Are you getting error message? If so, please post them and show us your attempt at interpreting them.

    Also, how do you call a method and pass values into them?
    Please have a look at this great tutorial on Passing Information to a Method or a Constructor

  5. #5
    Kirstielol is offline Member
    Join Date
    Mar 2012
    Posts
    28
    Rep Power
    0

    Default Re: Taxi Driver Assignment Help!

    Quote Originally Posted by Fubarable View Post
    Aren't working how? Are you getting error message? If so, please post them and show us your attempt at interpreting them.
    I'm getting an error message. It says "non-static variable this cannot be referenced from a static context". I have no idea how to go about fixing this. Also, in my assignment instructions is says to "instantiate two Cab objects, passing the cab IDs of 1111 for the first Cab and 2222 for the second Cab into their constructors". I'm not exactly sure how to do that. I tried

    Java Code:
            Cab cab1 = new Cab(cabID);
            Cab cab2 = new Cab(cabID);
    but that didn't work, it gave an error "cannot find symbol cabID".

    As for that article, thank you! I will read through it now.

  6. #6
    jcarosella10 is offline Member
    Join Date
    Mar 2012
    Location
    Vestal, NY
    Posts
    36
    Rep Power
    0

    Default Re: Taxi Driver Assignment Help!

    I would put your driver (public static void main...) in a different class. Therefore fixing your static context problem

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Taxi Driver Assignment Help!

    Quote Originally Posted by jcarosella10 View Post
    I would put your driver (public static void main...) in a different class. Therefore fixing your static context problem
    This will not help fix the problem.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Taxi Driver Assignment Help!

    Quote Originally Posted by Kirstielol View Post
    I'm getting an error message. It says "non-static variable this cannot be referenced from a static context". I have no idea how to go about fixing this.
    Thanks for telling us the error. Now you'll have to indicate which lines are causing this error so we can better help you fix it.

    Also, in my assignment instructions is says to "instantiate two Cab objects, passing the cab IDs of 1111 for the first Cab and 2222 for the second Cab into their constructors". I'm not exactly sure how to do that. I tried

    Java Code:
            Cab cab1 = new Cab(cabID);
            Cab cab2 = new Cab(cabID);
    but that didn't work, it gave an error "cannot find symbol cabID".
    cabID has no meaning in the main method and doesn't belong there. Why not simply pass in actual int literals instead of variables that don't exist in that context? Simply pass in 1111 into the constructor call?

    As for that article, thank you! I will read through it now.
    You're welcome.

Similar Threads

  1. Replies: 0
    Last Post: 03-03-2012, 01:01 AM
  2. type 4 driver
    By Dayanand in forum New To Java
    Replies: 3
    Last Post: 04-06-2011, 02:49 PM
  3. JDBC Driver
    By Ursula in forum New To Java
    Replies: 6
    Last Post: 08-23-2010, 05:41 PM
  4. using a driver with a class
    By vendetta in forum New To Java
    Replies: 7
    Last Post: 01-11-2010, 06:12 AM
  5. Driver for Java-SQL
    By Pr0n in forum New To Java
    Replies: 7
    Last Post: 07-23-2009, 11:30 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •