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

    Question Objects passing information to constructors.

    Hello, I'm new at java so I'm sorry if I don't make much sense, but I need some help initiating objects. I have to write a program that calculates cab fares, and for the first part I have to ask what the cab ID is. In the instructions it 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". This is what I have so far:

    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 static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    Cab cab1 = new Cab("1111", cabID);
    Cab cab2 = new Cab("2222", cabID);

    System.out.print("Cab ID (1111 or 2222): ");
    this.cabID = input.nextInt();
    }
    }

    I know my objects are probably way off, I've tried a few different ways but I just can't get it. Thanks in advance!

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Objects passing information to constructors.

    Java Code:
    Cab cab1 = new Cab("1111", cabID);
    Cab cab2 = new Cab("2222", cabID);
    in your snippet, the 'cabID' is unnecessary. You'll note that the constructor for your cab only takes 1 parameter, but you are trying to pass it 2. Furthermore, cabID isn't actually a declared variable in your main class, so even if you could take two params, this would error out with an undefined variable.

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Objects passing information to constructors.

    P.S. You put the ID in quotes which denotes a String, but your constructor clearly says it should be an int. Loose the quotes. Also, what is the point of hard coding in a cab ID like 1111 or 2222 if you then replace it with one read from the keyboard?

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

    Default Re: Objects passing information to constructors.

    Yeah I know my objects are completely wrong, that's why I'm asking for help right now. I'm pretty sure the constructor and this part "this.cabID = input.nextInt();" are wrong too. :( I'm really new to java.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Objects passing information to constructors.

    The constructor is used to create some instance of the class. Generally it has a parameter list which is used to initialize all the classes instance variables. When you want to create an object you must pass in the data you want. If you have an object like cab, your constructor is correct. If you want to get input from a user to initialize an instance of Cab, you would want to do something like this:

    Java Code:
    get user input and store it in some integer variable
    create Cab object with stored user input

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

    Default Re: Objects passing information to constructors.

    Okay, thanks. Is my order correct? Also I'm pretty sure my objects are still wrong. For my assignment it says "The main chunk of your program will be a while loop that runs forever following this sequence of operations: a) Prompt for a Cab ID..." So I'm assuming my objects come before the while loop. What am I doing wrong :S

    public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    Cab cab1 = new Cab(cabID);
    Cab cab2 = new Cab(cabID);

    while (true) {
    System.out.print("Cab ID (1111 or 2222): ");
    int cabID = input.nextInt();
    }
    }

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: Objects passing information to constructors.

    That is close. You want to instead do the following

    Java Code:
    Declare and initialize a scanner
    
    loop
      print prompt
      get input
      create and initialize a Cab with user input
    end loop

Similar Threads

  1. Replies: 6
    Last Post: 10-02-2011, 06:55 PM
  2. main not passing information to created class
    By Teclis in forum New To Java
    Replies: 2
    Last Post: 04-19-2011, 09:58 PM
  3. Constructors Objects and Classes
    By Tykk in forum New To Java
    Replies: 4
    Last Post: 10-11-2009, 12:31 AM
  4. Passing objects into constructors
    By aaronfsimons in forum New To Java
    Replies: 8
    Last Post: 04-14-2009, 01:08 PM
  5. Passing variable information between classes
    By zen_to_go in forum New To Java
    Replies: 1
    Last Post: 10-30-2007, 09:09 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
  •