Question
In this assignment you will write a program that will implement your own version of String. Your class will be based around a character array.
In this assignment you will write a program that will implement your own version of String. Your class will be based around a character array. You will write several methods that mimic String methods and then perform searches within the String, similar to the ones from Assignment 2. Note: You do not have to worry about multiple occurrences of something you are searching for or blank Strings. You may NOT convert your character array to a String at any time other than the toString method. It would defeat the entire purpose of writing your own version. Requirements Your program must do the following in order to receive full credit on this assignment. 1. Create a class called MyString. 2. Make two private instance variables for MyString. a. An int, which is the length of the String. b. A char array, which is the characters in the String. 3. Create a default constructor for MyString which sets the length of the array to 0. 4. Create a regular constructor which takes a single String parameter and sets this MyString object to be that String. a. This means that the array should be as long as the parameter and each element in the array is one character from the parameter. 5. Create an boolean method called equals that accepts a String parameter and returns true if the contents of the parameter match the contents of the array. a. This should function identically to the equals method from String. b. Hint: Dont forget that String has a toCharArray method and a charAt method. 6. Create an int method called indexOf which takes a char argument and returns the index of the first occurrence of that char in the array. a. Again, you do not have to worry about multiple occurrences. b. This should return -1 if the char was not found. 7. Overload the indexOf method to create a version which takes a String parameter and returns the index of the start position of the parameter in the char array. a. Again, return -1 if it is not in the array. b. The whole parameter must be in the array, in order, for this to return an index. i. See Example Outputs c. Hint: For this and the upcoming steps, make sure you make use of your indexOf methods as they can save you a lot of time. 8. Write a boolean method called startsWith, which accepts a String parameter and returns true if the parameter matches the beginning of the array. 9. Write a boolean method called endsWith, which accepts a String parameter and returns true if the parameter matches the end of the array. 10. Write a boolean method called contains, which accepts a String parameter and returns true if the parameter is anywhere within the array. 11. Write a String method called toString which returns a String version of all the characters in the array. a. If there are none it should return a blank String. b. This completes the MyString class. 12. In your main file, write a private static String method called mainMenu, which prints out the main menu and returns the users input. The menu options are as follows. a. Create a Searchable String b. Print the current Searchable String c. Search within the String d. Quit i. Like Assignments 5 and 6, this should validate the users input before returning it. 13. Write a private static void method called searchString, which accepts a MyString object as a parameter. a. This parameter is the MyString the search will be done on. 14. Have searchString prompt the user for a String to search for and accept that from the user. 15. searchString will print out a different message for each of the following scenarios. a. The search string is not in the MyString b. The search is identical to the MyString c. The MyString starts with the search string d. The MyString ends with the search string e. Otherwise, print the start index of the search within the MyString i. All of these should use the methods you wrote in MyString 16. Write your main method, which will print a welcome message, run the main menu, have a loop to process inputs until the user quits, and a goodbye message after the loop exits. 17. You main method should declare and initialize a MyString object with the default constructor. 18. Inside the loop, determine which option the user typed in and do the proper action. The actions are explained in the steps below. 19. If the user chose to enter a new searchable string, prompt them for a string and then set the local MyString object to that string. a. Remember that one of MyStrings constructors takes a String parameter. 20. If the user chose to print the current string, print out the MyString object. 21. If the user chose to search for a string, call your searchString method and pass the MyString object. Example Inputs Below are five example runs of the program with the inputs and outputs. Remember, the graders will be testing your program against these as well as their own, so make sure you test these and come up with your own before submitting your program. #1 Hi. Welcome to the String searcher. What would you like to do? a. Create a Searchable String b. Print the current Searchable String c. Search within the String d. Quit b What would you like to do? a. Create a Searchable String b. Print the current Searchable String c. Search within the String d. Quit a Please enter the searchable String Cake Set Cake as the searchable String What would you like to do? a. Create a Searchable String b. Print the current Searchable String c. Search within the String d. Quit b Cake What would you like to do? a. Create a Searchable String b. Print the current Searchable String c. Search within the String d. Quit d Bye! #2 Hi. Welcome to the String searcher. What would you like to do? a. Create a Searchable String b. Print the current Searchable String c. Search within the String d. Quit a Please enter the searchable String cheese is good Set cheese is good as the searchable String What would you like to do? a. Create a Searchable String b. Print the current Searchable String c. Search within the String d. Quit c What String did you want to search for? cheese is really good Search term is not in the searchable String What would you like to do? a. Create a Searchable String b. Print the current Searchable String c. Search within the String d. Quit c What String did you want to search for? cheese The search is at the beginning of the string What would you like to do? a. Create a Searchable String b. Print the current Searchable String c. Search within the String d. Quit d Bye! #3 Hi. Welcome to the String searcher. What would you like to do? a. Create a Searchable String b. Print the current Searchable String c. Search within the String d. Quit a Please enter the searchable String aaaaa Set aaaaa as the searchable String What would you like to do? a. Create a Searchable String b. Print the current Searchable String c. Search within the String d. Quit c What String did you want to search for? a The search is at the beginning of the string What would you like to do? a. Create a Searchable String b. Print the current Searchable String c. Search within the String d. Quit c What String did you want to search for? aaaa The search is at the beginning of the string What would you like to do? a. Create a Searchable String b. Print the current Searchable String c. Search within the String d. Quit d Bye! #4 Hi. Welcome to the String searcher. What would you like to do? a. Create a Searchable String b. Print the current Searchable String c. Search within the String d. Quit c What String did you want to search for? test Search term is not in the searchable String What would you like to do? a. Create a Searchable String b. Print the current Searchable String c. Search within the String d. Quit a Please enter the searchable String test Set test as the searchable String What would you like to do? a. Create a Searchable String b. Print the current Searchable String c. Search within the String d. Quit c What String did you want to search for? st The search is at the end of the string What would you like to do? a. Create a Searchable String b. Print the current Searchable String c. Search within the String d. Quit d Bye! #5 Hi. Welcome to the String searcher. What would you like to do? a. Create a Searchable String b. Print the current Searchable String c. Search within the String d. Quit a Please enter the searchable String afk[0] = new MyString("Test"); Set afk[0] = new MyString("Test"); as the searchable String What would you like to do? a. Create a Searchable String b. Print the current Searchable String c. Search within the String d. Quit b afk[0] = new MyString("Test"); What would you like to do? a. Create a Searchable String b. Print the current Searchable String c. Search within the String d. Quit c What String did you want to search for? MyString The search starts at index 13 in the string What would you like to do? a. Create a Searchable String b. Print the current Searchable String c. Search within the String d. Quit d Bye!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started