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<s.length(); 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<frequency.length;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<barray.length;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<rarray.length; 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\n"
+ "David Marx Johnston\n"
+ "1001\n"
+ "aaaaab\n"
+ "the dog jumped over the fence\n"
+ "the\n"
+ "that\n");
} 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!
//mostFrequent method
public static char mostFrequent(String str) {
// TODO Auto-generated method stub
int count[] = new int[256];
//Integer array for all the ASCII character(total 256 ASCII characters)
for (int i=0; i<str.length(); i++){
count[str.charAt(i)]++;
}
int maximum = -1; // Initialize max count
char result = ' '; // Initialize result
// Traversing through the string and maintaining
// the count of each character
for (int i = str.length()-1; i >= 0; i--) {
if (maximum < count[str.charAt(i)]) {
maximum = count[str.charAt(i)];
result = str.charAt(i);
}
}
return result;
}
//Main Method
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "helloo";
char ch = mostFrequent(str);
System.out.println("Most Occuring Character in "+ str +" is "+ch);
}
Output:
Given the following classes: StringTools.java: public class StringTools { public static String reverse(String s){ char[] original=s.toCharArray();...
This is Crypto Manager blank public class CryptoManager { private static final char LOWER_BOUND = ' '; private static final char UPPER_BOUND = '_'; private static final int RANGE = UPPER_BOUND - LOWER_BOUND + 1; /** * This method determines if a string is within the allowable bounds of ASCII codes * according to the LOWER_BOUND and UPPER_BOUND characters * @param plainText a string to be encrypted, if it is within the allowable bounds * @return true if all characters...
import java.util.Scanner; public class StudentClient { public static void main(String[] args) { Student s1 = new Student(); Student s2 = new Student("Smith", "123-45-6789", 3.2); Student s3 = new Student("Jones", "987-65-4321", 3.7); System.out.println("The name of student #1 is "); System.out.println("The social security number of student #1 is " + s1.toString()); System.out.println("Student #2 is " + s2); System.out.println("the name of student #3 is " + s3.getName()); System.out.println("The social security number...
in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame { /** * @param args the command line arguments */ public static void main(String[] args) { Scanner input=new Scanner(System.in); Scanner input2=new Scanner(System.in); UNOCard c=new UNOCard (); UNOCard D=new UNOCard (); Queue Q=new Queue(); listplayer ll=new listplayer(); System.out.println("Enter Players Name :\n Click STOP To Start Game.."); String Name = input.nextLine();...
Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp { public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...
JAVA public static String generatePassword(String gp) { String password=""; boolean b= false; for (int i =0;i { char ch = gp.charAt(i); if (i == 0) { password += Character.toUpperCase(ch); } if (ch == 'S' || ch == 's') ...
Here is the indexOf method that I wrote: public static int indexOf(char[] arr, char ch) { if(arr == null || arr.length == 0) { return -1; } for (int i = 0; i < arr.length; i++) { if(arr[i] == ch) { return i; } } return -1; ...
You will need to think about problem solving. There are several multi-step activities. Design, compile and run a single Java program, StringEx.java, to accomplish all of the following tasks. Add one part at a time and test before trying the next one. The program can just include a main method, or it is neater to split things into separate methods (all static void, with names like showLength, sentenceType, lastFirst1, lastFirst), and have main call all the ones you have written...
How can i print a diamond implementing these methods public static void printNChars(int n, char c)). This method will print n times the character c in a row, public static void printDiamond(int size, char edgeChar, char fillChar). This method will call printNChars() method to print the shape. It will use the edgeChar to print the sides and the fillChar to fill the interior of the shape. The shape will have a height and a width of the given size. public...
public static int countCharacter(String str, char c) { // This recursive method takes a String and a char as parameters and // returns the number of times the char appears in the String. You may // use the function charAt(int i) described below to test if a single // character of the input String matches the input char. // For example, countCharacter(“bobbie”, ‘b’) would return back 3, while // countCharacter(“xyzzy”, ‘y’) would return back 2. // Must be a RECURSIVE...
Java class quiz need help~ This is the Backwards.java code. public class Backwards { /** * Program starts with this method. * * @param args A String to be printed backwards */ public static void main(String[] args) { if (args.length == 0) { System.out.println("ERROR: Enter a String on commandline."); } else { String word = args[0]; String backwards = iterativeBack(word); // A (return address) System.out.println("Iterative solution: " + backwards); backwards = recursiveBack(word); // B (return address) System.out.println("\n\nRecursive solution: " +...