Create an ArrayListReview class with one generic type to do the following • Crea
ID: 3746944 • Letter: C
Question
Create an ArrayListReview class with one generic type to do the following
• Creates an array list filled with numbers, and inserts new elements into the specified location in the list.
• Create a method inside the class to implement the calculation of Fibonacci numbers. Use System.nanoTime to find out how much time it takes to get fab(50).
• Create a method inside the class to check if an array list is a palindrome, assuming each element in the array list is one character.
• Implement insertionSort either recursively or non-recursively.
• Implement mergeSort using recursion.
• Use System.nanoTime() to find out the speed of your program.
Bonus:
Designing Generic Matrix Classes -- This program includes a generic class for matrix arithmetic. This class
implements matrix addition and multiplication common for all types of matrices, (including String).
(bonus)
Okay so basically this is how far I got.
import java.util.ArrayList;
public class ArrayListReview<N extends Number>
{
ArrayList <N> mylist = new ArrayList<>;();
LinkedList<N> myllist = new LinkedList<>();
PriorityQueue <N> mypq = new PriorityQueue<>();
ArrayListReview()
{
mylist = (ArrayList<N>) new ArrayList<>(Arrays.asList(new Integer[] {4... ????????????
myllist = (LinkedList<N>) new LinkedList<>(Arrays.asList(new Integer[] {...?????????
mypq.offer((N) new Integer(45));
mypq.offer((N) new Integer(53));
mypq.offer((N) new Integer(10));
...?????????
The rest I really do not know. but I believe that iswhat my teacher wants me to do just like the program up there.I put the ... and the ?s because there are things I do not know there and that there are some code missing.
Explanation / Answer
Answer :
import java.util.ArrayList;
import java.util.Arrays;
public class ArrayListReview {
public static void fibonacciSeries(int n) {
System.out.println(" Fibonacci Series: ");
int first = 0;
int second = 1;
System.out.print(first + " " + second);
for(int i=3; i<=n; i++) {
int third = first + second;
System.out.print(" " + third);
first = second;
second = third;
}
}
public static boolean isPalindrome(ArrayList<Character> list)
{
int len = list.size();
for(int i = 0, j = len - 1; i < len/2; i++, j--)
{
if(list.get(i) != list.get(j))
return false;
}
return true;
}
public static void merge(int arr[], int left[], int right[]) {
int i = 0;
int j = 0;
int k = 0;
while(i < left.length && j < right.length) {
if(left[i] < right[j]) {
arr[k++] = left[i];
i++;
} else {
arr[k++] = right[j];
j++;
}
}
while(i < left.length) {
arr[k++] = left[i];
i++;
}
while(j < right.length) {
arr[k++] = right[j];
j++;
}
}
public static int[] mergeSort(int arr[]) {
int n = arr.length;
int[] left = new int[n/2];
int[] right = new int[n-n/2];
if( n < 2) {
return arr;
}
int mid = n/2, k = 0;
// Insert the elements from A[0....mid-1] to left[]
for(int i=0; i<mid; i++) {
left[k++] = arr[i];
}
k = 0;
for(int i=mid; i<n; i++) {
right[k++] = arr[i];
}
left = mergeSort(left);
right = mergeSort(right);
merge(arr, left,right);
return arr;
}
public static void main(String args[]) {
long startTime = System.nanoTime();
int a[] = new int[5];
a[2] = 10;
a[0] = 15;
a[3] = 67;
a[1] = 34;
a[4] = 5;
System.out.println(" Content of the array list is: ");
for(int i=0; i<5; i++) {
System.out.print(a[i] + " ");
}
fibonacciSeries(10);
ArrayList<Character> chars = new ArrayList<>(Arrays.asList('m', 'a', 'd', 'a', 'm'));
System.out.println(" Result of Palindrome function for 'madam' : " + isPalindrome(chars));
int mergedArr[] = {12, 11, 13, 5, 6, 7};
mergedArr = mergeSort(mergedArr);
System.out.println(" Merge sort result: ");
for(int i=0; i<mergedArr.length; i++) {
System.out.print(mergedArr[i] + " ");
}
long stopTime = System.nanoTime();
System.out.println(" Total time taken = " + (stopTime - startTime) + " nanoseconds");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.