ObjectList Java task











up vote
0
down vote

favorite












I have been given the following main method and must write the code for the ObjectList class. I am supposed to infer the necessary functions of the ObjectList class and write the class myself, however I am unsure exactly what I need to do to fulfill this function. Any help understanding this is greatly appreciated. This is the code I was given:



   ObjectList ol = new ObjectList(3);

String s = "Im Happy";
Dog d = new Dog();
DVD v = new DVD();
Integer i = 1234;

System.out.println(ol.add(s));
System.out.println(ol.add(d));
System.out.println(ol.add(v));
System.out.println(ol.add(i));

ol.remove(0);
System.out.println(ol.add(i));


System.out.println("Is the list full? "+ isFull());
System.out.println("Is the list empty? "+ isEmpty());

System.out.println("Total number of objects in the list: " + getTotal());

Object g = ol.getObject(1);
g.bark();









share|improve this question




















  • 1




    considering this code, since adding different types of objects to ObjectList it should be list data structure
    – Deadpool
    Nov 11 at 20:01










  • From the fact that you are adding both a String and an Integer, their only common supertype is Object, so the add method must have the signature public <Something> add (Object o); Because it appears in a println method without error, <Something> must be an Object of some type and not void. You can continue the analysis in similar manner.
    – James K Polk
    Nov 11 at 20:07

















up vote
0
down vote

favorite












I have been given the following main method and must write the code for the ObjectList class. I am supposed to infer the necessary functions of the ObjectList class and write the class myself, however I am unsure exactly what I need to do to fulfill this function. Any help understanding this is greatly appreciated. This is the code I was given:



   ObjectList ol = new ObjectList(3);

String s = "Im Happy";
Dog d = new Dog();
DVD v = new DVD();
Integer i = 1234;

System.out.println(ol.add(s));
System.out.println(ol.add(d));
System.out.println(ol.add(v));
System.out.println(ol.add(i));

ol.remove(0);
System.out.println(ol.add(i));


System.out.println("Is the list full? "+ isFull());
System.out.println("Is the list empty? "+ isEmpty());

System.out.println("Total number of objects in the list: " + getTotal());

Object g = ol.getObject(1);
g.bark();









share|improve this question




















  • 1




    considering this code, since adding different types of objects to ObjectList it should be list data structure
    – Deadpool
    Nov 11 at 20:01










  • From the fact that you are adding both a String and an Integer, their only common supertype is Object, so the add method must have the signature public <Something> add (Object o); Because it appears in a println method without error, <Something> must be an Object of some type and not void. You can continue the analysis in similar manner.
    – James K Polk
    Nov 11 at 20:07















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have been given the following main method and must write the code for the ObjectList class. I am supposed to infer the necessary functions of the ObjectList class and write the class myself, however I am unsure exactly what I need to do to fulfill this function. Any help understanding this is greatly appreciated. This is the code I was given:



   ObjectList ol = new ObjectList(3);

String s = "Im Happy";
Dog d = new Dog();
DVD v = new DVD();
Integer i = 1234;

System.out.println(ol.add(s));
System.out.println(ol.add(d));
System.out.println(ol.add(v));
System.out.println(ol.add(i));

ol.remove(0);
System.out.println(ol.add(i));


System.out.println("Is the list full? "+ isFull());
System.out.println("Is the list empty? "+ isEmpty());

System.out.println("Total number of objects in the list: " + getTotal());

Object g = ol.getObject(1);
g.bark();









share|improve this question















I have been given the following main method and must write the code for the ObjectList class. I am supposed to infer the necessary functions of the ObjectList class and write the class myself, however I am unsure exactly what I need to do to fulfill this function. Any help understanding this is greatly appreciated. This is the code I was given:



   ObjectList ol = new ObjectList(3);

String s = "Im Happy";
Dog d = new Dog();
DVD v = new DVD();
Integer i = 1234;

System.out.println(ol.add(s));
System.out.println(ol.add(d));
System.out.println(ol.add(v));
System.out.println(ol.add(i));

ol.remove(0);
System.out.println(ol.add(i));


