In the file Collection.java (provided with this homework), a Collection class is
ID: 664686 • Letter: I
Question
In the file Collection.java (provided with this homework), a Collection class is defined which allows users to perform operations (e.g., insert, remove, and check for membership) among a collection of integers, which is stored by an int array. The first element in the array stores the first integer inserted, the second element stores the second integer inserted, and so on. There is a field called next, which points to the next available slot in the collection. In this problem, you need to (1) Complete the method: public void delete(int k). In this method, after you delete an integer k (if existed), for the array you need to move left every number which is right of k left, and adjust next. (2) Satisfy the tests in the main method. For each test, the correct output is already given (the first output), and you need to make sure your output (the second output) is the same as the first one. Note: the following two methods are provided in the code to help you debug your code. (1) private void printArray(): allow you to print all elements in the array. (2) public String toString(): because this method is given (overriden), you can simply print the collection by using the following statement, where C is a collection. System.out.println(C); If you are interested in more details about toString() method, you can take a look at http: //www.javatpoint.com/understanding-toString()-method. Following is the code of collection.java: public class Collection { private int[] A; // array to hold integers in the collection private int next; // pointer to next available slot in collection // default constructor public Collection() { A = new int[10]; next = 0; } // constructor for making initial array a given size public Collection(int n) { A = new int[n]; next = 0; } // just utility methods to determine status of collection public int size() { return next; } public boolean isEmpty() { return (next == 0); } public boolean isFull() { return (next == A.length); } // search the collection for the given integer public boolean member(int k) { for(int i = 0; i < next; ++i) { if(A[i] == k) return true; } return false; } // insert a new integer into the collection; if already there, do nothing public void insert(int k) { if(!member(k)) { A[next] = k; ++next; } } // delete an integer; if not there, do nothing public void delete(int k) { // scan from left, if find k, move over every number to right of k; move them left and adjust next } private void printArray() { System.out.print("["); if(isEmpty()) { System.out.println("]"); return; } else { for(int i = 0; i < A.length-1; ++i) { System.out.println(A[i] + ","); } System.out.println(A[A.length-1] + "]"); } } public String toString() { String s = "["; if(isEmpty()) { return s + "]"; } else { for(int i = 0; i < next-1; ++i) { s += (A[i] + ","); } s += A[next-1] + "]"; return s; } } public static void main(String[] args) { Collection C = new Collection(); System.out.println(" Test 1: Should be: true 0 false"); System.out.println(C.isEmpty() + " " + C.size() + " " + C.isFull()); System.out.println(" Test 2: Should be: false 2"); C.insert(5); C.insert(7); System.out.println(C.isEmpty() + " " + C.size()); System.out.println(" Test 3: Should be: [5,7,-3,27,51]"); C.insert(-3); C.insert(27); C.insert(51); System.out.println(C); System.out.println(" Test 4: Should be: [5,7,-3,27,51,0,0,0,0,0] next = 5"); C.printArray(); System.out.println(" Test 5: Should be: [5,7,-3,27,51] 5"); C.insert(5); C.insert(-3); C.insert(51); System.out.println(C + " " + C.size()); System.out.println(" Test 6: Should be: [5,7,-3,27,51,52,71,4,1,87] 10 true"); C.insert(52); C.insert(71); C.insert(4); C.insert(1); C.insert(87); System.out.println(C + " " + C.size() + " " + C.isFull() ); System.out.println(" Test 7: Should be: [-3,27,51,52,71,4,1,87] 8"); C.delete(5); C.delete(7); System.out.println(C + " " + C.size()); System.out.println(" Test 8: Should be: [27,51,52,71,4,1,87] 7"); C.delete(-3); System.out.println(C + " " + C.size()); System.out.println(" Test 9: Should be: [27,51,52,71,4,1] 6"); C.delete(-71); C.delete(87); System.out.println(C + " " + C.size()); System.out.println(" Test 10: Should be: [27,51,52,4,1] 5"); C.delete(71); C.delete(87); System.out.println(C + " " + C.size()); System.out.println(" Test 11: Should be: [] 0"); C.delete(27); C.delete(57); C.delete(1);C.delete(51); C.delete(4); C.delete(23); C.delete(52); System.out.println(C + " " + C.size()); } }Explanation / Answer
import java.util.*;
public class Collection
{
private int[] A; // array to hold integers in the collection
private int next; // pointer to next available slot in collection
// default constructor
public Collection()
{
A = new int[10];
next = 0;
} // constructor for making initial array a given size
public Collection(int n)
{
A = new int[n]; next = 0;
}
// just utility methods to determine status of collection
public int size()
{
return next;
}
public boolean isEmpty()
{
return (next == 0);
}
public boolean isFull()
{
return (next == A.length);
}
// search the collection for the given integer
public boolean member(int k)
{
for(int i = 0; i < next; ++i)
{
if(A[i] == k)
return true;
}
return false;
}
// insert a new integer into the collection; if already there, do nothing
public void insert(int k)
{
if(!member(k))
{
A[next] = k; ++next;
}
} // delete an integer; if not there, do nothing
public void delete(int k)
{
if(!member(k))
{
System.out.println("The value " + k + " was not found and cannot be deleted.");
}
else
{
int size = A.length;
for (int i = search(k); i < size; i++)
{
if(i < (size-1))
A[i]= A[i+1];
else
A[i] = 0; //set to some base case or zero
}
}
}
//A[0] = temp;
//--next;
//System.out.println(k);
// scan from left, if find k, move over every number to right of k; move them left and adjust next
private void printArray()
{
System.out.print("[");
if(isEmpty())
{
System.out.println("]");
return;
}
else
{
for(int i = 0; i < A.length-1; ++i)
{
System.out.println(A[i] + ",");
}
System.out.println(A[A.length-1] + "]");
}
}
public String toString()
{
String s = "[";
if(isEmpty())
{
return s + "]";
}
else
{
for(int i = 0; i < next-1; ++i)
{
s += (A[i] + ",");
}
s += A[next-1] + "]";
return s;
}
}
public static void main(String[] args)
{
Collection C = new Collection();
System.out.println(" Test 1: Should be: true 0 false");
System.out.println(C.isEmpty() + " " + C.size() + " " + C.isFull());
System.out.println(" Test 2: Should be: false 2");
C.insert(5);
C.insert(7);
System.out.println(C.isEmpty() + " " + C.size());
System.out.println(" Test 3: Should be: [5,7,-3,27,51]");
C.insert(-3);
C.insert(27);
C.insert(51);
System.out.println(C);
System.out.println(" Test 4: Should be: [5,7,-3,27,51,0,0,0,0,0] next = 5");
C.printArray();
System.out.println(" Test 5: Should be: [5,7,-3,27,51] 5");
C.insert(5);
C.insert(-3);
C.insert(51);
System.out.println(C + " " + C.size());
System.out.println(" Test 6: Should be: [5,7,-3,27,51,52,71,4,1,87] 10 true");
C.insert(52);
C.insert(71);
C.insert(4);
C.insert(1);
C.insert(87);
System.out.println(C + " " + C.size() + " " + C.isFull() );
System.out.println(" Test 7: Should be: [-3,27,51,52,71,4,1,87] 8");
C.delete(5);
C.delete(7);
System.out.println(C + " " + C.size());
System.out.println(" Test 8: Should be: [27,51,52,71,4,1,87] 7");
C.delete(-3);
System.out.println(C + " " + C.size());
System.out.println(" Test 9: Should be: [27,51,52,71,4,1] 6");
C.delete(-71);
C.delete(87);
System.out.println(C + " " + C.size());
System.out.println(" Test 10: Should be: [27,51,52,4,1] 5");
C.delete(71);
C.delete(87);
System.out.println(C + " " + C.size());
System.out.println(" Test 11: Should be: [] 0");
C.delete(27);
C.delete(57);
C.delete(1);
C.delete(51);
C.delete(4);
C.delete(23);
C.delete(52);
System.out.println(C + " " + C.size());
}
}
import java.util.*;
public class Collection
{
private int[] A; // array to hold integers in the collection
private int next; // pointer to next available slot in collection
// default constructor
public Collection()
{
A = new int[10];
next = 0;
} // constructor for making initial array a given size
public Collection(int n)
{
A = new int[n]; next = 0;
}
// just utility methods to determine status of collection
public int size()
{
return next;
}
public boolean isEmpty()
{
return (next == 0);
}
public boolean isFull()
{
return (next == A.length);
}
// search the collection for the given integer
public boolean member(int k)
{
for(int i = 0; i < next; ++i)
{
if(A[i] == k)
return true;
}
return false;
}
// insert a new integer into the collection; if already there, do nothing
public void insert(int k)
{
if(!member(k))
{
A[next] = k; ++next;
}
} // delete an integer; if not there, do nothing
public void delete(int k)
{
if(!member(k))
{
System.out.println("The value " + k + " was not found and cannot be deleted.");
}
else
{
int size = A.length;
for (int i = search(k); i < size; i++)
{
if(i < (size-1))
A[i]= A[i+1];
else
A[i] = 0; //set to some base case or zero
}
}
}
//A[0] = temp;
//--next;
//System.out.println(k);
// scan from left, if find k, move over every number to right of k; move them left and adjust next
private void printArray()
{
System.out.print("[");
if(isEmpty())
{
System.out.println("]");
return;
}
else
{
for(int i = 0; i < A.length-1; ++i)
{
System.out.println(A[i] + ",");
}
System.out.println(A[A.length-1] + "]");
}
}
public String toString()
{
String s = "[";
if(isEmpty())
{
return s + "]";
}
else
{
for(int i = 0; i < next-1; ++i)
{
s += (A[i] + ",");
}
s += A[next-1] + "]";
return s;
}
}
public static void main(String[] args)
{
Collection C = new Collection();
System.out.println(" Test 1: Should be: true 0 false");
System.out.println(C.isEmpty() + " " + C.size() + " " + C.isFull());
System.out.println(" Test 2: Should be: false 2");
C.insert(5);
C.insert(7);
System.out.println(C.isEmpty() + " " + C.size());
System.out.println(" Test 3: Should be: [5,7,-3,27,51]");
C.insert(-3);
C.insert(27);
C.insert(51);
System.out.println(C);
System.out.println(" Test 4: Should be: [5,7,-3,27,51,0,0,0,0,0] next = 5");
C.printArray();
System.out.println(" Test 5: Should be: [5,7,-3,27,51] 5");
C.insert(5);
C.insert(-3);
C.insert(51);
System.out.println(C + " " + C.size());
System.out.println(" Test 6: Should be: [5,7,-3,27,51,52,71,4,1,87] 10 true");
C.insert(52);
C.insert(71);
C.insert(4);
C.insert(1);
C.insert(87);
System.out.println(C + " " + C.size() + " " + C.isFull() );
System.out.println(" Test 7: Should be: [-3,27,51,52,71,4,1,87] 8");
C.delete(5);
C.delete(7);
System.out.println(C + " " + C.size());
System.out.println(" Test 8: Should be: [27,51,52,71,4,1,87] 7");
C.delete(-3);
System.out.println(C + " " + C.size());
System.out.println(" Test 9: Should be: [27,51,52,71,4,1] 6");
C.delete(-71);
C.delete(87);
System.out.println(C + " " + C.size());
System.out.println(" Test 10: Should be: [27,51,52,4,1] 5");
C.delete(71);
C.delete(87);
System.out.println(C + " " + C.size());
System.out.println(" Test 11: Should be: [] 0");
C.delete(27);
C.delete(57);
C.delete(1);
C.delete(51);
C.delete(4);
C.delete(23);
C.delete(52);
System.out.println(C + " " + C.size());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.