Write a class with the following static methods: - wordCount. This method should
ID: 3536364 • Letter: W
Question
Write a class with the following static methods:
- wordCount. This method should accept a reference to a String object as an argument and return the number of words contained in the object.
- arrayToString. This method accepts a char array as an argument and converts it to a String object. The method should return a reference to the String object.
- mostFrequent. This method accepts a reference to a String object as an argument and returns the character that occurs the most frequently in the object.
- replaceSubstring. This method accepts three references to String objects as arguments. Let's call them string1, string2, and string3.
It searches string1 for all occurrences of string2. When it finds an occurrence of string2, it replaces it with string3. For example, suppose the three arguments have the following values:
string1: "the dog jumped over the fence'
string2: ''the''
string3: "that"
you need to have a class and a main program
import java.util.Scanner;
public class miscellaneousString
{
public static void main(String[] args)
{
String string1 = "the dog jumped over the fence";
String string2 = "the";
String string3 = "that";
char[] charArray = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v','w', 'x',
'y', 'z'};
// wordCount method
int numberOfWords = wordCount(string1);
System.out.println("Count the number of words: " + string1);
System.out.println("Number of words given string: " + numberOfWords);
// arrayToString method
String character = arrayToString(charArray);
System.out.print(" Given characters in the array:");
for(int i = 0; i < charArray.length; i++)
System.out.print(charArray[i] + " ");
System.out.print(" String from given character array: "
+ character);
// mostFrequent method
char mostOccurrence = mostFrequent(string1);
System.out.println(" Given string to get the most occured characters: "
+ string1);
System.out.println("The most occured character in given string: "
+ mostOccurrence);
//replaceSustring method
String string = replaceSubstring(string1, string2, string3);
System.out.println(" Given string to modify: " + string1);
System.out.println("Replace for: " +string2);
System.out.println("After replacing, the new string: " + string);
}
}
the class
public class miscellaneousStringClass
{
int numberOfWords;
/*
method should accept a reference to a string object
as an argument and return the number of words contain
in the object
*/
private static int wordCount(String string1)
{
int number = 0;
for(int i = 0; i < string1.length(); i++)
{
char ch = string1.charAt(i);
if(Character.isWhitespace(ch))
{
number = number + 1;
}
}
return number + 1;
}
/*
method should accept a char array as an argument and convert i to a string object.
The method should return to the string object.
*/
private static String arrayToString(char[] charArray)
{
return String.valueOf(charArray);
}
/*
method accepts a reference to a string object as an argument
and returns the character that occurs the most frequently
in the object.
*/
private static char mostFrequent(String string1)
{
char mostOccurrence = ' ';
int most = 0;
int j;
for(int i = 0; i < string1.length(); i++)
{
int count = 0;
char ch = string1.charAt(i);
for(j = 0; j < string1.length(); j++)
{
if(ch == string1.charAt(j))
count = count + 1;
}
if(count >= most)
{
most = count;
mostOccurrence = ch;
}
}
return mostOccurrence;
}
/*
method accepts three references to string object as arguments.
Call them string1, string2, and string3; when it finds an occurrence
of string2, it replaces it with string 3.
*/
private static String replaceSubstring(String string1, String string2,
String string3)
{
return string1.replaceAll(string2, string3);
}
}
error: cannot find symbol
int numberOfWords = wordCount(string1);
^
symbol: method wordCount(String)
location: class miscellaneousString
error: cannot find symbol
System.out.println("Number of words given string: " + s.numberOfWords);
^
symbol: variable s
location: class miscellaneousString
error: cannot find symbol
String character = arrayToString(charArray);
^
symbol: method arrayToString(char[])
location: class miscellaneousString
error: cannot find symbol
char mostOccurrence = mostFrequent(string1);
^
symbol: method mostFrequent(String)
location: class miscellaneousString
: error: cannot find symbol
String string = replaceSubstring(string1, string2, string3);
^
symbol: method replaceSubstring(String,String,String)
location: class miscellaneousString
5 errors
Explanation / Answer
//here is the modified version of your code and it is giving output according to your requirenment
import java.util.Scanner;
public class miscellaneousString
{
public static void main(String[] args)
{
String string1 = "the dog jumped over the fence";
String string2 = "the";
String string3 = "that";
char[] charArray = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',
'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v','w', 'x',
'y', 'z'};
// wordCount method
int numberOfWords = wordCount(string1);
System.out.println("---------------word count-------------- ");
System.out.println("The string object is : " + string1);
System.out.println("Number of words in the above string is : " + numberOfWords);
// arrayToString method
String character = arrayToString(charArray);
System.out.println(" ---------------Array to string conversion-------------- ");
System.out.print(" The array is :");
for(int i = 0; i < charArray.length; i++)
System.out.print(charArray[i] + " ");
System.out.print(" String from the above character array is : "
+ character);
// mostFrequent method
char mostOccurrence = mostFrequent(string1);
System.out.println(" ---------------calculating most frequent char-------------- ");
System.out.println(" The string is : "
+ string1);
System.out.print("The most occured character in the above string is : "
+ mostOccurrence);
System.out.println(" ---------------modifying string-------------- ");
//replaceSustring method
String string = replaceSubstring(string1, string2, string3);
System.out.println(" The string to be modify = : " + string1);
System.out.println("Replace for: " +string2);
System.out.println("replacement string is: "+string3);
System.out.println("After replacing, the new string is : " + string);
}
int numberOfWords;
/*
method should accept a reference to a string object
as an argument and return the number of words contain
in the object
*/
private static int wordCount(String string1)
{
int number = 0;
for(int i = 0; i < string1.length(); i++)
{
char ch = string1.charAt(i);
if(Character.isWhitespace(ch))
{
number = number + 1;
}
}
return number + 1;
}
/*
method should accept a char array as an argument and convert i to a string object.
The method should return to the string object.
*/
private static String arrayToString(char[] charArray)
{
return String.valueOf(charArray);
}
/*
method accepts a reference to a string object as an argument
and returns the character that occurs the most frequently
in the object.
*/
private static char mostFrequent(String string1)
{
char mostOccurrence = ' ';
int most = 0;
int j;
for(int i = 0; i < string1.length(); i++)
{
int count = 0;
char ch = string1.charAt(i);
for(j = 0; j < string1.length(); j++)
{
if(ch == string1.charAt(j))
count = count + 1;
}
if(count >= most)
{
most = count;
mostOccurrence = ch;
}
}
return mostOccurrence;
}
/*
method accepts three references to string object as arguments.
Call them string1, string2, and string3; when it finds an occurrence
of string2, it replaces it with string 3.
*/
private static String replaceSubstring(String string1, String string2,
String string3)
{
return string1.replaceAll(string2, string3);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.