The goal of this assignment is to learn more about object-oriented programming, class hierarchies, and interfaces.
This assignment requires you to build a hierarchy of classes representing different kinds of strings.
It also has you practice writing meaningful toString() and equals() methods, overriding the ones that are provided by the Object class.
Java has a built-in class String. However, that class is declared final, meaning it cannot be extended, so it cannot be used as a superclass of any other class. Also, a Java String cannot be changed in any way.
So for this assignment, you will implement editable strings by extending the class HW3String. In this class, the instance variables are protected, meaning they are accessible to any subclass.
An editable string has all the operations of HW3String, and in addition the two methods
public void insert(char value, int index); public void delete(int index);and a constructor similar to the one in HW3String.
You must (10%) create an interface EditableStringInterface that has the length method of HW3String and the insert and delete methods of EditableString.
You must (10%) create a class EditableString that implements EditableStringInterface and extends HW3String. The class must have a unit test in its main method that exercises the following two methods: insert and delete.
Since this class inherits the length method from its parent, HW3String, there is no need to re-implement length in EditableString.
The insert (20%) method of EditableString must add the character in the correct place in the array, shifting all other characters out of the way. For example, given an editable string s whose value is "eco", s.insert('h', 2) should change s to now contain "echo". insert does this by calling either new or Arrays.copyOf to create a new array that is larger by one than the old data, and copying into the new array the characters from the old data and the new character in its parameters. In the example of "eco", the editable string has an array of length 3, with element 0 being 'e', element 1 being 'c', element 2 being 'o'. After s.insert('h', 2) the data variable should now be an array of length 4, with element 0 being 'e', element 1 being 'c', element 2 being 'h', element 3 being 'o', and the length being 4.
The delete (20%) method is similar. For example, if s contains "Moana", s.delete(1) should change s to now have "Mana".
You must (20%) create a new exception IllegalIndexInEditableString that extends RuntimeException. You must have your insert and delete methods throw this exception if the index given is out of bounds.
You must (20%) create a class Password that inherits from (i.e. extends) EditableString, but whose constructor throws IllegalArgumentException unless its parameter has at least one uppercase character, one lowercase character, one digit, and a minumum length of 8. The Character class in the Java standard library has useful methods such as isLowerCase that can be used with the String.charAt method to test individual characters of a string.
This class must have a toString method that returns as many asterisks as there are characters in the password. For example, if the password has 8 characters, toString should return "********". This method overrides the parent class's toString method.
This class must have an equals(Object otherPassword) method that returns whether two passwords are equal. One way to do this is by comparing the lengths of the two data arrays and, if the two lengths are equal, by comparing the contents of the two data arrays. Again, this method overrides the parent class's equals method.
The equals method must be as follows, with you filling out the missing part:
public boolean equals(Object otherPassword) { try { Password op = (Password)otherPassword; // missing part, to be filled out by the student -- must return true if 'op' has the same characters as 'this' } catch (ClassCastException ex) { System.out.println("caught " + ex); } return false; }
The Password class must have a unit test (main method) that exercises the constructor, the equals method, and the toString method, including at least all of the following code:
Password p = new Password("Xy3abcdef"); Password p2 = new Password("Xy3abcdef2"); EditableStringInterface esi = new Password("Xy3abcdef"); EditableString es = new Password("Xy3abcdef"); System.out.println("p is " + p); System.out.println("p2 is " + p2); System.out.println("esi is " + esi); System.out.println("es is " + es); if (p.equals(p2)) { System.out.println("p is equal to p2"); } else { System.out.println("p is not equal to p2"); } if (p2.equals(esi)) { System.out.println("p2 is equal to esi"); } else { System.out.println("p2 is not equal to esi"); } if (esi.equals(es)) { System.out.println("esi is equal to es"); } else { System.out.println("esi is not equal to es"); } if (es.equals(p)) { System.out.println("es is equal to p"); } else { System.out.println("es is not equal to p"); } try { Password p3 = new Password("helloworld"); System.out.println("error: all lower case password created, " + p3); } catch (IllegalArgumentException e) { System.out.println("correct: all lower case password is not valid"); }
The expected result is:
p is ********* p2 is ********** esi is ********* es is ********* p is not equal to p2 p2 is not equal to esi esi is equal to es es is equal to p correct: all lower case password is not valid
Be sure to review all your code at least once, adding comments where needed. Make sure each of your source files has a proper file comment including your name and a description of what your code does.
If you have time, it is usually helpful to have a substantial interval of time (hours to days) between when you complete your code, and when you do the final commenting and cleanup. Your comments will make more sense because you will be seeing the code with fresh eyes, and you might even spot and fix the occasional bug!
Use Laulima to turn in your files to the TA. Once you log into Laulima and select the ICS 211 site, on the left-hand side will be an assignments tab.