System.out.println("Is the list full? "+ isFull());
System.out.println("Is the list empty? "+ isEmpty());

System.out.println("Total number of objects in the list: " + getTotal());

Object g = ol.getObject(1);
g.bark();






java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 20:20









JPadley

133219




133219










asked Nov 11 at 19:54









Gearoid Sheehan

42




42








  • 1




    considering this code, since adding different types of objects to ObjectList it should be list data structure
    – Deadpool
    Nov 11 at 20:01










  • From the fact that you are adding both a String and an Integer, their only common supertype is Object, so the add method must have the signature public <Something> add (Object o); Because it appears in a println method without error, <Something> must be an Object of some type and not void. You can continue the analysis in similar manner.
    – James K Polk
    Nov 11 at 20:07
















  • 1




    considering this code, since adding different types of objects to ObjectList it should be list data structure
    – Deadpool
    Nov 11 at 20:01










  • From the fact that you are adding both a String and an Integer, their only common supertype is Object, so the add method must have the signature public <Something> add (Object o); Because it appears in a println method without error, <Something> must be an Object of some type and not void. You can continue the analysis in similar manner.
    – James K Polk
    Nov 11 at 20:07










1




1




considering this code, since adding different types of objects to ObjectList it should be list data structure
– Deadpool
Nov 11 at 20:01




considering this code, since adding different types of objects to ObjectList it should be list data structure
– Deadpool
Nov 11 at 20:01












From the fact that you are adding both a String and an Integer, their only common supertype is Object, so the add method must have the signature public <Something> add (Object o); Because it appears in a println method without error, <Something> must be an Object of some type and not void. You can continue the analysis in similar manner.
– James K Polk
Nov 11 at 20:07






From the fact that you are adding both a String and an Integer, their only common supertype is Object, so the add method must have the signature public <Something> add (Object o); Because it appears in a println method without error, <Something> must be an Object of some type and not void. You can continue the analysis in similar manner.
– James K Polk
Nov 11 at 20:07














2 Answers
2






active

oldest

votes

















up vote
0
down vote













It's quite simple just need to create a list of Object types using ArrayList or LinkedList in the ObjectList class and implement the function as follows



public class ObjectList{
private ArrayList<Object> objects;
public ObjectList(int size)
{
objects = new ArrayList<Object>(size);
}

public String add (Object object)
{
objects.add(object);
//anything you would like to return I'm just returning a string
return "Object Added";
}

public void remove (int index)
{
objects.remove(index);
}

public boolean isEmpty()
{
return objects.isEmpty();
}

public int getTotal()
{
return objects.size();
}

public Object getObject(int index)
{
return objects.get(index);
}
}


The isFull()is not needed since ArrayListsize can change dynamically. You can use a simple array instead of ArrayList and then implement the isFull() function.



Also when getting an object using the get getObject() function, you need to cast it to the correct type before using there function. In your code g.bark() won't work because Object doesn't have a bark function



Object g = ol.getObject(1);

//This can give a runtime error if g is not a Dog
//Use try catch when casting
Dog d = (Dog)g;

d.bark();


EDIT



This is how you would implement isFull() and other functions if using arrays instead of ArrayList but for the sake of simplicity use the ArrayList version



 public class ObjectList{
private Object objects;
private int size = 0;
private int currentIndex = 0;


public ObjectList(int size)
{
this.size = size;
objects = new Object[size];
}

private boolean isFull() {
if(currentIndex == size)
return true;
else
return false;
}

public String add (java.lang.Object object)
{
if ( ! isFull() ) {
objects[currentIndex] = object;
currentIndex++;
return "Object added";
}
return "List full : object not added";

}

public void remove (int index)
{
if( !isEmpty() ) {
//shifting all the object to the left of deleted object 1 index to the left to fill the empty space
for (int i = index; i < size - 1; i++) {
objects[i] = objects[i + 1];
}
currentIndex--;
}
}

public boolean isEmpty()
{
if(currentIndex == 0)
return true;
else
return false;
}

public int getTotal()
{
return currentIndex;
}

public java.lang.Object getObject(int index)
{
if(index < currentIndex)
return objects[index];
else
return null;
}
}





