Use the API for the String class to answer the following questions. Assume that
ID: 667955 • Letter: U
Question
Use the API for the String class to answer the following questions. Assume that string1 and string2 are variables declared as type String. Also assume that x is an int variable. Write a complete Java statement as an answer for each question:
How would you find the first position where string2 appears in string1? Store the answer in x.
_____________________________
How would you remove leading and trailing spaces from string1?
________________________
How do you get the number of characters in string1? Store the answer in x.
________________________________
How do you convert string1 to all lowercase letters?
_________________
How can you tell if string1 has the same value as string2? You should get an integer answer stored in x.
__________________________________
If the value of x (from e above) is 0, what does that mean?
_____________________
Use the API for the Double class to answer the following question. Assume you have variables aReal (declared as a Double) and x (declared as an int):
What method can you use to convert aReal to an integer value stored in x?
___________________________________
Use the API for the Scanner class to answer the following questions. Assume you have a Scanner object called input.
What method would you use to get a true/false answer (stored in a variable called more) to tell you if there was any more input?
_______________________
What method would you use to get a String value from the input source and store it into a String variable called string1?
________________________
Explanation / Answer
2)
>Given two strings string1 and string2, find the smallest substring in string1 containing all characters of string2 efficiently.
For Example:
Input string1: “this is a test string”
Input string2: “tist”
Output string: “t stri”
example 1( Brute force solution )
a) Generate all substrings of string1 (“this is a test string”)
b) For each substring, check whether the substring contains all characters of string2 (“tist”)
c) Finally print the smallest substring containing all characters of string2.
example 2
>Build a count array count[] for string 2. The count array stores counts of characters.
count[‘i’] = 1
count[‘t’] = 2
count[‘s’] = 1
>Scan the string1 from left to right until we find all the characters of string2. To check if all the characters are there, use count[] built in step 1. So we have substring “this is a t” containing all characters of string2. Note that the first and last characters of the substring must be present in string2. Store the length of this substring as min_len.
>Now move forward in string1 and keep adding characters to the substring “this is a t”. Whenever a character is added, check if the added character matches the left most character of substring. If matches, then add the new character to the right side of substring and remove the leftmost character and all other extra characters after left most character. After removing the extra characters, get the length of this substring and compare with min_len and update min_len accordingly.
Basically we add ‘e’ to the substring “this is a t”, then add ‘s’ and then’t’. ‘t’ matches the left most character, so remove ‘t’ and ‘h’ from the left side of the substring. So our current substring becomes “is a test”. Compare length of it with min_len and update min_len.
Again add characters to current substring “is a test”. So our string becomes “is a test str”. When we add ‘i’, we remove leftmost extra characters, so current substring becomes “t stri”. Again, compare length of it with min_len and update min_len. Finally add ‘n’ and ‘g’. Adding these characters doesn’t decrease min_len, so the smallest window remains “t stri”.
>Return min_len.
1)
>String positions must be positive and start at 1
>String1 and String2 are strings and Position is an integer
>Also used to find the first position in String1 that its substring String2 begins. In this case, String1 and String2 are strings and Position is a variable
substring("str","st",1).
substring("abcabcabc","bc",X) (gives X=2).
substring("abcabcabc","bc",8).
substring("abc","",X). (gives X=1).
substring("abc","",2).
4)
Here is the syntax of this method:
public String1 toLowerCase()
or
public String1 toLowerCase(Locale locale)
5)
string1.equals(string2) is the way.
It returns true if string1 is equals to string2 in value. Else, it will return fals
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.