[JAVA BASIC] 1. Write a complete Java method called computeAverage that takes an
ID: 666214 • Letter: #
Question
[JAVA BASIC]
1. Write a complete Java method called computeAverage that takes an array of doubles as a parameter and returns their average as a double. Use a while loop to process the array. (3 points)
2. Write a complete Java method called sliceString that takes a String as a parameter and returns an ArrayList of Character data with each char in the String represented as a separate element in the ArrayList. Use a for loop to process the string. For example, if the method is passed the string “cat”, it returns an ArrayList of 3 Characters: c, a, t. (3 points)
3. Write a complete Java method called printTable that takes a 2-dimensional array of Strings (remember: this is a table, not a list) as a parameter and prints the contents of the array to the command line. Separate the strings on a row using tabs. (Hint: use " " to include a tab in your output) Each row should end with a new line. Below is some example output. (3 points)
This is a table
with three rows and
four cols of data
Explanation / Answer
1.
static double computeAverage(double array[]){
double avg = 0;
for(int i = 0; i < array.length; i++){
avg += array[i];
}
avg /= array.length;
return avg;
}
2.
static ArrayList sliceString(String str){
ArrayList al = new ArrayList();
for(int i = 0; i < str.length(); i++){
al.add(str.charAt(i));
}
return al;
}
3.
static void printTable(String arr[][]){
for(int i = 0; i < arr.length; i++){
for(int j = 0; j < arr[i].length; j++){
System.out.print(arr[i] + " ");
}
System.out.println();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.