Java Program Practice: (Implement the String class) The String class is provided
ID: 3671747 • Letter: J
Question
Java Program Practice:
(Implement the String class) The String class is provided in the Java library. Provide your own implementation for the following methods (name the new class MyString1):
**DO NOT USE THE STRING OBJECT AND ITS METHODS.
IMPLEMENT THEM ALL AGAIN YOURSELF!
public MyString1(char[] chars);
public char charAt(int index);
public int length();
public MyString1 substring(int begin, int end);
public MyString1 toLowerCase();
public boolean equals(MyString1 s);
public static MyString1 valueOf(int i);
Explanation / Answer
class main { public static void main (String[] args) { char[] c = {'Z','e','T','s','u','B','o'}; MyString1 s = new MyString1(c); System.out.print("Verify contents: "); s.print(); System.out.print(" "); // indexing System.out.print("MyString1.charAt(int index): "); System.out.println(s.charAt(3)); // string length System.out.print("MyString1.length(): "); System.out.println(s.length()); // substring System.out.print("MyString1.substring(int begin, int end): "); s.substring(2,5).print(); System.out.print(" "); // to character array System.out.print("MyString1.toChars(): "); System.out.println(s.toChars()); // to lower case System.out.print("MyString1.toLowerCase(): "); s.toLowerCase().print(); System.out.print(" "); // equals char[] c2 = {'Z','e','t','s','u','b','o'}; MyString1 s2 = new MyString1(c2); char[] c3 = {'Z','e','T','s','u'}; MyString1 s3 = new MyString1(c3); System.out.println("MyString1.equals(): "); // this is why you use built-ins. When you start // re-implementing things, the normal methods don't work with // them. At least they could be made to work in a duck-typed // language... System.out.print(" "); s.print(); System.out.print(" == "); s.print(); System.out.print(" ? "); System.out.println(s.equals(s)); System.out.print(" "); s.print(); System.out.print(" == "); s2.print(); System.out.print(" ? "); System.out.println(s.equals(s2)); System.out.print(" "); s.print(); System.out.print(" == "); s3.print(); System.out.print(" ? "); System.out.print(s.equals(s3)); System.out.print(" "); // convert Integer to MyString1 MyString1.valueOf(34).print(); System.out.print(" "); } } /** * A partial re-implementation of the java String class * */ class MyString1 { char[] __data; /** * Create a new MyString1 object from a character array. * */ public MyString1(char[] chars) { // If you don't clone the array, the data will remain available // outside the class. this.__data = new char[chars.length]; System.arraycopy(chars, 0, this.__data, 0, chars.length); } /** * Print the MyString1 object. * Used for verfication of contents. * * This is a generative function, if for no other reason than I * could do it. */ public MyString1 print() { for(int i = 0; iRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.