Question: please help me with my computer science homework thx a lot Your program must do the following in order to receive full credit on this
please help me with my computer science homework thx a lot
Your program must do the following in order to receive full credit on this assignment.
Create a class called MyString.
Make two private instance variables for MyString.
An int, which is the length of the String.
A char array, which is the characters in the String.
Create a default constructor for MyString which sets the length of the array to 0.Create a regular constructor which takes a single String parameter and sets this MyString object to be that String.
This means that the array should be as long as the parameter and each element in the array is one character from the parameter.
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.
This should function identically to the equals method from String.
Hint: Dont forget that String has a toCharArray method and a charAt method.
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.
Again, you do not have to worry about multiple occurrences.
This should return -1 if the char was not found.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.
Again, return -1 if it is not in the array.
The whole parameter must be in the array, in order, for this to return an index.
See Example Outputs
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.
Write a boolean method called startsWith, which accepts a String parameter and returns true if the parameter matches the beginning of the array.
Write a boolean method called endsWith, which accepts a String parameter and returns true if the parameter matches the end of the array.
Write a boolean method called contains, which accepts a String parameter and returns true if the parameter is anywhere within the array.
Write a String method called toString which returns a String version of all the characters in the array.
If there are none it should return a blank String.
This completes the MyString class.
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.
Create a Searchable String
Print the current Searchable String
Search within the String
Quit
this should validate the users input before returning it.
Write a private static void method called searchString, which accepts a MyString object as a parameter.
This parameter is the MyString the search will be done on.
Have searchString prompt the user for a String to search for and accept that from the user.
searchString will print out a different message for each of the following scenarios.
The search string is not in the MyString
The search is identical to the MyString
The MyString starts with the search string
The MyString ends with the search string
Otherwise, print the start index of the search within the MyString
All of these should use the methods you wrote in MyString
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.
You main method should declare and initialize a MyString object with the default constructor.
Inside the loop, determine which option the user typed in and do the proper action. The actions are explained in the steps below.
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.
Remember that one of MyStrings constructors takes a String parameter.
If the user chose to print the current string, print out the MyString object.
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
Get step-by-step solutions from verified subject matter experts
