Question

In: Computer Science

Hide Assignment Information Instructions In this assignment, you will be practicing the Java OOP skills we've...

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

Solutions

Expert Solution

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.


Related Solutions

In this assignment, you will be practicing the Java OOP skills we've learned this week in...
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...
In this assignment, you will be practicing the Java OOP skills we've learned this week in...
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...
Hide Assignment Information Instructions The following information is available to reconcile Cloy Company's book balance of...
Hide Assignment Information Instructions The following information is available to reconcile Cloy Company's book balance of cash with its bank statement cash balance as of March 31, 2019. The March 31, 2019 cash balance according to the accounting records is $58,542, and the bank statement cash balance for that date is $68,047. a. The bank erroneously cleared a $395 check against the account in June that was not issued by Cloy. The check documentation included with the bank statement indicates...
Hide Folder Information Turnitin® This assignment will be submitted to Turnitin®. Instructions The Final Project for...
Hide Folder Information Turnitin® This assignment will be submitted to Turnitin®. Instructions The Final Project for this course is the creation of an employee database. This assignment will give you some insight into how Visual Basic programming can be useful in the real world.   As you work on this application, try to make it presentable like an application you would want to use if you were using an employee database application. Feel free to incorporate pictures to make it appeal...
Hide Folder Information Instructions You are the Chief Financial Officer for Bio Innovations, a private company...
Hide Folder Information Instructions You are the Chief Financial Officer for Bio Innovations, a private company operating since 1993. Bio develops and markets cancer drugs, and recently became a cloud provider for medical centers. You recently joined the company and have been encouraging the founder and executive chair, Mary Cooper, to take the company public. Mary is reluctant to do that primarily because of the nature of the business and her belief that GAAP financial statements place the company in...
Program in Java using Inheritence The purpose of this assignment is to practice OOP programming covering...
Program in Java using Inheritence The purpose of this assignment is to practice OOP programming covering Inheritance. Core Level Requirements (up to 6 marks) The scenario for this assignment is to design an online shopping system for a local supermarket (e.g., Europa Foods Supermarket or Wang Long Oriental Supermarket). The assignment is mostly concentrated on the product registration system. Design and draw a UML diagram, and write the code for the following classes: The first product category is a fresh...
Create java class with name MyClass1 Instructions for MyClass1 For this portion of the assignment you...
Create java class with name MyClass1 Instructions for MyClass1 For this portion of the assignment you will need to create a java class of your own to model some objects in the real world or your own imagination. your classes should have at least three instances variables, accessor and mutator methods for each of those instance variables, one constructor method of any type, as well as an overridden toString method that will display every field value as part of a...
Hide Folder Information Instructions What is The Importance Of Analyzing Accounts Receivable. Accounts Receivable Financing :...
Hide Folder Information Instructions What is The Importance Of Analyzing Accounts Receivable. Accounts Receivable Financing : An alternative to bank financing for your small business DISCUSS THE ABOVE STATEMENT BY TAKING EXAMPLE OF BUSINESS ORGANIZATION OF YOUR CHOICE.
you are practicing your basketball skills by shooting some hoops. You shoot a basketball with initial...
you are practicing your basketball skills by shooting some hoops. You shoot a basketball with initial velocity vo= 10.2 m/s @ θo = 34.0° above the horizontal. Doing so causes the basketball to first go higher than the net, reach a maximal height, and then come back down through the net from above. Find the time it takes the basketball to reach a maximal height (call this height tmax)b.What is the ball’s vertical velocity at tmax?c.What is the ball’s total...
C++(OOP)plz SOLVE THE PROBLEM ACCORDING TO INSTRUCTIONS Imagine you are on a trip around Europe and...
C++(OOP)plz SOLVE THE PROBLEM ACCORDING TO INSTRUCTIONS Imagine you are on a trip around Europe and have collected currencies from the different countries that you have travelled. You are to write a simple money counting program. Your program will be able to deal in the following three currencies a) Pakistani Rupee b) Turkish Lira c) Pound Sterling  You must make a class called Currency. Create its data members and any member functions you require. Your class should be written...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT