Question: Given two java classes, Explain WHY and provide a solution. Note that your solutions must change the sum methods method signature and not change anything
Given two java classes, Explain WHY and provide a solution. Note that your solutions must change the sum methods method signature and not change anything in the main method. Also, What is the upper bound of the above method?
class EmptyListException extends RuntimeException { } class Node { int data; Node next; public Node(int data){ this.data = data; } } class List { Node head; public void add(int data){ Node temp = new Node(data); Node current = head; if (head == null) { head = temp; } else { while(current.next != null) { current = current.next; } current.next = temp; } } public int getLastData() throws EmptyListException{ if (head == null) throw new EmptyListException(); Node current = head; while(current.next != null) current = current.next; return current.data; } }
import java.util.ArrayList; public class Quiz9Question2 { public static double sum(ArrayList list) { double total = 0; // initialize total for (Number element : list) total += element.doubleValue(); return total; } public static void main(String[] args) { for (Integer element : integerList) integerList.add(element); sum(integerList);//ERROR!!!!!!!!!! } }
Lastly,
public static int[] bubbleSort(int[] x) {
int n = x.length;
for (int j = 1; j < n; j++) {
for (int i = 0; i < n - j; i++) {
if (x[i] > x[i + 1]) {
int temp = x[i];
x[i] = x[i + 1];
x[i + 1] = temp;
}
}
}
return x;
}
Give the generic version for the above method. Note that some code will need to be adjusted as the sortable objects must be Comparable and thus use compareTo() method.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
