In: Computer Science
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
Write the code using JAVA OPP skills .Please read the following requirements carefully and then implement your own customized Array class:
Please provide me with the written form of the code along with the screen shots.
Thank you.
Ans)
Java program
Code:
import java.util.*;
public class ArrayClass {
private int capacity;
private int[] arr;
ArrayClass(int cap){
//initializing the array with capacity
this.capacity = cap;
this.arr = new int[capacity];
}
ArrayClass() {
//initializing array with 1 capacity
this(1);
}
//defining the getter setter and basic methods
// getCapacity method which return the capacity
public int getCapacity() {
return this.capacity;
}
// getSize method which return the size of array
public int getSize() {
int size = this.arr.length;
return size;
}
//set method which set an element with index
public void set(int index, int value) {
this.arr[index] = value;
}
//get method for fetch the elements for given index
public int get(int index) {
return this.arr[index];
}
// boolen method to check array if it is null or not
public boolean isEmpty() {
if(this.arr == null || this.arr.length == 0)
return true;
else
return false;
}
//Defining the crud operations
public void add(int index, int value) {
if(index > this.arr.length-1) {
//when index size greater than array length then resizing the array
resize();
}
this.arr[index] = value;
}
public void addLast(int value) {
this.arr[this.arr.length-1] = value;
}
public void addFirst(int value) {
this.arr[0] = value;
}
public void remove(int index) {
//checking if given index is outside of range or not
if(this.arr == null || index < 0 || index > this.arr.length) {
return ;
}
//to removing an element from array first create an array with less 1 size
int [] temp = new int[this.arr.length-1];
for(int i = 0, j = 0; i < this.arr.length; i++) {
if(i == index)
continue;
temp[j++] = this.arr[i];
}
//re-refrencing the arr with temp
this.arr = temp;
}
public void removeLast() {
//to removing an element from array first create an array with less 1 size
int [] temp = new int[this.arr.length-1];
for(int i = 0; i < this.arr.length-1; i++) {
temp[i] = this.arr[i];
}
//re-refrencing the arr with temp
this.arr = temp;
}
public void removeFirst() {
//to removing an element from array first create an array with less 1 size
int [] temp = new int[this.arr.length-1];
for(int i = 1; i < this.arr.length; i++) {
temp[i] = this.arr[i];
}
//re-refrencing the arr with temp
this.arr = temp;
}
public void removeElement(int target) {
//to removing an element from array first create an array with less 1 size
int [] temp = new int[this.arr.length-1];
for(int i = 0, j = 0; i < this.arr.length; i++) {
if(this.arr[i] == target)
continue;
temp[j++] = this.arr[i];
}
//re-refrencing the arr with temp
this.arr = temp;
}
public void removeAll() {
this.arr = new int[0];
}
// resize method call inside add method whenever adding element index is greater than size of array
public void resize() {
this.capacity = this.capacity*2;
int [] temp = new int[this.capacity];
for(int i = 0; i<this.arr.length; i++) {
temp[i] = this.arr[i];
}
this.arr = temp;
}
//swap method
public void swap(int a, int b) {
int temp = this.arr[a];
this.arr[a] = this.arr[b];
this.arr[b] = temp;
}
// sort method for storing element in ascending order
public void sort() {
Arrays.sort(this.arr);
}
//reverse method to store element in reverse order
public void reverse() {
int temp [] = new int[this.arr.length];
for(int j = this.arr.length-1, i = 0; j >= 0; j--, i++) {
temp[i] = this.arr[j];
}
this.arr = temp;
}
public static void main(String [] arg) {
//calling a parameterized constructor
System.out.println("Call for parameterized constructor: \n");
ArrayClass obj1 = new ArrayClass(5);
//assigning the value inside array using add method
obj1.add(0, 2);
obj1.add(1, 3);
obj1.add(2, 1);
obj1.add(3, 5);
obj1.add(4, 4);
//printing the assign element using get method;
System.out.println("Printing the elements: ");
for(int i = 0; i < 5; i++) {
int value = obj1.get(i);
System.out.print(" " + value);
}
//finding the capacity using getCapacity method
int cap = obj1.getCapacity();
System.out.println("\nCapacity of array: = "+ cap);
//finding the size of array
System.out.println("Size of array: = " + obj1.getSize());
//set an element inside array in a particular index
obj1.set(3, 8);
//getting element using get method
System.out.println("Element at index 3 = " + obj1.get(3));
//calling addLast method to add element at last
obj1.addLast(6);
// calling addFirst method
obj1.addFirst(1);
//checking if array is empty or not
boolean flag = obj1.isEmpty();
if(flag == true)
System.out.println("Array is empty ");
else
System.out.println("Array is not empty ");
//printing the assign element using get method;
System.out.println("Printing the elements: ");
for(int i = 0; i < 5; i++) {
int value = obj1.get(i);
System.out.print(" "+ value);
}
//removing an element using remove method
obj1.remove(2);
//printing the array after removing element using remove method;
System.out.println("\nPrinting the elements after removing: ");
for(int i = 0; i < 4; i++) {
int value = obj1.get(i);
System.out.print(" " + value);
}
//swapping element using swap method
obj1.swap(2, 3);
//printing element after swapping
System.out.println("\nPrinting the elements after swapping: ");
for(int i = 0; i < 4; i++) {
int value = obj1.get(i);
System.out.print(" " + value);
}
//sorting the array using sort method
obj1.sort();
//printing the array after sorting using sort method;
System.out.println("\nPrinting the elements after sorting: ");
for(int i = 0; i < 4; i++) {
int value = obj1.get(i);
System.out.print(" " + value);
}
//reversing the stored element using reverse method
obj1.reverse();
//printing the array after reversing element using reverse method;
System.out.println("\nPrinting the elements after reversing: ");
for(int i = 0; i < 4; i++) {
int value = obj1.get(i);
System.out.print(" " + value);
}
//calling non parameterized constructor or initializing array with one capacity
System.out.println();
System.out.println("\nCall for default constructor: ");
ArrayClass obj2 = new ArrayClass();
System.out.println("Capacity of singular array: = " + obj2.getCapacity());
System.out.println("Size of singular array: = " + obj2.getSize());
obj2.add(0, 5);
System.out.println("Element of singular array: = " + obj2.get(0));
}
}
Above java compile and execute and get output in below screen shot once check
Output
if your satisfy above answer please give positive rating or like?
please don't dislike
Thank you!