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

Thread: Problem with automatically creating new objects with unique names

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Problem with automatically creating new objects with unique names

    I am learning Java in the course of an intro CS class, so I not very well versed in this language and I apologize in advance if I confuse some of the terms, though I did my best to use them accurately.

    For this a problem in this class I am taking, I need to design three classes that work together, but there is a certain aspect of it that I don't understand.

    These are the three classes that I have to design and the parts of them that are relevant to my question:

    Car
    Has a boolean field called parkedLegally //True if parked legally, false if parked illegally

    PoliceOfficer
    Has a string field called officerName //Contains the officer's name, like "Bob Smith"
    Has a method called evaluateCar //Accepts a Car object as a parameter and determines if the car needs to be fined based on its parkedLegally value. If, and ONLY IF, it is to receive a fine, a Ticket type object is to be generated. If no fine is to be assessed, a Ticket object is NOT generated.

    Ticket
    Has a double field called fineAmount //The amount of the fine, calculated by another method but not important to my issue

    Since I don't know how many Car objects a PoliceOfficer will be asked to evaluate, let alone whether those Car objects will be parked illegally or not, I can't know in advance how many ticket objects need to be created. Also, since there may be more than one ticket, they have to have unique names. Therefore, I wanted to write code so that if a ticket needs to be generated, the evaluateCar method creates a new Ticket object each time it is called and determines that a fine needs to be issued, and gives it a procedurally generated name like bobSmithTicket1, bobSmithTicket2, bobSmithTicket3... etc.

    What I don't understand is how to make the evaluateCar method create a Ticket object only under the proper circumstances, and to automatically assign a unique name to a Ticket. I can't seem to stick a string or anything like that in the area where the variable name would go in a declaration, so I can't generate new and uniquely identified Ticket objects. I have tried to look around and, as far as I can tell from my research, creating new objects after a Java program is compiled seems to be impossible. However, since the book I am working from asked me to do this I thought I would ask my specific question here just in case. Also, it would seem to be a huge limitation not to be able to create new instances of objects while a program was running. For instance, how would you make any program in java that required some number of objects but you couldn't know that number in advance (like this very problem, where I don't know how many tickets the user will need to write)?

    Thanks for your help!


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Problem with automatically creating new objects with unique names

    I reccommend you have a look at this ArrayList (Java Platform SE 6)


    Chris

  3. The Following User Says Thank You to Freaky Chris For This Useful Post:

    oniamien (December 4th, 2010)

  4. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs down Re: Problem with automatically creating new objects with unique names

    Ah ha! That seems to offer a solution to my problem. TYVM for your help!

Similar Threads

  1. Creating array from generic type objects?
    By AndrewMiller in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 13th, 2010, 09:22 PM
  2. Loop not creating objects
    By xecure in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2010, 10:48 PM
  3. regular expressions, characters unallowed in file names
    By chopficaro in forum Java Theory & Questions
    Replies: 3
    Last Post: May 6th, 2010, 03:17 PM
  4. [SOLVED] Creation of objects of Array in Java
    By sadi_shihab in forum Collections and Generics
    Replies: 4
    Last Post: July 9th, 2009, 01:38 PM
  5. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM