In: Computer Science
In this assignment, you will be practicing the Java OOP skills we've learned this week in implementing a customized mutable Array class. Please read the following requirements carefully and then implement your own customized Array class:
=== 1) Basic (100' pts total) ===
Your Array class must provide the following features:
--1.1) (20' pts) Two constructors. One takes the input of the initialized capacity (int) and create the underlying array with that capacity; the other takes no input and creates an array that can hold a single element by default (that is, init capacity == 1);
--1.2) (20' pts) Getters and setters and basic methods we discussed during the class including: getCapacity(), getSize(), set(int index, E val), get(int index), isEmpty();
--1.3) (20' pts) CRUD operations we discussed during the class including: add(int index, E val), addLast(), addFirst(), remove(int index), removeLast(), removeFirst(), removeElement(int target), removeAll(int target);
--1.4) (20' pts) Supports generic types;
--1.5) (20' pts) Mutable, that is when add element exceeding its capacity, it can resize to accommodate the change.
=== 2) Bonus (20' pts total) ===
2.1) (10' pts) Implement a (private or public) swap(int a, int b) method that swaps the two elements in the index a and b if a and b are both admissible; also a public reverse() method that reverses the order of stored elements in-place (means, no additional spaces are allocated).
2.2) (10' pts) A public void sort() method sorts the stored elements in ascending order inO(nlog(n)) time.
[hint]: Quicksort or Merge sort
import java.util.*;
import java.lang.*;
// Generic class
public class Array<T>{
static Object[] array = null; // Generic array
static int capacity = 0; // declare variable
// Class constructor
Array(int capacity){
this.capacity = capacity;
array = new Object[capacity];
}
// Class constructor
Array() {
this.capacity = 1;
array = new Object[1];
}
// getCapacity method
static int getCapacity() {
return capacity;
}
// getSize method
static int getSize() {
return array.length;
}
// set method
static void set(int index, Object val) {
array[index] = val;
}
// get method
static Object get(int index) {
return array[index];
}
isEmpty method
static boolean isEmpty() {
if(array.length == 0) return
true;
else return false;
}
// swap method
static void swap(int a, int b) {
Object temp;
temp = array[a];
array[a] = array[b];
array[b] = temp;
}
// reverse method
static void reverse() {
Arrays.sort(array,Collections.reverseOrder());
}
// sort method
static void sort() {
Arrays.sort(array);
}
// insertArray method
static void insertArray() {
for(int i=0; i<capacity; i++)
{
array[i] =
i;
}
}
public static void main(String[] args) {
Array obj = new Array(20); //
Create Array class object
insertArray(); // Call insertArray
method
System.out.println(obj.getCapacity()); // Call getCapacity
method
System.out.println(obj.getSize());
// Call getSize method
obj.set(4,2); // Call set
method
System.out.println(obj.get(4)); //
Call get method
System.out.println(obj.isEmpty());
// Call isEmpty method
obj.swap(2,4); // Call swap
method
obj.reverse(); // Call reverse
method
obj.sort(); // Call sort method
}
}