A. Consider a problem to find the student who had the highest GPA for the 2020/2021 academic year. i. Explain your choice of either a search or a sorting algorithm to solve this problem.
ii. Write the algorithm for your implementation. [
B. Consider the analogy of a pile of books on a table. Using a diagram, explain the basic operations involved in adding a book to the pile and removing a book from the pile.
C. Use the code below to answer the questions that follow:
public class CS204 { public static int linearSearch(int[] data, int target) {
for (int i = 0; i < data; i++) {
if (target == data[i]){ return i; 5
}
}
return -1;
}
public void main(String[] args) {
int[] data = {3, 14, 7, 22, 45, 12, 19, 42, 6};
System.out.println("Search for 7: " +
linearSearch(7));
}
}
i. Why is method linearSearch declared static?
ii. Identify and resolve the errors in the code.
public class CS204 {
// A.On this case,on my opinion,will be collection ArrayList,it let collect a
// lot of students grouped by indexes,quick sort students by special condition
// and find some student by special request.
// B.For soling this problem,on my point of view,will be collection
// "Stack",'cause normally you put book and removing a book from the pile from
// up to down.
public void main(String[] args) {
int[] data = { 3, 14, 7, 22, 45, 12, 19, 42, 6 };
System.out.println("Search for 7: " +
linearSearch(data, 7));
}
public static int linearSearch(int[] data, int target) {
for (int i = 0; i < data.length; i++) {
if (target == data[i]) {
return i;
}
}
return -1;
}
// i. method linearSearch() is static because you may invoke him without class
// instantiation
// ii.all mistakes are catched and fixed.
}
Comments
Leave a comment