share|improve this answer























  • It’s true that it isn’t needed, but the OP asked for the isFull() method so you should add that to your answer
    – JPadley
    Nov 11 at 21:22










  • considering the OP just asked for a guideline but I almost showed the solution , I think its not that necessary to show and OP can try to do him/her self. But anyways I have added the array version in the post.
    – Syed Ahmed Jamil
    Nov 11 at 21:55




















up vote
0
down vote













What it seems you want to achieve is "expand" the functionality of an ArrayList and create a custom list of objects. What you could do is to create a class extending the ArrayList and define/override any other methods you want.



public class ObjectList extends ArrayList<Object> {
//constructor with initial capacity
private int length;
public ObjectList(int size){
super(size);
this.length= size;
}

public Object getObject(int index){
return this.get(index);
}
}


Now you have the add and remove functions inherited from the ArrayList class and the getObject method.



Concerning the isFull method, you can check if the size of your ObjectList class is equal to the size it was instantiated with



if(this.size() == this.length){
return true
}
return false;


And getTotal



public int getTotal(){
return this.size();
}





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252613%2fobjectlist-java-task%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    It's quite simple just need to create a list of Object types using ArrayList or LinkedList in the ObjectList class and implement the function as follows



    public class ObjectList{
    private ArrayList<Object> objects;
    public ObjectList(int size)
    {
    objects = new ArrayList<Object>(size);
    }

    public String add (Object object)
    {
    objects.add(object);
    //anything you would like to return I'm just returning a string
    return "Object Added";
    }

    public void remove (int index)
    {
    objects.remove(index);
    }

    public boolean isEmpty()
    {
    return objects.isEmpty();
    }

    public int getTotal()
    {
    return objects.size();
    }

    public Object getObject(int index)
    {
    return objects.get(index);
    }
    }


    The isFull()is not needed since ArrayListsize can change dynamically. You can use a simple array instead of ArrayList and then implement the isFull() function.



    Also when getting an object using the get getObject() function, you need to cast it to the correct type before using there function. In your code g.bark() won't work because Object doesn't have a bark function



    Object g = ol.getObject(1);

    //This can give a runtime error if g is not a Dog
    //Use try catch when casting
    Dog d = (Dog)g;

    d.bark();


    EDIT



    This is how you would implement isFull() and other functions if using arrays instead of ArrayList but for the sake of simplicity use the ArrayList version



     public class ObjectList{
    private Object objects;
    private int size = 0;
    private int currentIndex = 0;


    public ObjectList(int size)
    {
    this.size = size;
    objects = new Object[size];
    }

    private boolean isFull() {
    if(currentIndex == size)
    return true;
    else
    return false;
    }

    public String add (java.lang.Object object)
    {
    if ( ! isFull() ) {
    objects[currentIndex] = object;
    currentIndex++;
    return "Object added";
    }
    return "List full : object not added";

    }

    public void remove (int index)
    {
    if( !isEmpty() ) {
    //shifting all the object to the left of deleted object 1 index to the left to fill the empty space
    for (int i = index; i < size - 1; i++) {
    objects[i] = objects[i + 1];
    }
    currentIndex--;
    }
    }

    public boolean isEmpty()
    {
    if(currentIndex == 0)
    return true;
    else
    return false;
    }

    public int getTotal()
    {
    return currentIndex;
    }

    public java.lang.Object getObject(int index)
    {
    if(index < currentIndex)
    return objects[index];
    else
    return null;
    }
    }





    share|improve this answer























    • It’s true that it isn’t needed, but the OP asked for the isFull() method so you should add that to your answer
      – JPadley
      Nov 11 at 21:22










    • considering the OP just asked for a guideline but I almost showed the solution , I think its not that necessary to show and OP can try to do him/her self. But anyways I have added the array version in the post.
      – Syed Ahmed Jamil
      Nov 11 at 21:55

















    up vote
    0
    down vote













    It's quite simple just need to create a list of Object types using ArrayList or LinkedList in the ObjectList class and implement the function as follows



    public class ObjectList{
    private ArrayList<Object> objects;
    public ObjectList(int size)
    {
    objects = new ArrayList<Object>(size);
    }

    public String add (Object object)
    {
    objects.add(object);
    //anything you would like to return I'm just returning a string
    return "Object Added";
    }

    public void remove (int index)
    {
    objects.remove(index);
    }

    public boolean isEmpty()
    {
    return objects.isEmpty();
    }

    public int getTotal()
    {
    return objects.size();
    }

    public Object getObject(int index)
    {
    return objects.get(index);
    }
    }


    The isFull()is not needed since ArrayListsize can change dynamically. You can use a simple array instead of ArrayList and then implement the isFull() function.



    Also when getting an object using the get getObject() function, you need to cast it to the correct type before using there function. In your code g.bark() won't work because Object doesn't have a bark function



    Object g = ol.getObject(1);

    //This can give a runtime error if g is not a Dog
    //Use try catch when casting
    Dog d = (Dog)g;

    d.bark();


    EDIT



    This is how you would implement isFull() and other functions if using arrays instead of ArrayList but for the sake of simplicity use the ArrayList version



     public class ObjectList{
    private Object objects;
    private int size = 0;
    private int currentIndex = 0;


    public ObjectList(int size)
    {
    this.size = size;
    objects = new Object[size];
    }

    private boolean isFull() {
    if(currentIndex == size)
    return true;
    else
    return false;
    }

    public String add (java.lang.Object object)
    {
    if ( ! isFull() ) {
    objects[currentIndex] = object;
    currentIndex++;
    return "Object added";
    }
    return "List full : object not added";

    }

    public void remove (int index)
    {
    if( !isEmpty() ) {
    //shifting all the object to the left of deleted object 1 index to the left to fill the empty space
    for (int i = index; i < size - 1; i++) {
    objects[i] = objects[i + 1];
    }
    currentIndex--;
    }
    }

    public boolean isEmpty()
    {
    if(currentIndex == 0)
    return true;
    else
    return false;
    }

    public int getTotal()
    {
    return currentIndex;
    }

    public java.lang.Object getObject(int index)
    {
    if(index < currentIndex)
    return objects[index];
    else
    return null;
    }
    }





    share|improve this answer























    • It’s true that it isn’t needed, but the OP asked for the isFull() method so you should add that to your answer
      – JPadley
      Nov 11 at 21:22










    • considering the OP just asked for a guideline but I almost showed the solution , I think its not that necessary to show and OP can try to do him/her self. But anyways I have added the array version in the post.
      – Syed Ahmed Jamil
      Nov 11 at 21:55















    up vote
    0
    down vote










    up vote
    0
    down vote









    It's quite simple just need to create a list of Object types using ArrayList or LinkedList in the ObjectList class and implement the function as follows



    public class ObjectList{
    private ArrayList<Object> objects;
    public ObjectList(int size)
    {
    objects = new ArrayList<Object>(size);
    }

    public String add (Object object)
    {
    objects.add(object);
    //anything you would like to return I'm just returning a string
    return "Object Added";
    }

    public void remove (int index)
    {
    objects.remove(index);
    }

    public boolean isEmpty()
    {
    return objects.isEmpty();
    }

    public int getTotal()
    {
    return objects.size();
    }

    public Object getObject(int index)
    {
    return objects.get(index);
    }
    }


    The isFull()is not needed since ArrayListsize can change dynamically. You can use a simple array instead of ArrayList and then implement the isFull() function.



    Also when getting an object using the get getObject() function, you need to cast it to the correct type before using there function. In your code g.bark() won't work because Object doesn't have a bark function



    Object g = ol.getObject(1);

    //This can give a runtime error if g is not a Dog
    //Use try catch when casting
    Dog d = (Dog)g;

    d.bark();


    EDIT



    This is how you would implement isFull() and other functions if using arrays instead of ArrayList but for the sake of simplicity use the ArrayList version



     public class ObjectList{
    private Object objects;
    private int size = 0;
    private int currentIndex = 0;


    public ObjectList(int size)
    {
    this.size = size;
    objects = new Object[size];
    }

    private boolean isFull() {
    if(currentIndex == size)
    return true;
    else
    return false;
    }

    public String add (java.lang.Object object)
    {
    if ( ! isFull() ) {
    objects[currentIndex] = object;
    currentIndex++;
    return "Object added";
    }
    return "List full : object not added";

    }

    public void remove (int index)
    {
    if( !isEmpty() ) {
    //shifting all the object to the left of deleted object 1 index to the left to fill the empty space
    for (int i = index; i < size - 1; i++) {
    objects[i] = objects[i + 1];
    }
    currentIndex--;
    }
    }

    public boolean isEmpty()
    {
    if(currentIndex == 0)
    return true;
    else
    return false;
    }

    public int getTotal()
    {
    return currentIndex;
    }

    public java.lang.Object getObject(int index)
    {
    if(index < currentIndex)
    return objects[index];
    else
    return null;
    }
    }





    share|improve this answer














    It's quite simple just need to create a list of Object types using ArrayList or LinkedList in the ObjectList class and implement the function as follows



    public class ObjectList{
    private ArrayList<Object> objects;
    public ObjectList(int size)
    {
    objects = new ArrayList<Object>(size);
    }

    public String add (Object object)
    {
    objects.add(object);
    //anything you would like to return I'm just returning a string
    return "Object Added";
    }

    public void remove (int index)
    {
    objects.remove(index);
    }

    public boolean isEmpty()
    {
    return objects.isEmpty();
    }

    public int getTotal()
    {
    return objects.size();
    }

    public Object getObject(int index)
    {
    return objects.get(index);
    }
    }


    The isFull()is not needed since ArrayListsize can change dynamically. You can use a simple array instead of ArrayList and then implement the isFull() function.



    Also when getting an object using the get getObject() function, you need to cast it to the correct type before using there function. In your code g.bark() won't work because Object doesn't have a bark function



    Object g = ol.getObject(1);

    //This can give a runtime error if g is not a Dog
    //Use try catch when casting
    Dog d = (Dog)g;

    d.bark();


    EDIT



    This is how you would implement isFull() and other functions if using arrays instead of ArrayList but for the sake of simplicity use the ArrayList version



     public class ObjectList{
    private Object objects;
    private int size = 0;
    private int currentIndex = 0;


    public ObjectList(int size)
    {
    this.size = size;
    objects = new Object[size];
    }

    private boolean isFull() {
    if(currentIndex == size)
    return true;
    else
    return false;
    }

    public String add (java.lang.Object object)
    {
    if ( ! isFull() ) {
    objects[currentIndex] = object;
    currentIndex++;
    return "Object added";
    }
    return "List full : object not added";

    }

    public void remove (int index)
    {
    if( !isEmpty() ) {
    //shifting all the object to the left of deleted object 1 index to the left to fill the empty space
    for (int i = index; i < size - 1; i++) {
    objects[i] = objects[i + 1];
    }
    currentIndex--;
    }
    }

    public boolean isEmpty()
    {
    if(currentIndex == 0)
    return true;
    else
    return false;
    }

    public int getTotal()
    {
    return currentIndex;
    }

    public java.lang.Object getObject(int index)
    {
    if(index < currentIndex)
    return objects[index];
    else
    return null;
    }
    }






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 11 at 21:52

























    answered Nov 11 at 21:13









    Syed Ahmed Jamil

    312315




    312315












    • It’s true that it isn’t needed, but the OP asked for the isFull() method so you should add that to your answer
      – JPadley
      Nov 11 at 21:22










    • considering the OP just asked for a guideline but I almost showed the solution , I think its not that necessary to show and OP can try to do him/her self. But anyways I have added the array version in the post.
      – Syed Ahmed Jamil
      Nov 11 at 21:55




















    • It’s true that it isn’t needed, but the OP asked for the isFull() method so you should add that to your answer
      – JPadley
      Nov 11 at 21:22










    • considering the OP just asked for a guideline but I almost showed the solution , I think its not that necessary to show and OP can try to do him/her self. But anyways I have added the array version in the post.
      – Syed Ahmed Jamil
      Nov 11 at 21:55


















    It’s true that it isn’t needed, but the OP asked for the isFull() method so you should add that to your answer
    – JPadley
    Nov 11 at 21:22




    It’s true that it isn’t needed, but the OP asked for the isFull() method so you should add that to your answer
    – JPadley
    Nov 11 at 21:22












    considering the OP just asked for a guideline but I almost showed the solution , I think its not that necessary to show and OP can try to do him/her self. But anyways I have added the array version in the post.
    – Syed Ahmed Jamil
    Nov 11 at 21:55






    considering the OP just asked for a guideline but I almost showed the solution , I think its not that necessary to show and OP can try to do him/her self. But anyways I have added the array version in the post.
    – Syed Ahmed Jamil
    Nov 11 at 21:55














    up vote
    0
    down vote













    What it seems you want to achieve is "expand" the functionality of an ArrayList and create a custom list of objects. What you could do is to create a class extending the ArrayList and define/override any other methods you want.



    public class ObjectList extends ArrayList<Object> {
    //constructor with initial capacity
    private int length;
    public ObjectList(int size){
    super(size);
    this.length= size;
    }

    public Object getObject(int index){
    return this.get(index);
    }
    }


    Now you have the add and remove functions inherited from the ArrayList class and the getObject method.



    Concerning the isFull method, you can check if the size of your ObjectList class is equal to the size it was instantiated with



    if(this.size() == this.length){
    return true
    }
    return false;


    And getTotal



    public int getTotal(){
    return this.size();
    }





    share|improve this answer

























      up vote
      0
      down vote













      What it seems you want to achieve is "expand" the functionality of an ArrayList and create a custom list of objects. What you could do is to create a class extending the ArrayList and define/override any other methods you want.



      public class ObjectList extends ArrayList<Object> {
      //constructor with initial capacity
      private int length;
      public ObjectList(int size){
      super(size);
      this.length= size;
      }

      public Object getObject(int index){
      return this.get(index);
      }
      }


      Now you have the add and remove functions inherited from the ArrayList class and the getObject method.



      Concerning the isFull method, you can check if the size of your ObjectList class is equal to the size it was instantiated with



      if(this.size() == this.length){
      return true
      }
      return false;


      And getTotal



      public int getTotal(){
      return this.size();
      }





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        What it seems you want to achieve is "expand" the functionality of an ArrayList and create a custom list of objects. What you could do is to create a class extending the ArrayList and define/override any other methods you want.



        public class ObjectList extends ArrayList<Object> {
        //constructor with initial capacity
        private int length;
        public ObjectList(int size){
        super(size);
        this.length= size;
        }

        public Object getObject(int index){
        return this.get(index);
        }
        }


        Now you have the add and remove functions inherited from the ArrayList class and the getObject method.



        Concerning the isFull method, you can check if the size of your ObjectList class is equal to the size it was instantiated with



        if(this.size() == this.length){
        return true
        }
        return false;


        And getTotal



        public int getTotal(){
        return this.size();
        }





        share|improve this answer












        What it seems you want to achieve is "expand" the functionality of an ArrayList and create a custom list of objects. What you could do is to create a class extending the ArrayList and define/override any other methods you want.



        public class ObjectList extends ArrayList<Object> {
        //constructor with initial capacity
        private int length;
        public ObjectList(int size){
        super(size);
        this.length= size;
        }

        public Object getObject(int index){
        return this.get(index);
        }
        }


        Now you have the add and remove functions inherited from the ArrayList class and the getObject method.



        Concerning the isFull method, you can check if the size of your ObjectList class is equal to the size it was instantiated with



        if(this.size() == this.length){
        return true
        }
        return false;


        And getTotal



        public int getTotal(){
        return this.size();
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 22:33









        NickAth

        187112




        187112






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252613%2fobjectlist-java-task%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values