You may NOT use Collections, ArrayList, Map, or anything else that uses generics
ID: 3674525 • Letter: Y
Question
You may NOT use Collections, ArrayList, Map, or anything else that uses generics <E>
A. Write a method called mostCommonInt that accepts an array of integers as its only parameter, and returns the int that occurs most frequently. Break ties by returning the lower value For example, {-1,2,-2,3,4,4,5,5} would return 4 as the most common int.
B. Write mostCommonDouble that accepts an array of doubles, and returns the double that occurs most frequently. Consider any double values that are within 0.5% of each other to be equal to their average value. So 2.4 and 2.5 are not equal because the difference is 4%, well over 1/2 percent. But 2.34 and 2.35 are considered equal to the average 2.345 which is only 0.2% different from either value. For example, {1.,2.,2.,3.,4.,4.} would return 2.0 as the most common double. And passing in the array {1.,2.34,2.35,2.4,2.5,4.2,42} would return 2.345 as per calculations above.
C. Write a method called charCounter that accepts a String as its only argument, and returns an integer array ( int[128] ) representing the count of each ASCII character within that String (recall there are 128 original ASCII characters). For example a call to charCounter("I think, therefore I am") will return an int array with index 97 (ASCII for 'a') will be 1 (only 1 'a'), and at index 101 will be a 3 ('e' is ASCII 101, and there are 3 e's in that String), while most of the array will be zeroes as most characters are not present. Other possible testing string are contained in something like Dumb.txt
D. Write a method called mostCommonChar that that accepts a String representing as its only argument, and returns an integer which is equal to the count of the most common char in the String.
Explanation / Answer
A).
public static int mostCommonInt(int arr[]){
int max_element = arr[0]; // initializing max occuring element with first
int count =1;
int max_count =0;
for(int i=0; i<arr.length; i++){// for each element, count number of occurence
count = 0;
for(int j= 0; i<arr.length; i++){
if(arr[j] == arr[i])// if current element = to picked element
count++;
}
if(count > max_count)//checking for max
max_element = arr[i];
}
return max_element;
}
B).
public static double mostCommonDouble(double arr[]){
double max_element = arr[0]; // initializing max occuring element with first
int count =1;
int max_count =0;
for(int i=0; i<arr.length; i++){// for each element, count number of occurence
count = 0;
for(int j= 0; i<arr.length; i++){
double avg = (arr[i]+arr[j])/2;// finding middle point of picked and current element
// double values that are within 0.5% of each other to be equal to their average value
if((Math.abs(avg - arr[i])==0.005) && (Math.abs(avg - arr[j])==0.005))
count++;
}
if(count > max_count)//checking for max
max_element = arr[i];
}
return max_element;
}
C).
public static int[] charCounter(String str){
int[] counter = new int[str.length()];
for(int i=0; i<str.length(); i++){
char c = str.charAt(i);
counter[c]++;
}
return counter;
}
D).
public static char mostCommonChar(String str){
char max_element = str.charAt(0); // initializing max occuring element with first
int count =1;
int max_count =0;
for(int i=0; i<str.length(); i++){// for each element, count number of occurence
count = 0;
for(int j= 0; i<str.length(); i++){
if(str.charAt(i) == str.charAt(j))
count++;
}
if(count > max_count)//checking for max
max_element = str.charAt(i);
}
return max_element;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.