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
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)
The program should be similar to this example (the one with generics).
import java.util.ArrayList;
public class ArrayListReview
{
ArrayList
LinkedList
PriorityQueue
ArrayListReview()
{
mylist = (ArrayList
myllist = (LinkedList
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 is what 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.
This is what I got so far.
import java.util.ArrayList;
import java.util.Arrays;
public class ArrayListReview
{
// this method is used to generate Fibonacci numbers
public static void fibonacciSeries(int n)
{
System.out.println(" Fibonacci Series: ");
int first = 0;
int second = 1;
// This prints the first two Fibonacci numbers
System.out.print(first + " " + second);
for(int i=3; i<=n; i++)
{
// makes the third int sum of first and second
int third = first + second;
// and then prints it out
System.out.print(" " + third);
// shifts the first number to the second
// and the second number to the third
first = second;
second = third;
}
}
// this method finds out if a list of characters is a palindrome.
public static boolean isPalindrome(ArrayList
{
//len is set to the size number of the list
int len = list.size();
// starts from the bottom with i and from the top is j
for(int i = 0, j = len - 1; i < len/2; i++, j--)
{
// This compares each letters in the for loop.
// If the list i != to list j , it means that the list is not a palindrome
if(list.get(i) != list.get(j))
{
return false;
}
}
return true;
}
// This method merges subarrays into one array
// The First subarray is from left all the way to the middle
// And the second is from the middle with another 1 added and all the way to the right
public static void merge(int arr[], int left[], int right[])
{
int i = 0; // i is the first subarray's index
int j = 0; // j is the second subarray's index
int k = 0; // k is the merged subarray's index
// while loop runs in order to compare stuff from left subarray and from the right
// and then places the elements of the two into arr[]
while(i < left.length && j < right.length)
{
// If the left[i] is <= right[j], left[i] is then placed into arr[]
if(left[i] > right[j])
{
arr[k++] = left[i];
i++;
}
else
{
// If left[i] > right[j], right[j] is then placed into arr[]
arr[k++] = right[j];
j++;
}
}
// If there are any leftovers this loop duplicates element remnants of the left
while(i < left.length)
{
arr[k++] = left[i];
i++;
}
// If there are any leftovers this loop duplicates element remnants of the right
while(j < right.length)
{
arr[k++] = right[j];
j++;
}
}
public static int[] mergeSort(int arr[])
{
// left and right subarray are declared again but in mergeSort
// n is used to find the length of the list
int n = arr.length;
int[] left = new int[n/2];
int[] right = new int[n-n/2];
// if array has none of the elements or one, this returns the primary array
if(n < 2)
{
return arr;
}
// mid is there to get middle index of the arr[] and then break that array into two pieces
int mid = n/2, k = 0;
// for loop is used to inserting elements from arr to the left
for(int i=0; i { left[k++] = arr[i]; } k = 0; // for loop is used to inserting elements from arr from greatest to least for(int i=mid; i { right[k++] = arr[i]; } // this method is recursively called to merge sort the left left = mergeSort(left); // this method is recursively called to merge sort the right right = mergeSort(right); // merge method is called to use arr, left and right merge(arr, left,right); return arr; } public static void insertionSort(int array[], int n) // insertionSort method recursively for numbers greatest to least. { if(n<=1) { return; } insertionSort(array, n-1); int finale = array[n-1]; int k = n-2; while(k >=0 && array[k] < finale) { array[k+1] = array[k]; k--; } array[k+1] = finale; } public static void main(String args[]) //main method { // startTime is used to starting time for loop long startTime = System.nanoTime(); //Creates an array list called a that is filled with numbers, // and inserts new elements into the specified location in the list. 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] + " "); } // fibonnacciSeries method is called to implement the calculation of Fibonacci numbers. fibonacciSeries(50); // This finds out the speed of the program long stopTime = System.nanoTime(); System.out.println(" Time taken = " + (stopTime - startTime) + " nanoseconds"); // Method is called class to check if an array list is a palindrome, // assuming each element in the array list is one character. ArrayList System.out.println(" Result of Palindrome function for 'madam' : " + isPalindrome(chars)); // this Implements mergeSort using recursion. int mergedArr[] = {12, 11, 13, 5, 6, 7}; mergedArr = mergeSort(mergedArr); System.out.println(" Merge sort result: "); for(int i=0; i System.out.print(mergedArr[i] + " "); } int insertionArray[] = {5, 14, 67, 12, 24}; insertionSort(insertionArray, insertionArray.length); System.out.println(" Insertion sort result: "); System.out.print(Arrays.toString(insertionArray)); } } This code is missing generics. I could use some help on fixing this code.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
