Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using the array notations, build your own immutable String class with an array of characters to store characters. And realize the following String methods. You

Using the array notations, build your own immutable String class with an array of characters to store characters. And realize the following String methods. You will be given a main() method inside the MyStr.java file to test your class.

a. Construct a new class called "mystring". It includes an array of character to store the string content. Implement methods mentioned below. b. A constructor that initializes your array with an input String. You should store each character of the input String in each of the array cell. c. A method myLength() that has no input parameter but returns an int which is the length of this array. It should work the same as the length() method offered in the original String class. d. A method myCharAt(int a) that has a integer index as parameter, and returns a character that at the given index position in your array. It should work the same as the charAt() method offered in the original String class, which means indexing from 0. e. A method myEquals(mystring a) that has one your class object as parameter, and returns a Boolean. It should work the same as the equals() method offered in the original String class. f. You should construct your own methods with the features of array. You cannot transfer your array to a String and call equals() or charAt() directly to complete the requirements g. Note you are going to construct the immutable String class. None of above methods should change the original string array (caller object). They should return a new object containing the result. 

The main method is :

 public class MyStr { public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.println("Please input three sentences: "); mystring str1 = new mystring(in.nextLine()); mystring str2 = new mystring(in.nextLine()); mystring str3 = new mystring(in.nextLine()); System.out.println("The length for these three sentences are " + str1.myLength() + ", " + str2.myLength() + ", " + str3.myLength()); System.out.println("The second characters of these three sentences are: "); System.out.println(str1.myCharAt(1) + ", " + str2.myCharAt(1) + ", " + str3.myCharAt(1) + ". "); if(str1.myEquals(str2)) System.out.println("The first string and the second string are the same."); else System.out.println("The first string and the second string are different."); } }

The sample output is:

Please input three sentences: Hello Hello my apples. The length for these three sentences are 5, 5, 10 The second characters of these three sentences are: e, e, y. The first string and the second string are the same.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions

Question

Commen Name with scientific name Tiger - Wolf- Lion- Cat- Dog-

Answered: 1 week ago