public String halfAndHalf(String str) Given a String argument, return a new stri
ID: 3558376 • Letter: P
Question
public String halfAndHalf(String str)
Given a String argument, return a new string that has the upper case version of the first half of the argument at the end and the lower case version of the last half of the argument at the beginning. If there are an odd number of letters, there should be one more lower case letters at the beginning of the result. Assume the String argument's length >= 2.
Example:
halfAndHalf("x2y4abcd") returns "abcdX2Y4"
halfAndHalf("AbcDef") returns "defABC"
halfAndHalf("Hello") returns "lloHE"
.
.
public int charPairs(String str)
Return the number of times two consecutive characters occur in the given string.
Example:
charPairs("") returns 0
charPairs("H") returns 0
charPairs("abc") returns 0
charPairs("aabbcc") returns 3
charPairs("!!!") returns 2
charPairs("mmmm") returns 3
charPairs("mmOmm") returns 2
.
.
public int sumInScanner(Scanner stream)
Given a Scanner constructed with a String of valid integers, return the sum of all integers.
.
.
public int howMany(String[] array, String valueToFind)
Complete method howMany to return the number of elements in an array of Strings that equals valueToFind. The array may be empty.
Example:
howMany( {"A", "a", "A", "a" }, "A" ) returns 2
howMany( {"And", "there", "goes", "another"}, "another" ) returns 1
howMany( {"And", "there", "goes", "another"}, "Not Here" ) returns 0
.
.
public void selectionSort(int[] array)
Modify array in method sort so the associated int[] argument is sorted in ascending order. Sort the array yourself; do not use an external class to sort it.
.
.
These questions were spent at least 2 hours (each), but I couldn't solve it. You don't need println. All I need to do is, pass the jUnit test to see if method really works. Thank you for your hard work.
Explanation / Answer
please rate
public String halfAndHalf(String str){
int length = str.length();
return str.substring(length/2).toLowerCase()+str.substring(0, length/2).toUpperCase();
}
public int charPairs(String str){
int result = 0;
for(int i=0;i<str.length()-1;i++){
if(str.charAt(i)==str.charAt(i+1))
result += 1;
}
return result;
}
public int sumInScanner(Scanner stream){
int result = 0;
while(stream.hasNextInt()){
result += stream.nextInt();
}
return result;
}
public int howMany(String[] array,String valueToFind){
int result = 0;
for(String s:array){
if(valueToFind.equals(s))
result += 1;
}
return result;
}
public void selectionSort(int[] array){
int min = Integer.MIN_VALUE;
int minPos = 0;
for(int i=0;i<array.length;i++){
min = array[i];
minPos = i;
for(int j=i+1;j<array.length;j++){
if(array[j]<min){
min = array[j];
minPos = j;
}
}
array[minPos] = array[i];
array[i] = min;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.