Java String Subclass: String127A and Adding substring Method, Study notes of Computer Science

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

Pre 2010

Uploaded on 08/31/2009

koofers-user-0sr
koofers-user-0sr 🇺🇸

9 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C Sc 127A Monday 10-Nov Scribe's Section Leader ___________
Collaboration Work in teams of 2. The scribe is the person born closest to this room. Write the
scribe's section leader above and both names below:
1. _______________________________ 2. __________________________________
Problem Add method substring to class String127A to return a new java.lang.String like that String
class already does. Assume the indexes are in range and the 1st int argument <= 2nd int argument.
These assertions must pass
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) {

Partial preview of the text

Download Java String Subclass: String127A and Adding substring Method and more Study notes Computer Science in PDF only on Docsity!

C Sc 127A Monday 10-Nov Scribe's Section Leader ___________

Collaboration Work in teams of 2. The scribe is the person born closest to this room. Write the

scribe's section leader above and both names below:

1. _______________________________ 2. __________________________________

Problem Add method substring to class String127A to return a new java.lang.String like that String

class already does. Assume the indexes are in range and the 1st^ int argument <= 2nd^ int argument.

These assertions must pass

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) {