
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Information on the implementation of a substring method for the java string subclass named string127a. The method should return a new java string object based on the given beginindex and endindex. Assertions to ensure the method's correctness.
Typology: Study notes
1 / 1
This page cannot be seen from the preview
Don't miss anything!

String127A str = new String127A("abcd"); assertEquals ("", str.substring(1, 1)); // empty string assertEquals ("a", str.substring(0, 1)); assertEquals ("abc", str.substring(0, 3)); assertEquals ("abcd", str.substring(0, 4)); assertEquals ("bcd", str.substring(1, 4)); assertEquals ("cd", str.substring(2, 4)); assertEquals ("d", str.substring(3, 4)); assertEquals ("", str.substring(4, 4)); // A mutable string type public class String127A { private int n; private char [] theChars; // Precondition: initialString.length() <= 128 public String127A(String initialString) { theChars = new char [128]; n = initialString.length(); for ( int i = 0; i < n; i++) theChars[i] = initialString.charAt(i); } public int length() { return n; } // Precondition: index is in range of 0..size() - 1 public char charAt( int index) { return theChars[index]; } // Preconditions: beginIndex is in range of 0..size() // endIndex is in range of 0..size() // beginIndex <= endIndex public String substring( int beginIndex, int endIndex) {