Question

In: Computer Science

Hi, I want to implement the following methods with a driver class In the comment block...

Hi, I want to implement the following methods with a driver class

In the comment block for add, give the best possible big-O of the worst-case running time for executing a single add operations and give the best possible big-O of the total worst-case running time of executing a sequence of N add operations.

here is the Implement class:

import java.util.Iterator;

// Do not modify the given code.

@SuppressWarnings("unchecked") // Given

public class MyArrayList {

private T[] data; // Given

private int size; // Given

/*

Implement. You must provide a comment block!

*/

public MyArrayList(int inCapacity) {

}

/*

Implement. You must provide a comment block!

*/

public boolean isEmpty() { }

/*

Implement. You must provide a comment block!

*/

public boolean add(T obj) {

// This method appends obj toward the back of data

// Always return true

}

/*

Implement. You must provide a comment block!

Implement this method so that a sequence of add operations

executes as efficiently as possible.

*/

private void grow() { }

/*

Implement. You must provide a comment block!

*/

public boolean remove(Object obj) {

// Return true if obj was removed from data

// and false otherwise.

}

Here is the driver class:

public String toString() {
if (size == 0) {
return "[ ]";
}
String retStr = "[ ";
for (int i = 0; i < size - 1; i++) {
retStr += data[i] + ", ";
}
retStr += data[size - 1] + " ]";
return retStr;
}

public boolean equals(Object obj) {
if (obj instanceof MyArrayList) {
MyArrayList rhs = (MyArrayList) obj;
if (size != rhs.size) {
return false;
}
for (int i = 0; i < size; i++) {
if (!rhs.contains(data[i])) {
return false;
}
}
return true;
}
return false;
}

private int find(Object obj) {
for (int i = 0; i < size; i++) {
if (data[i].equals(obj)) {
return i;
}
}
return -1;
}

public boolean contains(Object obj) {
return find(obj) >= 0;
}
public void addAll(MyArrayList inList) {
for (int i = 0; i < inList.size; i++) {
add(inList.data[i]);
}
}

private class MyArrayListIterator implements Iterator {

private int index = 0;

public void remove() {
}

public T next() {
T temp = data[index];
index++;
return temp;
}

public boolean hasNext() {
return index < size;
}
}


public Iterator iterator() {
return new MyArrayListIterator();
}
}

Solutions

Expert Solution

//Java program

import java.util.Iterator;

class MyArrayList<T> {

private T[] data; // Given

private int size; // Given

/*

Implement. You must provide a comment block!

*/

public MyArrayList (int inCapacity) {
   data = (T[]) new Object[inCapacity];
   size = 0;
}

/*

Implement. You must provide a comment block!

*/

public boolean isEmpty() { return size == 0;}

/*

Implement. You must provide a comment block!

*/

public boolean add(T data2) {

   if(size == data.length)this.grow();
   data[size++] = data2;
   return true;
}

/*

Implement. You must provide a comment block!

Implement this method so that a sequence of add operations

executes as efficiently as possible.

*/

private void grow() {
   T[] arr = (T[]) new Object[data.length*2];
  
   for(int i=0;i<size;i++) {
       arr[i] = data[i];
   }
   data = arr;
}

/*

Implement. You must provide a comment block!

*/

public boolean remove(Object obj) {

   int i;
   for(i=0;i<size;i++) {
       if(data[i] == obj)break;
   }
   if(i==size)return false;
   size--;
   for(;i<size;i++) {
       data[i] = data[i+1];
   }
   return true;
}

public String toString() {
if (size == 0) {
return "[ ]";
}
String retStr = "[ ";
for (int i = 0; i < size - 1; i++) {
retStr += data[i] + ", ";
}
retStr += data[size - 1] + " ]";
return retStr;
}

public boolean equals(Object obj) {
if (obj instanceof MyArrayList) {
MyArrayList rhs = (MyArrayList) obj;
if (size != rhs.size) {
return false;
}
for (int i = 0; i < size; i++) {
if (!rhs.contains(data[i])) {
return false;
}
}
return true;
}
return false;
}

private int find(Object obj) {
for (int i = 0; i < size; i++) {
if (data[i].equals(obj)) {
return i;
}
}
return -1;
}

public boolean contains(Object obj) {
return find(obj) >= 0;
}
public void addAll(MyArrayList inList) {
for (int i = 0; i < inList.size; i++) {
   add((T)inList.data[i]);
}
}

private class MyArrayListIterator implements Iterator {

private int index = 0;

public void remove() {
}

public T next() {
T temp = data[index];
index++;
return temp;
}

public boolean hasNext() {
return index < size;
}
}


public Iterator iterator() {
return new MyArrayListIterator();
}
}

