Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Given the following classes: StringTools.java: public class StringTools { public static String reverse(String s){ char[] original=s.toCharArray(); char[] reverse = new char[original.length]; for(int i =0; i

Given the following classes:

StringTools.java:

public class StringTools {

public static String reverse(String s){

char[] original=s.toCharArray();

char[] reverse = new char[original.length];

for(int i =0; i

reverse[i] = original[original.length-1-i];

}

return new String(reverse);

}

/**

* Takes in a string containing a first, middle and last name in that order.

* For example Amith Mamidi Reddy to A. M. Reddy.

* If there are not three words in the string then the method will return null

* @param name in First Middle Last format.

* @return name as F. M. Last

*/

public static String initials(String name){

String[] initials = new String[3];

name = name.toUpperCase();

String[] words = name.split(" ");

if (words.length !=3){

return null;

}

for (int i =0; i<3;i++){

initials[i]= Character.toString(words[i].charAt(0));

}

String lastName= Character.toString(words[2].charAt(0))

+words[2].substring(1).toLowerCase();

return initials[0]+ ". "+ initials[1] + ". "+lastName;

}

/**

* Takes a string and returns the most frequent occurring characters

* @param s a string

* @return the most frequent character

*/

public static char mostFrequent(String s){

//Splitting up the string into characters and

char [] chars = s.toCharArray();

int [] frequency = new int[chars.length];

//counting how many time each one occurs

for (int i=0; i

char ch = chars[i];

for(char charecter: chars){

if(charecter==ch)

frequency[i]+=1;

}

}

//Sort frequency of each char in ascending order

int [] min2max = java.util.Arrays.copyOf(frequency, frequency.length);

java.util.Arrays.sort(min2max);

int indexOfMax = 0;

return chars[indexOfMax];

}

/**

* This converts from a binary number to a base ten number. If string has any

* character that is not 1 or 0 it returns -1.

* @param binary a valid binary string

* @return decimal version of the number

*/

public static int binaryToDecimal(String binary){

char[] barray = binary.toCharArray();

char[] rarray= new char [barray.length];

for (int i =0; i

char n = barray[barray.length-1-i];

if( n !='0' && n !='1'){

return -1;

}

rarray[i] = n;

}

int dec=0;

for (int i=0; i

dec+=Math.pow(2, i)*Character.getNumericValue(rarray[i]);

}

return dec;

}

/**

* Takes a string s1, finds a pattern s2 in s1. Replaces all occurrences of s2 in s1 with s3.

* @param s1 This is the string to be modified

* @param s2 This is the pattern to look for

* @param s3 This is what the pattern will be replaced with

* @return the modified string

*/

public static String replaceSubString(String s1, String s2, String s3){

return s1.replaceAll(s2,s3);

}

}

And then the StringToolsTester.java:

import java.util.Scanner;

public class StringToolsTester {

public static void main(String[] args) {

boolean testing = false;

Scanner keyb;

if (testing){

keyb= new Scanner("Hello "

+ "David Marx Johnston "

+ "1001 "

+ "aaaaab "

+ "the dog jumped over the fence "

+ "the "

+ "that ");

} else {

keyb = new Scanner(System.in);

}

//Testing reverse function

String original = getValidString(keyb,"Enter a string to reverse");

String reverse = StringTools.reverse(original);

print(reverse);

//Testing initials function

String initalString = getValidString(keyb,"Enter a name to convert into "

+ "initialized and capitalized name");

print(StringTools.initials(initalString));

//Binary to Decimal conversion

String s = getValidString(keyb,"Enter a binary string to convert");

print(StringTools.binaryToDecimal(s));

//Testing most frequent character

String freq = getValidString(keyb,"Enter a string to find most frequent character");

print(StringTools.mostFrequent(freq));

//String substitution

print("Enter three Strings");

String s1,s2,s3;

s1 = getValidString(keyb,"Enter a String");

s2 = getValidString(keyb,"Enter a pattern");

s3 = getValidString(keyb,"Enter a string to replace the pattern");

String s4 =StringTools.replaceSubString(s1, s2, s3);

print("Your new string is:");

print(s4);

}

/**

* Prompts the user for a string, and keeps asking until the user

* provides clean string.

* @param in

* @param prompt what to prompt

* @return a valid

*/

public static String getValidString(Scanner in,String prompt){

String s="";

do{

print(prompt);

s=in.nextLine();

}while(s == null || s.equals(""));

return s;

}

/**

* The print method does as it says, it prints the result.

* @param s prints the String result.

*/

public static void print(String s){

System.out.println(s);

}

/**

*

* @param o

*/

public static void print(Object o){

print(o.toString());

}

}

Please fix the mostFrequent() method. When user enters an input, say hello, the program, must print l as the most frequent character. When there is a tie, say helloo, then the program must print, the last character that was checked in ascending order. So when helloo, is entered, the program should return o as the most frequent as it was the last one checked. This is what I tried to do in this part:

int [] min2max = java.util.Arrays.copyOf(frequency, frequency.length);

java.util.Arrays.sort(min2max);

int indexOfMax = 0;

return chars[indexOfMax]

But as you can see I failed, and would like your help in fixing this.

ALSO, AS YOU CAN SEE, THIS IS BASIC COMPUTER SCIENCE II COURSE. PLEASE DONT USE HASHMAPS, STRING TOKENIZERS, STRING BUFFERS, STRING TOOLS OR APACHE COMMONS OR THINGS LIKE THOSE. STAY BASIC SIMILAR TO WHAT I HAVE. THANKS!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Transaction Processing

Authors: Philip M. Lewis, Arthur Bernstein, Michael Kifer

1st Edition

0201708728, 978-0201708721

More Books

Students also viewed these Databases questions