Write a class of static methods that accomplish the followin
Solution
package com.tt.tester;
public class StringTools {
public static String reverse(String name) {
StringBuilder builder = new StringBuilder();
builder.append(name);
builder = builder.reverse();
String reverseString = builder.toString();
return reverseString;
}
public static int binaryToDecimal(String s) {
int bin = Integer.parseInt(s);
int dec = 0;
int power = 0;
while (true) {
if (bin == 0) {
break;
} else {
int tmp = bin % 10;
dec += tmp * Math.pow(2, power);
bin = bin / 10;
power++;
}
}
return dec;
}
public static String[] initials(String s) {
int count = 0;
String[] sstring = s.split(\"\\\\s+\");
for (String s1 : sstring) {
count++;
}
if (count == 3) {
return sstring;
} else
return null;
}
public static String mostFrequent(String s) {
char[] charray = s.toCharArray();
int maximumCount = 1;
char maximumChar = charray[0];
for (int i = 0, k = 0; i < s.length() - 1; i = k) {
int counts = 1;
while (++k < s.length() && charray[i] == charray[k]) {
counts++;
}
if (counts > maximumCount) {
maximumCount = counts;
maximumChar = charray[i];
}
}
return (maximumChar + \" = \" + maximumCount);
}
}
package com.tt.tester;
public class StringTester {
public static void main(String[] args) {
StringTools tools = new StringTools();
String r = tools.reverse(\"mark\");
System.out.println(\"the reverse of a string is \" + r);
int r1 = tools.binaryToDecimal(\"10011\");
System.out.println(\"the decimal is \" + r1);
String[] s = tools.initials(\"ramu ramum ramesh\");
if (s != null) {
for (String string : s) {
System.out.println(\"the string are \" + string);
}
}
String s2 = tools.mostFrequent(\"ramusramusr\");
System.out.println(s2.toString());
}
}
output
the reverse of a string is kram
the decimal is 19
the string are ramu
the string are ramum
the string are ramesh
r = 1

