Create an ArrayListReview class to do the following (JAVA) -Creates an array lis
ID: 3745500 • Letter: C
Question
Create an ArrayListReview class to do the following (JAVA)
-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.
-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 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. (bonus)
Explanation / Answer
Code:
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
Will submit the Bonus in Comments as question is very long
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.