Search code examples
stringinheritancelinked-listmutabledelegation

linked list MutableString homework inheriting vs delegation


I have a linked list question that stems from this homework prompt I am about to post. it might help with the answer:

Specifics

The Java String class is immutable (you cannot change the contents). This is sometimes a hindrance to what you want to do with a string (i.e. make changes to the existing string). So, for this assignment you will create a class called MutableString. NOTE/DISCLAIMER: The Java API does have a class StringBuffer, which is mutable, but for the intents and purposes of this assignment we'll pretend we don't know about it ;-)

An object of the MutableString class contains the following operations/behaviors -- which means the class itself must contain the following methods:

Character charAt(int index): returns the Character at the specified index in the string -- if the index lies outside the string, throw a MutableStringIndexOutOfBoundsException (you must write this class)

void set(int index, Character ch): replaces the existing Character at the specified location -- if the index lies outside the string throw a MutableStringIndexOutOfBoundsException

void add(int index, Character ch): creates a new spot in the list for the Character at the index specified -- if the index lies outside the string throw a MutableStringIndexOutOfBoundsException

Character remove(int index): removes the Character at specified index -- if the index lies outside the string, throw a MutableStringIndexOutOfBoundsException

boolean remove(Character ch): removes the first occurrence (starting from the beginning of the MutableString) of the Character -- if the Character is not found, return false

boolean removeAll(Character ch): removes all occurrences of the specified Character -- if Character is not found, return false

void toUpper(): converts the current string to all upper case

void toLower(): converts the current string to all lower case

MutableString substring(int start, int finish): returns a string starting at the Character specified by start and concluding with the Character specified by finish -- if start or finish is outside the the string, throw a MutableStringIndexOutOfBoundsException

char [] toCharArray(): returns a char array containing all the characters in the string -- be sure and handle all cases

int length(): reports/returns the length of the string

String toString(): returns a String containing all the Characters

String toReverseString(): returns a String containing all the Characters in reverse order -- you must utilize recursion to accomplish this task

int compareTo(MutableString that): allows comparison of two MutableString objects -- this implies you will implement the Comparable interface for your MutableString class

void sort(): alphabetizes the letters in case-insensitive fashion in ascending order -- you must write the code for this sort (no API calls are allowed)

You must use a linked list to represent the characters of the string (you must write the LinkeList class). Your LinkedList class should contain fundamental operations for updating and modifying the list. With this in mind, implement the List interface in the Java API. For methods that you deem unnecessary for your LinkedList's functionality, stub those methods out and throw an Exception that contains information about what method was called and that it has not been implemented. NOTE: Make sure your LinkedList class does not do anything MutableString specific. Feel free to use the following stubbed out LinkedList class for your assignment. It may not contain all the methods you will need, but it does contain the methods from the List interface from the Java API.

MutableString must utilize the LinkedList behaviors (methods) wherever possible (don't write code in MutableString that does the same thing as something in the LinkedList class). Thus your MutableString will contain a field that is a reference to a LinkedList object. BTW, this concept is known as delegation, a very important idea in software engineering and design. Do NOT design MutableString so that it inherits from LinkedList.

Each Node of your MutableString should contain a Character -- note that this does not mean the reference to the data in your node class should/must be type Character -- that would make your LinkedList class specific to MutableString, and we don't want that in this case.

**the part I dont understand is the paragraph 2 above that talks about inheriting from the linked list class. I was always used to writing something like public class MutableString extends LinkedList but we cant do that and I dont really know how to start writing this mutablestring class without inheriting from the class. Help on explaining how to do it and the difference would be awesome. thanks.

Im not very good at linked lists so bare with this question :) **


Solution

  • It says that you should use delegation instead of inheritance.

    For difference between delegation and inheritance, check this out. It explains with an example.