In: Computer Science
| 
 Hide Assignment Information  | 
|
| Instructions | |
| 
 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), get(int index), isEmpty(); --1.3) (20' pts) CRUD operations we discussed during the class including: add(int index), 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.*;
class mutableArray{
  private int capacity;
  private int array[]=new int[capacity];;
  public mutableArray(int capacity){
    this.capacity=capacity;
  }
  public mutableArray(){
    capacity=1;
  }
  public void setArray(int array[]){
     this.array=array;
  }
   public int[] getArray(){
     return array;
  }
  public int getCapacity(){
    return capacity;
  }
  public void add(int index){
    int newArray[]=Arrays.copyOfRange(array,0,array.length+1);
    int temp=newArray[index];
    newArray[index]=newArray[newArray.length-1];
    newArray[newArray.length-1]=temp;
  }
  public void addLast(){
    int newArr[]=Arrays.copyOfRange(array,0,array.length+1);
  }
  public void addFirst(){
    int newArr[]=Arrays.copyOfRange(array,0,array.length+1);
    for(int i=newArr.length;i>=0;i--){
     if(i!=0){
     newArr[i]=newArr[i-1];}
     else{
     newArr[i]=newArr[newArr.length-1];  
     }
    }
  }
  public void remove(int index){
     for(int i=index;i<array.length-1;i++){
     array[i]=array[i+1];
     }
     int newArr[]=Arrays.copyOfRange(array,0,array.length-1);
  }
  public void removeLast(){
    int newArr[]=Arrays.copyOfRange(array,0,array.length-1);
  }
  public void removeFirst(){
    int newArr[]=Arrays.copyOfRange(array,1,array.length);
  }
  public void removeElement(int target){
    int index=0;
    for(int i=0;i<array.length;i++){
          if(array[i]==target){
              index=i;
          }
      }
    for(int i=index;i<array.length-1;i++){
     array[i]=array[i+1];
    }
     int newArr[]=Arrays.copyOfRange(array,0,array.length-1);
  }
  public void removeAll(int target){
    int newarray[];
  }
  public void swap(int a,int b){
      int indexA=0;
      int indexB=0;
      for(int i=0;i<array.length;i++){
          if(array[i]==a){
              indexA=i;
          }
      }
      for(int i=0;i<array.length;i++){
          if(array[i]==b){
              indexB=i;
          }
      }
      int temp;
      temp=array[indexA];
      array[indexA]=array[indexB];
      array[indexB]=temp;
  }
  public void sort(){
      int temp;
      for(int i=0;i<array.length;i++){
          for(int j=i+1;j<array.length;j++){
              if(array[j]>array[i]){
                  temp=array[i];
                  array[i]=array[j];
                  array[j]=temp;
              }
          }
      }
  }
  
}
//Test all the implemented functions if you want
public class Main {
  public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter the array capacity:");
    int size=sc.nextInt();
    System.out.println("Enter the array elements");
    int arr[]=new int[size];
    for(int i=0;i<size;i++){
        arr[i]=sc.nextInt();
    }
    mutableArray obj=new mutableArray(size);
    obj.setArray(arr);
  }
}
Thank you!, if you have any queries post it below in the comment section i will try my best to resolve your queries and i will add it to my answer if required. Please give upvote if you like.