Question

In: Computer Science

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 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

Solutions

Expert Solution


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

   }

}


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...
Please use the skills you learned in section 9.2 for this assignment. For this activity, you...
Please use the skills you learned in section 9.2 for this assignment. For this activity, you will be creating a confidence interval for the average number of hours of TV watched. Last semester, my MAT 152 online students asked the question, “How many hours of screen time do you have in a typical week?” Please use the data they collected to answer the following questions. Data: 21, 2, 28, 30, 18, 21, 25, 20, 25, 14, 21, 25, 50, 39,...
Please use the skills you learned in section 9.2 for this assignment. For this activity, you...
Please use the skills you learned in section 9.2 for this assignment. For this activity, you will be creating a confidence interval for the average number of hours of TV watched. Last semester, my MAT 152 online students asked the question, “How many hours of screen time do you have in a typical week?” Please use the data they collected to answer the following questions. Data: 21, 2, 28, 30, 18, 21, 25, 20, 25, 14, 21, 25, 50, 39,...
For this assignment, you are going to build upon several skills that you've learned: Create an...
For this assignment, you are going to build upon several skills that you've learned: Create an object that contains objects. Using querySelectorAll to read a nodeList from the DOM. Looping through the nodeList then updating the HTML page. Set up Create the assignment in a "week6" folder with the typical files: index.html css/styles.css js/scripts.js This is the standard structure that we'll use in all assignments. Here is the HTML to use for this assignment. Change the meta author tag to...
We've learned about PHI and how important it is to protect patient information. We've also learned...
We've learned about PHI and how important it is to protect patient information. We've also learned how "loose talk" causes problems. But how many of us have put that into practice? Your challenge is to go out into the world this week (or your current home or work environment), sit for awhile, and count how many times in a day you hear others talking about someone behind their back. It can be good or bad, it doesn't matter for the...
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...
For this assignment, you will apply what you learned in analyzing a simple Java™ program by...
For this assignment, you will apply what you learned in analyzing a simple Java™ program by writing your own Java™ program. The Java™ program you write should do the following: Display a prompt on the console asking the user to type in his or her first name Construct the greeting string "Hello, nameEntered!" Display the constructed greeting on the console Complete this assignment by doing the following: Download and unzip the linked zip file. Add comments to the code by...
For this assignment, you will apply what you learned in analyzing Java™ code so far in...
For this assignment, you will apply what you learned in analyzing Java™ code so far in this course by writing your own Java™ program. The Java™ program you write should do the following: Accept user input that represents the number of sides in a polygon. Note: The code to do this is already written for you. If input value is not between 3 and 5, display an informative error message If input value is between 3 and 5, use a...
For this assignment, you will be analyzing the Java™ code in the linked Week 3 Analyze...
For this assignment, you will be analyzing the Java™ code in the linked Week 3 Analyze Assignment Zip File, and predicting the results. You will also examine both the code and the output for inconsistencies and clarity. This Java™ code includes examples of for, while, and do-while loops. Carefully read through the code line by line, then answer the following questions in a Microsoft® Word document: What is the output of the program as it is written? What improvement(s) could...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT