gString Class

Step 1: problem domain

  • Suppose that java has not String Class.
  • In this projects students should analyse and implement a class named gString absolutely like Java String Class.
  • Students are not permitted to use Java String Class to implement gString.

Step 2: Public Interface

gString should support all (possible) functionalities of  Java String Class.

2-1- Constructor Methods

gString s=new gString ();       // set to ""
gString s=new gString("test");  // set to "test"
gString p=new gString(s);       // set to s (s is object of gString)
gString p=new gString(123);     // set to "123" 
gString p=new gString(231.043); // set to "231.043"

2-2- Setter Methods

public void set (String s);          // x.set("test")
public void set (char[] s);          // x.set(s)  s is array of char
public void set (gString s);         // x.set(y);  y is object of gString
public void set (int index,char c);  // x.set(5,'a');

2-3- Getter Methods

public char charAt (int index);
public int length();

2-4- Functionalities of class (Methods)

public long toLong();     // if can be converted
public double toDouble(); // if can be
public gString Reverse(); 
public gString setW(gString s, int width);  // s.set("test");  s.setW("0",6) returns--> "00test"
public gString concat(gString str);   //Concatenates the specified string to the end of this string.
public boolean	endsWith(gString suffix); //Tests if this string ends with the specified suffix.
public static gString	format(gString format, Object... args); // see Java String Class
public byte[]	getBytes(); 
public int	indexOf(int ch); //Returns the index within this string of the first occurrence of the specified character.
public int	indexOf(int ch, int fromIndex);       
public int	indexOf(gString str);
public int	indexOf(gString str, int fromIndex);
public boolean	isEmpty();
public int	lastIndexOf(int ch);
public int	lastIndexOf(int ch, int fromIndex);
public int	lastIndexOf(gString str);
public int	lastIndexOf(gString str, int fromIndex);
public gString	replace(char oldChar, char newChar);
public gString	replace(gString oldS, gString newS);
public String[] split(gString regex);
public boolean	startsWith(gString prefix);
public gString	substring(int beginIndex, int endIndex);
public char[]	toCharArray();
public gString	toLowerCase();
public gString	toUpperCase();
public gString	LTrim();  
public gString	RTrim(); 
public gString	Trim(); 
public gString	FullTrim(); // any spaces with the length more than 1 reduce to 1 space and then Trim
                            // "   this    is  a test   " -->  "this is a test"

2-5- Standard Library Methods overloading (such as toString, equals,…)

public String toString();
public int compareTo(String anotherString);
public boolean equals(Object anObject);
public int	hashCode();
public String clone();


Step 3: private variables

Students should use just an array of char and an int variable named length. All variables should be private.

Step 4: implementing main classes

Students work !

Step 5: useful application

To test the correctness and abilities of gString, Students should develop a small project. In this project User enter an string and then based on a menu, choose a function to apply on given string.

gString will be used widely in next projects such as gIS.