Question

In: Computer Science

Your Array class must provide the following features: --1.1) (20' pts) Two constructors. One takes the...

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.

Solutions

Expert Solution

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!


Related Solutions

Java programming! Implement a static stack class of char. Your class should include two constructors. One...
Java programming! Implement a static stack class of char. Your class should include two constructors. One (no parameters) sets the size of the stack to 10. The other constructor accepts a single parameter specifying the desired size of the stack a push and pop operator an isEmpty and isFull method . Both return Booleans indicating the status of the stack Change this cods according to instructions please: public class Stack { int stackPtr; int data[]; public Stack() //constructor { stackPtr=0;...
Design and implement a class Rectangle to represent a rectangle. You should provide two Constructors for...
Design and implement a class Rectangle to represent a rectangle. You should provide two Constructors for the class, the first being the default constructor and the second which takes the basic dimensions and sets up the private member variables with the appropriate initial values. Methods should be provided that allow a user of the class to find out the length, width, area and perimeter of the shape plus a toString()method to print the values of the basic dimensions. Now implement...
Define the exception class called TornadoException. The class should have two constructors including one default constructor....
Define the exception class called TornadoException. The class should have two constructors including one default constructor. If the exception is thrown with the default constructor, the method getMessage should return "Tornado: Take cover immediately!" The other constructor has a single parameter, m, of int type. If the exception is thrown with this constructor, the getMessage should return "Tornado: m miles away; and approaching!" Write a Java program to test the class TornadoException.
This class has two constructors. The default constructor (the one that takes no arguments) should initialize the first and last names to "None", the seller ID to "ZZZ999", and the sales total to 0.
For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson.The Seller classUse the following class definition:class Seller { public:   Seller();   Seller( const char [], const char[], const char [], double );        void print();   void setFirstName( const char [] );   void setLastName( const char [] );   void setID( const char [] );   void setSalesTotal( double );   double getSalesTotal(); private:   char firstName[20];   char lastName[30];   char ID[7];   double salesTotal; };Data MembersThe data members for the class are:firstName holds the Seller's first namelastName holds the Seller's last nameID holds the Seller's id numbersalesTotal holds the Seller's sales totalConstructorsThis class has two constructors. The default constructor (the one that takes...
Write a function in C that takes one argument, an array of 50 elements. Your function...
Write a function in C that takes one argument, an array of 50 elements. Your function should print out the index and value of the smallest element in the array.
Language: Java Question:Using your Circle class (or the one provided below), create a Circle array of...
Language: Java Question:Using your Circle class (or the one provided below), create a Circle array of size 4 in a driver class using the following statement: Circle circleArr[] = new Circle[4]; Populate the array with four different radiuses and then, using a for loop from 0 to one less then the length of the array, print the area and the diameter of each of the four circles Circle Class: import java.text.DecimalFormat; public class Circle {    DecimalFormat dec = new...
Provide code samples for the following in C#a. Declare a two-dimensional array of integers names...
Provide code samples for the following in C#a. Declare a two-dimensional array of integers names intArray17.b. Create a loop to calculate the sum of every element in the first column.c. Create a loop to calculate the sum of every element in the array.
In C# - Provide code samples for the following: Declare a two-dimensional array of integers names...
In C# - Provide code samples for the following: Declare a two-dimensional array of integers names intArray17. Create a loop to calculate the sum of every element in the first column. Create a loop to calculate the sum of every element in the array.
Please provide your answer in the following format Write a C++ function, smallestIndex, that takes as...
Please provide your answer in the following format Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Also, write a program to test your function. You must insert the following comments at the beginning of your program and write our commands in the middle: Write a C++ Program: /* // Name: Your Name // ID: Your ID // Purpose...
c++ (1) Create a class, named Board, containing at least one data member (two-dimensional array) to...
c++ (1) Create a class, named Board, containing at least one data member (two-dimensional array) to store game states and at least two member functions for adding players’ moves and printing the game board. Write a driver to test your class. (2). The data type of the two-dimensional array can be either int or char. As a passlevel program, the size of the board can be hardcoded to 10 or a constant with a pre-set value, anything between 4 and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT