Proposal for a New Java Exercise

Proposal for a New Java Exercise: Implementing a Custom MyArrayList Class

Summary

I’d like to propose a new Java exercise for the Exercism Java track called MyArrayList. This exercise focuses on creating a simplified version of Java’s ArrayList to give students hands-on experience with arrays, dynamic resizing, and core data structure methods. This project will help students better understand arrays, collections, and object-oriented programming, while building the foundational skills needed for more advanced data structures.

Purpose and Learning Objectives

The MyArrayList exercise is designed to help students:

  • Understand the fundamentals of dynamic data structures.
  • Gain proficiency in array resizing, indexing, and implementing typical collection methods.
  • Build a foundation for more complex data structures, such as linked lists and stacks.
  • Practice object-oriented programming concepts within the context of a custom Java collection type.

This exercise emphasizes practical skills directly applicable to real-world Java programming by exploring the mechanics of Java’s ArrayList.

Exercise Outline

Task

Students will implement a MyArrayList class that replicates the core functionality of Java’s ArrayList. The class will use a standard array internally to manage elements, and students will need to implement dynamic resizing when the array reaches capacity. By building this data structure from scratch, students gain a deeper understanding of the underlying mechanics of Java collections.

Key Methods

Here’s an outline of the primary methods to be implemented:

  1. Constructor: Initializes an internal array with a default capacity.

  2. Core Methods:

    • add(T element): Adds an element to the end of the list, resizing if needed.
    • get(int index): Returns the element at a specified index.
    • set(int index, T element): Replaces the element at a specified index.
    • remove(int index): Removes and returns an element at a specified index.
    • size(): Returns the current number of elements.
    • isEmpty(): Returns true if the list is empty.
  3. Search Methods:

    • contains(T element): Checks if a specific element exists in the list.
    • indexOf(T element): Returns the first index of a specified element.
    • lastIndexOf(T element): Returns the last index of a specified element.

Suggested Test Cases

Test cases should cover both basic functionality and edge cases to ensure robustness. Here are some sample cases:

  1. Basic Functionality:

    • Adding elements and retrieving them by index.
    • Resizing when adding elements beyond the initial capacity.
  2. Boundary Conditions:

    • Handling out-of-bound indices with appropriate exceptions.
    • Testing removal from different positions (first, middle, last).
  3. Additional Functionality:

    • indexOf and lastIndexOf for locating elements.
    • Converting to array and verifying with toArray.
    • Creating sublists and verifying content with subList.

Proposed Difficulty Level

For the Java track, I propose that this exercise be classified as Medium. Exercises of this level often involve slightly more detailed implementation, similar to cipher-based exercises like Affine Cipher, Rotational Cipher, and Atbash Cipher. This level will challenge students who have a basic understanding of arrays, exception handling, and OOP concepts but are ready to explore custom data structures.

Interest in Authoring and Guidance

I am also interested in authoring this exercise and would appreciate guidance and feedback on design and difficulty level. The forum’s input on test cases, structure, and any adjustments to difficulty level will be valuable to ensure this exercise aligns with the goals of the Java track.

Conclusion

The MyArrayList exercise offers a practical and meaningful project for students eager to deepen their understanding of Java arrays and collection implementation. It provides a strong foundation for future exercises involving more complex data structures.

Thank you for considering this proposal! I’m excited to collaborate on refining it to meet the needs of the Exercism Java track and help students build a solid understanding of Java’s foundational data structures.

Thanks for the proposal. I’ve moved this to the Java category so that the Java maintainers will see it.

My initial reaction is that this looks like a useful and reasonable exercise to add. It might be a little large with all the different methods to add. But I can see the value in having them all too.

Thank you, @iHiD, for moving this under the Java category.

Regarding the exercise length, I suggest skipping the optional methods I marked. While the total number of methods makes it lengthy, they are not individually complex. We could categorize this as a “Hard” difficulty level, considering the exercise’s overall scope. I have contributed to individual exercises before, but this will be my first time fully authoring a new exercise. Guidance from experienced members would be invaluable to ensure quality, as well as to help the Java track move towards the 150-exercise goal that the JavaScript track recently achieved. Please let me know your thoughts.

I’d also like to request that the discussion starting after post #13 in the other topic be moved to a separate thread, as @tasx suggested:

@mods: Could you please move all posts related to the proposed changes to the instructions into a separate thread? (starting from Guidance Needed for Adding New Test Cases for Complex Number Exercise - #13 by jagdishdrp)

I think the general idea sounds good as a practice exercise. If cutting down the number of methods, I’d probably suggest removing toArray(), toString() and probably clone() too as I don’t think they are the important part of the ArrayList functionality. Do you want to go ahead and draft a PR?

If you need help getting started, check out Practice Exercises | Exercism's Docs. Feel free to reach out if you have further questions or need further help.

I’m not sure what the other topic you’re discussing is :slight_smile:

I am sorry I forgot to mention the link to that topic in the last message. It is- https://forum.exercism.org/t/guidance-needed-for-adding-new-test-cases-for-complex-number-exercise

Which specific posts. Everything after #13 or specific posts?

Everything after 13 (if possible, skip the last two, i.e. 30 and 31). The title of the new topic should be something like “Updating institutions.md for Complex Number exercise.”

Thank you @iHiD, in advance!

Hello @kahgoh,

I’ve removed all the methods you and @iHiD suggested, so the exercise now has significantly fewer methods overall. Let me know what you think!

I’d definitely like to raise a PR and check out the Exercism docs, though it may take some time as I’m tied up with my college exams coming up. In the meantime, I’m available for minor PRs—if anyone suggests a change and cannot raise a PR themselves, feel free to tag me. I’d be happy to help raise the PR and contribute! If there are any other ways I can help, please let me know

Be aware that many students will want to take the easy shortcut, and make MyArrayList a thin wrapper around ArrayList. You’ll want to add some text in the instructions suggesting that maximum learning will come from not taking the easy road.

Sure! I’ll make sure to include that in the instructions. Additionally, I’ve been considering if we could implement a test to prevent the import of ArrayList from the java.util package. If we can add a test to check for this import, it would be very helpful!

Hello @iHiD,

I hope you’re doing well! I just wanted to kindly follow up regarding the discussions we previously mentioned. I understand you might be busy, but whenever you have some time, could you please move them to a different topic as discussed?

Thank you so much for your help!