Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

String Exercises: Create a class named StringExercises that contains the followi

ID: 3559963 • Letter: S

Question

String Exercises: Create a class named StringExercises that contains the following methods (you may use any of the string classes and methods we learned about yesterday): Write a method called namelnitials that takes one String as argument, pertaining to somebody's full name and returns a String of the name's initials. Usage example, Write a method called letterCount that takes two String arguments, one containing some text and the other containing a single letter. This method should then compute and return the count of all of the occurrences of that letter in the given text. Usage example, Write a method called largest By that takes two arguments: a String object containing some text and an integer flag 1 or 2. If the flag is 1, then your method should return the lexicographical largest word in the String object (that is, the one that would appear latest in the dictionary). If the flag is 2, then your method should return the largest-by-length word in the String object. In case of ties, return the first occurrence of the largest word. . Also, if the integer flag in anything other than 1 or 2, return the empty string Usage example, Write four different tests for each of the parts above in the main method.

Explanation / Answer

class StringFunctions
{
public String nameInitials(String name)
{
String initials = "";

// spliting the given complete name with spaces so as per input Bertrand Arthur Willaim Russell each word is stored in array i.e Betterand in array[0], Arthur in array[1] and so on
String array[] = name.split(" ");

// go through the all words in array and get first character(inital) of each word
for(int i = 0; i < array.length; i++)
{

// for the first time i = 0, array[0] is Betterand,array[0].charAt(0) means 'B' and adding one "."(Full Stop).

// next i = 1,so array[1].charAt(0) == Arthur.charAt(0) means "A" and add "." this process continues until the all words in array are completed
initials+=array[i].charAt(0)+".";
}

// returning all initials as String

return initials;
}

public int letterCount(String name,String letter)
{
int lcount = 0;

// converting String letter as char i.e. we gave "w" becoms 'w',,now compare this 'w' with name String characters
char ch = letter.charAt(0);
for(int i = 0; i < name.length(); i++)
{

// for ecah character in name we are comparing with letter i.e 'w' so whenever 'w' character is found we increasing the count value with 1;
if(name.charAt(i) == ch)
{
lcount++;
}
}

// finally returning count value

return lcount;
}

public String largestBy(String name,int flag)
{
String large = "";

// checking flag is otherthan 1 and 2,,if true returning " " else goes to the "else"loop
if(flag != 1 && flag != 2)
large = " ";

else
{
if (flag == 1)
{
// if flag == 1 we need to return latest word in array means last word in the array
String array[] = name.split(" ");
// latest word

// returning the last word in array,, array[0] gives first word in array, array[1] gives second word in array ,,,,finally array[ array.length - 1 ] gives the last word in array.


large = array[array.length - 1];

}


else
{
// flag is 2
String array[] = name.split(" ");

// first time we taking array[0] word as maxLength
int maxLength = array[0].length();

// compare all other words in array for longest word
for(int i = 0; i < array.length; i++)
{

// if array word length is greater than maxLength( i.e array[0] word length) then change the maxLength value as new word length.

// in large String we stroing the array word which is longest word in array
if(maxLength < array[i].length())
{
large = array[i];
maxLength = array[i].length();
  
}

}
}
}

// returning the longest word
return large;
  
}

public static void main(String args[])

{
StringFunctions sf = new StringFunctions();
String i = sf.nameInitials("Bertrand Arthur Willaim Russell");
System.out.println(i);

int count = sf.letterCount("I am the very model of a modern major general", "m");
System.out.println(count);

String largest = sf.largestBy("I am the very model of a modern major general", 2);
System.out.println(largest);

  
}

}