Question: Sent with starter code and test strings, please send the completed code In OOP-speak, the above are different constructors allowing to instantiate the String class













In OOP-speak, the above are different constructors allowing to instantiate the String class into an individual String object. But we'll look into that in much more detail from next week onwards. Most of the built-in methods on strings are concerned with querying the content of the such as finding its length, searching for a substring or a character, or performing comparisons between different strings. Some methods will actually crehate a new Some methods defined on 5 tring s include the following: object. Returns the number of characters in the String. Does original String contain char (or String) s? Returns the index (as (int) within the original String of the first occurrence of Returns - 1 if the parameter String does not appear in the original String. Returns another String that is a copy of the subword between index and Returns the number of characters in the String. Does original 5 tring contain char (or String) 5? Returns the index (as within the original String of the first occurrence of c. Returns 1 if the parameter String does not appear in the original String. Returns another String that is a copy of the subword between index and Java interprets the symbol (+) as (inline) string concatenation when ot least one of the arguments is a This is why you can. concatenate a string and a number, say, to get a new string. However, Java does not understand the multiplication operator in the context of the FYI. S are "immutable", meaning that they cannot be changed once they are created. One effect of this is is that the string methods will return a new object, leaving the original one unchanged: String s4 - s1, tolppercose S Systen, out, sitintinc(s4): O Prints millo System.out.printh(s1): Prints Hello Your Turn! Try out these methods by writing a program to play around with them. You can use the code above (or similar code) to try these methods out. Make sure that you understand what these methods are supposed to be doing. Check out the official documentation here Go for what else the class can do for you. Let's write a small application to analyse and manipulate strings. There will be three parts to this exercise. Your solution to each part should live in its own method of a common class called given in the starter code. Part 1: Powers! Write a method called that will take a and a number n as parameters and will return a consists of the original one repeated (concatenated) n times. Caution! The code should the new string and not just print it on the terminal! So equals which following code the second Boolean variable is set to Can you explain why the first is not? int n=3; String bosis = "abc": String nfold = StringApp, por(basis, n ): boolean b1= "abcabcabc" = nfold; boolean b2 - "abcabcabc". equols(nfold): Part 2: Factors? How can I find all (non-overlapping) instances of a substring inside of another string? For example, given the string "helloworld hellomoon hellosun hello lamp post", and I want to find the indices of all instances of the substring "hello", how can I do that? Write a method called that takes two strings and returns the number of times the second string occurs in the first. factorCount("helloworld hellonoon hellosun hetlo tarp post", "hello") 4; Hint: Find the first instance (if it exists, of course), remove it from the original string, and repeat. By "remove: I really mean find the substring from the original string beyond the end of the first instance of the target string. For example, the first index of "hello" in the example string is 0 as it occurs right at the beginning of the string. If I remove the first instance of "hello", what I really want to do is to take the substring that starts at index 5 , consisting of the remainder of the string. Then I can search through that smaller substring of what remains. String s1 - "helloworld hellomoon hellosun hello lanp post"; String targetstring - "hello"; int firstIndex = si indexof (targetstring); if (firstindex =1 ) \{ system, out, printin(firstindex); W. Get the rost of the string to process. String restofstring - s1, substring(firstindex + targetstring iength O, st, lengthO); This would find the first instance of the string "hello" and I would end up with a resulting string that I would get by removing the initial So that in particular, String 5 - "hellosorid helLonoon hellosun HELLO Lamp post"; System. ouk.printin(factorcount(s, "hello", true)); Systen.out.printin(factorCount(s, "hello", false)); Would print 3 and 5. Testing In order to quickly test these functions out on longer strings you can use the input helper which eats a (relative) path to a text file and returns its contents as a string String ttigPor1 = (Comp122. reodfileisstring("teststrings/ttlgPar1, txt"); int kittenCount - StringApp. factorCount(ttigPar1, "kitty"); // tries youf code. Find some demo input files given in the starter code . We expect there to be 3,1,3, and 3 "kitten"s in files ttlgPar1 to ttlgPar4, respectively, and 11,9,11, and 12 times the factor "it: You can run the automarker on Codegrade as well for more checks. public static int factorcount(String a, String f, bootean caseSensitive) {} So that in particular, String s - "helloworld heltomoon hellosun HELlo lamp post"; System, out,printin(factorcount(s, "hello", true)): System, out, printlin(factorCount(s, "hello", folse)); Would print 3 and 5 . Testing In order to quickly test these functions out on longer strings you can use the input helper (relative) path to a text file and returns its contents as a string String ttlgPar1 - (Comp122, reodFileAsstrina "teststrings/ttlgPor1, txt"); int kittencount - StringApp. factorCount(telgPar1, "kitty"); /l tries yo code. Find some demo input files given in the starter code . We expect there to be 3,1,3, and 3 "kitten"s in files ttigPar1 to ttigPar4, respectively, and 11.9,11, and 12 times the factor "it: You can run the automarker on Codegrade as well for more checks. Write a program that counts the number of times each letter of the (English) alphabet occurs in the string given as first command line argument. To be clear, you should include a method so that the class is executable. This should read the input test from the first command line argument (not from standard in via the helper) and print exactly 26 lines, one for each (small) letter of the alphabet, like this, Here is another test run (that uses some shell magic to turn the content of the into a log quoted stringi don't worry about A word on efficiency Hint: you could re-use your code from part 2 here but would that be the most efficient way to do it? Yes and no! It is efficient in the sense that you do not spend time on a second implementation. Code re-use is always a good thing. However, calling your previous Hint: you could re-use your code from part 2 here but would that be the most efficient way to do it? Yes and no! It is efficient in the sense that you do not spend time on a second implementation. Code re-use is always a good thing. However, calling your previous method 26 times is inefficient in the sense that this will result in 26 readings of the original string. One can do better! For example, you could iterate over the string once, letter by letter, and keep a record of how often you have seen each of the 26 letters in an Array of characters A speedup by factor 26 will earn you brownie points with your boss if you are a software developer. Theoreticians will be less impressed, because both algorithms are O(n). Can you do better than that? Implementation Hint In Java, and variables are (more or less) interchangeable. A Java statement like int diff = 'e' - 'b'; is perfectly legal, i.e. Java can interpret the "difference of two letters" with no problem, and this will give an integer value. If the two letters are of the same case (both upper or both lower case), then this will give a value between 25 and 25 . In particular, if is a lower case letter, then int diff=ch=a 'i ped// touinzas{ noK // :0wnzas{2Hed/Ia.ayaposanoK/I}(u7uTsbuTJ.J)modbuTd75DTe75THqndIqued// One thing was certain, that the white kitten had had nothing to do with it:-it was the biack kitten's fautt entirely. For the white kitten had been having its face washed by the old cat for the last quarter of an hour (and bearing it pretty weil, considering); so you see that it _couldn't_ have had any hand in the mischief. The way Dinah washed her children's faces was thist first she held the poor thing down by its ear with one paw, and then with the other paw she rubbed its face all over, the wrong way, beginning at the nose: and just now, as I said, she was hard at work on the white kitten, which was lying quite still and trying to purr-no doubt feeling that it was all meant for its good. But the black kitten had been finished with earlier in the afternoon, and so, while Alice was sitting curled up in a corner of the great arm-chair, half talking to herself and half asleep, the kitten had been having a grand game of romps with the ball of worsted Alice had been trying to wind up, and had been rolling it up and down till it had all come undone again; and there it was, spread over the hearth-rug, all knots and tangles, with the kitten running after its own tail in the middle. "Oh, you wicked little thing!" cried Alice, catching up the kitten, and giving it a little kiss to make it understand that it was in disgrace. "Really, Dinah ought to have taught you better manners! You _ought_, Dinah, you know you ought!" she added, looking reproachfully at the old cat, and speaking in as cross a voice as she could manage-and then she scrambled back into the arm-chair, taking the kitten and the worsted with her, and began winding up the ball again. But she didn't get on very fast, as she was talking all the time, sometimes to the kitten, and sometimes to herself. Kitty sat very demurely on her knee, pretending to watch the progress of the winding, and now and then putting out one paw and gently touching the ball, as if it would be glad to help, if it might
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
