Results 1 to 2 of 2
  1. #1
    urpalshu is offline Member
    Join Date
    Oct 2011
    Rep Power

    Default ArrayList must contain unique objects (Inheritance)

    I am new to Java, I am woring on learning inheritance.

    I am creating an abstract class called Thing.

    I have to put this requirement in, Please could somebody help me with Syntax

    Thing will have the following private class field:
    • Things, an ArrayList defined with a Thing generic. This ArrayList must be maintained as Thing objects are constructed. This ArrayList must contain all unique objects for which (object instanceof Thing) is true. Each time an object for which (object instanceof Thing) is true is constructed, a reference to that object must be put in the ArrayList, if that object does not already exist in the ArrayList. The Thing equals method will be used to test if the object is not unique, i.e., already exists in the ArrayList. Your code must not useany object casts and must not use any instance of operator.

    Java Code:
    import java.util.*;
    public abstract class Thing
    public String iName = new String();
    private ArrayList<Thing> iThings = new ArrayList<Thing>();
    public static void main(String[] args)
    Thing vehicle1 = new Vehicle();
    public Thing(String name)
    iName = name;
    public String getName()
    return iName;
    public abstract void show();
    public abstract boolean equals(Thing b);
    public void showThings()
    Thank you,
    Last edited by DarrylBurke; 10-10-2011 at 05:04 PM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: ArrayList must contain unique objects (Inheritance)

    Moved from 'Java Applets'. In future, please post in an appropriate forum -- the question has nothing at all to do with Applets.

    Also, please use code tags for posting code. I've added them for you this time.


Similar Threads

  1. Unique data check in a 2d array using objects
    By sarevok9 in forum New To Java
    Replies: 6
    Last Post: 04-20-2011, 03:57 AM
  2. ArrayList with Objects the right approach?
    By scheffetz in forum New To Java
    Replies: 1
    Last Post: 03-25-2011, 10:26 AM
  3. Arbitrary number of objects, each with a unique name.
    By providence in forum New To Java
    Replies: 8
    Last Post: 02-23-2011, 02:12 AM
  4. Help with objects in ArrayList
    By BiteMuncher in forum New To Java
    Replies: 7
    Last Post: 02-21-2011, 12:55 PM
  5. ArrayList with different objects? Help
    By xtrmi in forum New To Java
    Replies: 4
    Last Post: 02-27-2009, 09:51 PM

Posting Permissions

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