public class List {
   public static void main(String args[]) {
       MyArrayList<Integer> list = new MyArrayList<Integer>(2);
       for(int i=0;i<10;i++) {
           list.add(i+1);
       }
       System.out.println(list);
       list.remove(5);
       System.out.println("List after removing 5 \n"+list);
      
       MyArrayList<Integer> list1 = new MyArrayList<Integer>(2);
       list1.addAll(list);
       System.out.println(list1);
   }
}

//sample output



Related Solutions

Implement a Linux Block Driver which simulates a block device with a region of memory init,...
Implement a Linux Block Driver which simulates a block device with a region of memory init, exit, and IO functionalities must be included. Please use either C or C++ Programming for this Thank You!
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
In C++ and pls comment every line so I UNDERSTAND Implement a class named DynamicArray that...
In C++ and pls comment every line so I UNDERSTAND Implement a class named DynamicArray that has the following members: A pointer to hold a dynamically allocated array, of type int. A member variable to hold the size of the array. A default constructor, which will allocate an array of size 10 A parameterized constructor, which takes a size and use the size to allocate array. A copy constructor, which performs deep copy. A copy assignment operator, which performs deep...
java code Add the following methods to the LinkedQueue class, and create a test driver for...
java code Add the following methods to the LinkedQueue class, and create a test driver for each to show that they work correctly. In order to practice your linked list cod- ing skills, code each of these methods by accessing the internal variables of the LinkedQueue, not by calling the previously de?ined public methods of the class. String toString() creates and returns a string that correctly represents the current queue. Such a method could prove useful for testing and debugging...
28. Add the following methods to the ArrayBoundedStack class, and create a test driver for each...
28. Add the following methods to the ArrayBoundedStack class, and create a test driver for each to show that they work correctly. In order to practice your array related coding skills, code each of these methods by accessing the internal variables of the ArrayBoundedStack, not by calling the previously defined public methods of the class. a. String toString()—creates and returns a string that correctly represents the current stack. Such a method could prove useful for testing and debugging the class...
Implement the following methods. Assume that these will be methods within your myArray class. operator[] Should...
Implement the following methods. Assume that these will be methods within your myArray class. operator[] Should take in an int and return arr at that index if it is a valid index notEqual The notEqual should take in another myArray object and return true if the objects are not equal to one another and false if they are equal. operator-(float) Will subtract the float value that is passed in from each of the values in arr Copy constructor Will take...
/* *       Suppose we want to implement a class IntArraySet. The difference of this...
/* *       Suppose we want to implement a class IntArraySet. The difference of this class from IntArrayBag is that each item can only occur once in the set We will use the same instance variables. */ public class IntArraySet { private int[ ] data; private int manyItems; public IntArraySet() { this(10); } public IntArraySet(int initialCapacity) { if (initialCapacity < 0) throw new IllegalArgumentException ("The initialCapacity is negative: " + initialCapacity); data = new int[initialCapacity]; manyItems = 0; }...
I WANT TO IMPLEMENT THIS IN JAVA PLEASE I want to create a small user input...
I WANT TO IMPLEMENT THIS IN JAVA PLEASE I want to create a small user input system for a university student, where they put the season and year of when they started their uni course. For example the system will ask "What year did you start your degree?", the user will input "Autumn/2022" as a string. Now from a string format as shown, it should take that user input and calculate for example +2 or +3 years to the date....
Write two classes, ToDoList and Driver ToDoList should implement the following UML class diagram: ToDoList -list:String[]...
Write two classes, ToDoList and Driver ToDoList should implement the following UML class diagram: ToDoList -list:String[] -size:int +ToDoList() +add(String item):void +remove(String item): boolean +toString():String add(String item) should add the item as the last element in the list, updating size. remove(String item) should remove the item and return true, or, if the item was not in the list, return false.   To remove the item, check every list element from 0 to size-1, and if that item is equal to the parameter...
Overview For this assignment, design and implement the methods for a class that can be used...
Overview For this assignment, design and implement the methods for a class that can be used to represent a quadratic equation. int main() has already been written for this assignment. It is available for download from Blackboard or by using the following link: http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240pgm8.cpp All that needs to be done for this assignment is to add the class definition and method implementation to the above CPP file. The Quadratic class Data Members The class contains three data members: an integer...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT