• Post Reply Bookmark Topic Watch Topic
  • New Topic

need help getting started with homework  RSS feed

 
Biran Quach
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Implement a class MyArrayList using an array of Objects (i.e., Object[]) and implement all the methods of java.util.ArrayList."
I have no idea what to do, i think i'm suppose to create all the arraylist methods without using arraylist. But i have no idea how to start it. Please help
 
Les Morgan
Rancher
Posts: 779
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would start out by going to the API and looking what ArrayList does and all of it's methods and properties, then looking at the list of other objects that you can use to build your own version.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your instructions are quite clear how: "using an array of Objects (i.e., Object[])"

Give it a try first, show us what you have.
 
Biran Quach
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is what i have so far, is this the right path?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Biran Quach wrote:this is what i have so far, is this the right path?


Are you sure you're allowed to deviate from the explicit instructions to "use an array of Objects, i.e., Object[])"? Sure, a Person is an Object but that would make your MyArrayList class only work with Person objects. I doubt that is what your instructor is looking for. Also, why do you have the "magic number" 5 when you are initializing your internal array? What is 5 supposed to represent?
 
Liutauras Vilda
Sheriff
Posts: 4922
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Keep instance variables as private. Always, unless there is a solid reason to do differently.

2. Why you choosing such cryptic variable names as: b, x, y, a, c, d? Can you tell me without looking to code, what variable a or c are meant to represent? Sure you can't, I couldn't too. That shouldn't happen when you write code, you should know exactly what variable is representing by looking to its name and that should happen instantly.

3. Why you decided to omit curly braces around if statement? Line 30. Better not to do so, especially when you learning to program.

4. One more style thing. Always use spaces around operators +, -, =, >, <, otherwise code becomes difficult to read.

5. Method name addindex. That should read as addIndex. I presume you know that. Try to follow that, that is considered as a good coding practice.

6. Leave an empty line between each declared method. That would help an eye to distinct where one method ends and where other begins.

That is it for a while from me
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you have read all about the List interface which is defined here? You will now know how many methods you need to implement, slightly over twenty of them. There are also two methods peculiar to ArrayList (and a few similar classes): 1 2. I see also that java.util.ArrayList implements Cloneable, which means you will have to override the clone() method too.
I suggest you do several things.
  • Search this website; a few years ago somebody else tried the same thing. They underestimated how big a task it would be.
  • Ask your teacher which version of List you are expected to implement. The latest version (Java8) has about double the number of methods which were in Java7; you may be able to implement them all by implementing the java.util.List interface and only writing the methods which were in the Java7 version.
  • The Collections Framework classes have been parameterised for the last eleven years. That means you do not store an Object[] behind the scenes, but an E[] array. Ask which of those you are supposed to implement. I think an E[] is better (and easier), but it is for your teacher to interpret the instructions.
  • I see Liutauras has commented about your spelling of the addindex method. I shall ask: why have you got an addindex method in the first place? That does not appear in the List interface, nor the ArrayList class, so why have you got it?
    Look at the older version of the List interface. That has 24 methods. Implement those 24 methods, the two mentioned above, equals hashCode toString and clone from Object, and do not create new methods like addindex. There is an add method in the List interface, and that is what you should implement.
     
    Biran Quach
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    +ArrayList()
    +add(o: Object) : void
    +add(index: int, o: Object) : void
    +clear(): void
    +contains(o: Object): boolean
    +get(index: int) : Object
    +indexOf(o: Object) : int
    +isEmpty(): boolean
    +lastIndexOf(o: Object) : int
    +remove(o: Object): boolean
    +size(): int
    +remove(index: int) : Object
    +set(index: int, o: Object) : Object

    These are the methods i'm suppose to use.These are my final code.In additional,im suppose to add another array and copy the first array to the new one with double index,if the old array is full. which is shown in add()
     
    Stefan Evans
    Bartender
    Posts: 1837
    10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ok, thats a fair amount of code.
    But does it work?

    I don't know if you are set up for unit testing but you should be able to follow this test even if you can't run it directly.



    Hint: with the code above, this test fails.
     
    Biran Quach
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    yea,i tried to use the code above and it did not work. Then wrote my own drive code but the result is not in String form. i tried to to implement a toString, but did not work.
    my drive code
     
    Campbell Ritchie
    Marshal
    Posts: 56562
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I suggest you stop writing code. I suggest you go back to your piece of paper and pencil and eraser (the last being probably the most important‍) and write down how you are going to add things to your List. Draw a diagram to show the state of the array and the values of the other fields. Work out how many of the fields you can delete. Work out how you tell when the array needs to be replaced by a larger array, then you can work out how to implement a method to create that larger array.
    What do you mean about not being able to implement toString? In which class?
     
    Liutauras Vilda
    Sheriff
    Posts: 4922
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It seems you haven't improved non of mentioned things earlier. Nevermind, you still have reference in case you decide to do so.

    What else? You have been asked what is magic number 5? I presume it is initial capacity.

    So you better write
    Do you see how 1 extra variable clarifies your code by milestone? You may not see it as an important part to improve now, but you will be forced to see it in industry when you start working.

    By the way, initial capacity suppose to be 10 if you want to mimic ArrayList class more accurately.

    Also, note how [] was placed next to type, but not reference variable as you did. Why? Because that reads as an array of Objects, not an array of b's. Technically it is allowed to do as you did, but it is unconventional - so FOLLOW standard conventions. You can find them here. It is very old document but still widely applied in practice.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!