Question

The String class is provided in the Java library. Provide your own implementation (name the new...

The String class is provided in the Java library.

Provide your own implementation (name the new class MyString2) with one data field, array of

characters, and the following methods:

 public MyString2(String s); constructor

 public MyString2 substring(int begin); create a new string starts at begin and return it.

 public MyString2 toUpperCase(); convert all characters in a string to uppercase and return it.

 public char[] toChars(); return string as an array of characters.

 public static MyString2 valueOf(boolean b); convert b value to string and return it.

Do not use any method from String, StringBuilder, or StringBuffer

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class MyString2 {

    private char[] data;

    public MyString2() {
        this.data = new char[0];
    }

    public MyString2(String s) {
        data = new char[s.length()];
        for (int i = 0; i < s.length(); i++) {
            data[i] = s.charAt(i);
        }
    }

    public MyString2 substring(int begin) {
        char[] result = new char[data.length - begin];
        for (int i = begin; i < data.length; i++) {
            result[i - begin] = data[i];
        }
        MyString2 myString = new MyString2();
        myString.data = result;
        return myString;
    }

    public MyString2 toUpperCase() {
        char[] result = new char[data.length];
        for (int i = 0; i < data.length; i++) {
            result[i] = Character.toUpperCase(data[i]);
        }
        MyString2 myString = new MyString2();
        myString.data = result;
        return myString;
    }

    public char[] toChars() {
        char[] result = new char[data.length];
        for (int i = 0; i < data.length; i++) {
            result[i] = data[i];
        }
        return result;
    }

    public static MyString2 valueOf(boolean b) {
        if (b) {
            return new MyString2("true");
        } else {
            return new MyString2("false");
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
The String class is provided in the Java library. Provide your own implementation (name the new...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT