Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create an ArrayListReview class with one generic type to do the following • Crea

ID: 3747627 • 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.

And please do not forget about <N extends Number> and the other generics please. Generics are required for the lab.

Explanation / Answer

package learning;

import java.util.ArrayList;

import java.util.Random;

public class ArrayListReview {

public static void main(String[] args) {

//Declaring and instantiating the arraylist of type Integers

ArrayList<Integer> al = new ArrayList<Integer> ();

//Adding elements to Arraylist

al.add(1);

al.add(5);

al.add(7);

al.add(23);

al.add(56);

//printing the arraylist

System.out.println(al);

//Adding element at mntioned position and printing the arrayist

al.add(1, 93);

System.out.println(al);

al.add(0, 45);

System.out.println(al);

System.out.println("Printing the fibonacci numbers");

System.out.print(fibonacci(1)+" ");

System.out.print(fibonacci(2)+" ");

System.out.print(fibonacci(3)+" ");

System.out.print(fibonacci(4)+" ");

System.out.print(fibonacci(5)+" ");

System.out.print(fibonacci(6)+" ");

//to check if al is palindrome 1,2,1

ArrayList<Character> alc = new ArrayList<Character>();

alc.add('a');

alc.add('b');

alc.add('a');

System.out.print(" "+alc);

System.out.println(" : "+isPalindrome(alc));

alc.add('b');

System.out.print(" "+alc);

System.out.println(" : "+isPalindrome(alc));

alc = new ArrayList<Character>();

alc.add('a');

alc.add('b');

alc.add('b');

alc.add('a');

System.out.print(" "+alc);

System.out.println(" : "+isPalindrome(alc));

alc = new ArrayList<Character>();

alc.add('a');

alc.add('b');

alc.add('c');

alc.add('d');

alc.add('a');

System.out.print(" "+alc);

System.out.println(" : "+isPalindrome(alc));

//Creating the array of size 1000

int arr[] = new int[1000];

//Using Random to generate Random numbers

Random rand = new Random();

//Adding random elements to array

for(int i=0;i<1000;i++)

arr[i]= rand.nextInt(1000);

//System.out.println("Printing Randomly generated Array");

//printArray(arr);

//To count time Requied to sort the array using Merge Sort in Nano Seconds

long start = System.nanoTime();

sort(arr, 0, arr.length-1);

long end = System.nanoTime();

System.out.println(" Time taken to sort 1000 numbers "+(end-start)+ " Nano Seconds");

}

//method to generate fibonacci nuber based on recurssion

public static int fibonacci(int n)

{

//returning 0 if n is negative

if(n <= 0)

return 0;

//returning 1 if for 1st and 2nd fibonacci numbers else calculating based on recurssion

if(n == 1 || n == 2)

return 1;

else

return fibonacci(n-1)+fibonacci(n-2);

}

//method to check if arraylist is palindrome or not

public static boolean isPalindrome(ArrayList<Character> al)

{

int tail = al.size()-1;

int start=0;

//using 2 indices one from starting and one from ending to compare the elements

while( start != tail && start <= tail)

{

if(al.get(start) != al.get(tail))

break;

//increasing the start index and decreasing the end index

start++;

tail--;

}

if(start >= tail)

return true;

if( start != tail)

return false;

else

return true;

}

// Merges two subarrays of arr[].

// First subarray is arr[l..m]

// Second subarray is arr[m+1..r]

static void merge(int arr[], int l, int m, int r)

{

// Find sizes of two subarrays to be merged

int n1 = m - l + 1;

int n2 = r - m;

/* Create temp arrays */

int L[] = new int [n1];

int R[] = new int [n2];

/*Copy data to temp arrays*/

for (int i=0; i<n1; ++i)

L[i] = arr[l + i];

for (int j=0; j<n2; ++j)

R[j] = arr[m + 1+ j];

/* Merge the temp arrays */

// Initial indexes of first and second subarrays

int i = 0, j = 0;

// Initial index of merged subarry array

int k = l;

while (i < n1 && j < n2)

{

if (L[i] <= R[j])

{

arr[k] = L[i];

i++;

}

else

{

arr[k] = R[j];

j++;

}

k++;

}

/* Copy remaining elements of L[] if any */

while (i < n1)

{

arr[k] = L[i];

i++;

k++;

}

/* Copy remaining elements of R[] if any */

while (j < n2)

{

arr[k] = R[j];

j++;

k++;

}

}

// Main function that sorts arr[l..r] using

// merge()

static void sort(int arr[], int l, int r)

{

if (l < r)

{

// Find the middle point

int m = (l+r)/2;

// Sort first and second halves

sort(arr, l, m);

sort(arr , m+1, r);

// Merge the sorted halves

merge(arr, l, m, r);

}

}

/* A utility function to print array of size n */

static void printArray(int arr[])

{

System.out.println("********************PRINTING********************");

int n = arr.length;

for (int i=0; i<n; i++)

{

System.out.print(arr[i] + " ");

if(i!=0 && i%10 ==0)

System.out.println();

}

System.out.println("********************PRINTING DONE********************");

}

}

SampleOutput

[1, 5, 7, 23, 56]
[1, 93, 5, 7, 23, 56]
[45, 1, 93, 5, 7, 23, 56]
Printing the fibonacci numbers
1 1 2 3 5 8
[a, b, a] : true

[a, b, a, b] : false

[a, b, b, a] : true

[a, b, c, d, a] : false

Time taken to sort 1000 numbers 1011441 Nano Seconds

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote