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!
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...
/* *       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; }...
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...
You shall implement six static methods in a class named BasicBioinformatics. Each of the methods will...
You shall implement six static methods in a class named BasicBioinformatics. Each of the methods will perform some analysis of data considered to be DNA. DNA shall be represented arrays of chars containing only the characters A, C, G and T. In addition to the six methods you will implement, six other methods exist in the class, which use Strings instead of char arrays to represent DNA. These other methods simply invoke the methods you are to implement, so all...
PhoneNumber You will want to go to PhoneNumber.java and implement the following methods getType setType(String) Once...
PhoneNumber You will want to go to PhoneNumber.java and implement the following methods getType setType(String) Once again no need to check type, any String is allowed. getNumber() returns the raw number setNumber(String number) sets the number (10 character string, digits only). You do not need to confirm the string format that is done in another class. getAreaCode() Uses substring to get the first 3 digits of the number. getPrefix() Gets the next three digits (so 4-6) as a string, uses...
Purpose Purpose is to implement some single linked list methods. Add methods to the List class...
Purpose Purpose is to implement some single linked list methods. Add methods to the List class In the ‘Implementation of linked lists’ lecture, review the ‘Dynamic implementation of single linked list’ section. You will be adding new methods to the List class. Eight new methods are required: new constructor – creates a new single linked list from an array of integers e.g. int a[] = {1, 2, 3, 4}; List list = new List(a); toString() – returns a string representing...
Overview For this assignment, implement and use the methods for a class called Seller that represents...
Overview For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson. The Seller class Use 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; };...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT