Help fix this program please! Most of it is written but I have outlined in the p
ID: 3736219 • Letter: H
Question
Help fix this program please! Most of it is written but I have outlined in the program below the area that needs to be edited. The output currently does not match what I need to get credit on the assignment and I need it to be fixed.
I wrote in the program a boundary where to edit the code. The rest of it does not need to be changed.
Please do this:
-Match outputs
-Write the public void remove(Object obj) method
DESIRED OUTPUT:
_____________________________________________________________________________________
COPY THIS:
package myset;
import java.util.Arrays;
/**
* This Java application implements a Set data structure
* that stores a set of Objects. MySet objects have a fixed
* capacity (i.e. they don't grow) and duplicate members
* are not allowed.
*
*/
interface CSC205_Set {
boolean isMember(Object obj);
boolean isEmpty();
boolean isFull();
int capacity();
int size();
void clear();
String toString();
int add(Object obj);
void remove(Object obj);
boolean equals(MySet that);
MySet union(MySet that);
MySet intersection(MySet that);
MySet difference(MySet that);
}
public class MySet implements CSC205_Set {
public static final int ADD_DUP_ERRNO = -1;
public static final int ADD_FULL_ERRNO = -2;
private static final int CAPACITY = 7;
private Object[] data;
private int size;
/**
* Constructs a MySet object having a default CAPACITY.
*/
public MySet() { this(CAPACITY); }
/**
* Constructs a MySet object having a client supplied capacity.
*
* @param capacity becomes CAPACITY if <= 0
*/
public MySet(int capacity) {
if (capacity <= 0) capacity = CAPACITY;
data = new Object[capacity];
size = 0;
}
/**
* Constructs a MySet object containing Integer objects.
*
* @param e int[] of values that get added to this MySet
*/
public MySet(int[] e) {
data = new Object[CAPACITY];
size = e.length > CAPACITY ? CAPACITY : e.length;
for (int i = 0; i < size; i++)
data[i] = new Integer(e[i]);
}
/**
* Checks to see of an object is a member of this MySet.
*
* @param obj the class Object object to search for
* @return true if object is a member of this MySet
* @return false if object is not a member of this MySet
*/
public boolean isMember(Object obj) {
for (int i = 0; i < size; i++)
if (data[i].equals(obj))
return true;
return false;
}
/**
* Checks to see if this MySet has zero members (elements).
*
* @return true if this MySet has zero members
* @return false if this MySet has more than zero members
*/
public boolean isEmpty() { return size == 0; }
/**
* Checks to see if this MySet is at capacity.
*
* @return true if at capacity
* @return false if not at capacity
*/
public boolean isFull() { return size == data.length; }
/**
* Returns the number of members in this MySet.
*
* @return number of members in this MySet
*/
public int size() { return size; }
/**
* An alias for size().
*
* @return number of members
*/
public int cardinality() { return size; }
/**
* Getter method so client can check the capacity of this MySet.
*
* @return capacity of this MySet
*/
public int capacity() { return data.length; }
/**
* Sets the size of this MySet to zero.
*/
public void clear() { size = 0; }
/**
* Constructs a String representation of this MySet.
*
* @return string representation of this MySet
*/
public String toString() {
StringBuffer sb = new StringBuffer("{");
int n = size - 1;
for (int i = 0; i < n; i++)
sb.append(data[i] + ", ");
if (size > 0)
sb.append(data[n]);
return new String(sb) + "}";
}
//***************************ONLY EDIT THE CODE BELOW THIS BOUNDARY***********
/*
* TBI (To Be Implemented) -- The remaining instance methods
* need to be implemented as part of the #Set assignment.
*/
/**
* TBI (To Be Implemented)
* Adds an object to this MySet.
*
* @param obj the class Object to add to this MySet
* @return ADD_FULL_ERRNO if this MySet is at capacity;
* else return ADD_DUP_ERRNO if object already
* a member of this MySet; else return the new
* size of this MySet
*/
public int add(Object obj) {
int i=contains(obj);
if (i==-1) {
if (data.length - size <= data.length / 2) {
this.reSizeArray();
}
data[size++] = obj;
return size;
}
return -1;
}
public void reSizeArray() {
data = Arrays.copyOf(data, data.length + 1);
}
public int contains(Object elem){
if (elem == null) {
for (int i = 0; i < size; i++)
if (data[i]==null)
return i;
}
else {
for (int i = 0; i < size; i++)
if (elem.equals(data[i]))
return i;
}
return -1;
}
/**
* TBI (To Be Implemented)
* Removes an object from this MySet. The method does nothing
* if the object is not a member.
*
* @param obj the object to remove from this MySet
*/
public void remove(Object obj) {
}
/**
* TBI (To Be Implemented)
* Checks to see if two MySet objects are equal.
* hint: a.difference(b) and b.difference(a)
* are both empty, then...
*
* @param that MySet to compare again this MySet
* @return true if the two MySets are equal; else
* return false
*/
public boolean equals(MySet that) {
if (size()==that.size) {
for (int i = 0; i < data.length; i++) {
if (!Arrays.asList(that.data).contains(data[i]))
return false;
}
return true;
}
return false;
}
/**
* TBI (To Be Implemented)
* Instantiates a new MySet object that contains all of the elements
* of this MySet and all of the elements of that MySet (duplicates
* are not allowed).
*
* @param that MySet to do union with this MySet
* @return the union of this and that MySets
*/
public MySet union(MySet that) {
for(int i = 0; i < data.length; i++)
that.add(data[i]);
return that;
}
/**
* TBI (To Be Implemented)
* Instantiates a new MySet object that contains all of the
* members that are found in both this MySet and that MySet.
*
* @param that MySet to do intersection with this MySet
* @return the intersection of this and that MySets
*/
public MySet intersection(MySet that) {
MySet newSet = new MySet();
int smallSize = 0;
for(int i = 0; i < data.length; i++)
if(Arrays.asList(that.data).contains(data[i]))
newSet.add(data[i]);
return newSet;
}
/**
* TBI (To Be Implemented)
* Instantiates a new MySet object that contains all of the
* members of this MySet that are not found in that MySet.
*
* @param that MySet to do difference with this MySet
* @return the difference of this and that MySets
*/
public MySet difference(MySet that) {
MySet newSet = new MySet();
for(int i = 0; i < data.length; i++)
if(!Arrays.asList(that.data).contains(data[i]))
newSet.add(data[i]);
return newSet;
}
//***************************ONLY EDIT THE CODE ABOVE THIS BOUNDARY***********
/**
* The main() method is used exclusively for testing.
*/
public static void main(String[] argv) {
Object[] objects = {
new Integer(205),
new String("Java supports OOP"),
new Boolean(true),
new Byte((byte)42),
new Integer(240),
new Byte((byte)42),
new String("foo"),
new Boolean(true),
new String("foo"),
new String("Java creator: James Gosling"),
new Integer(240),
new Double(3.14159265),
new Object(),
};
MySet test = new MySet();
for (int i = 0; i < objects.length; i++) {
System.out.print(objects[i]);
int rv = test.add(objects[i]);
switch (rv) {
case ADD_FULL_ERRNO:
System.out.println(" not added b/c test is full");
break;
case ADD_DUP_ERRNO:
System.out.println(" not added b/c it's a duplicate");
break;
default:
System.out.println(" added (" + rv + ")");
break;
}
}
System.out.println(test);
dump(test);
test.clear();
System.out.println("test cleared: " + test);
dump(test);
final String SEPARATOR = " =========================";
System.out.println(SEPARATOR);
final int[] A = { 5, 7, 3, 2 };
final int[] B = { 2, 6 };
final int[] C = { 1, 2, 5 };
final int[] D = { 3, 2, 7, 5 };
final int[] E = { 5, 2, 1 };
MySet a = new MySet(A); System.out.println("A: " + a);
MySet b = new MySet(B); System.out.println("B: " + b);
MySet c = new MySet(C); System.out.println("C: " + c);
MySet d = new MySet(D); System.out.println("D: " + d);
MySet e = new MySet(E); System.out.println("E: " + e);
System.out.println(a + ".equals(" + b + ") is " + a.equals(b));
System.out.println(c + ".equals(" + d + ") is " + c.equals(d));
System.out.println(c + ".equals(" + e + ") is " + c.equals(e));
System.out.println(a + ".union(" + b + "): " + a.union(b));
System.out.println(a + ".intersection(" + b + "): " + a.intersection(b));
System.out.println(a + ".difference(" + b + "): " + a.difference(b));
System.out.println(b + ".difference(" + a + "): " + b.difference(a));
System.out.println(c + ".union(" + d + "): " + c.union(d));
System.out.println(c + ".intersection(" + d + "): " + c.intersection(d));
System.out.println(c + ".difference(" + d + "): " + c.difference(d));
System.out.println(d + ".difference(" + c + "): " + d.difference(c));
System.out.println(c + ".difference(" + e + "): " + c.difference(e));
System.out.println(a + ".union(" + a + "): " + a.union(a));
System.out.println(a + ".intersection(" + a + "): " + a.intersection(a));
System.out.println(a + ".difference(" + a +"): " + a.difference(a));
Integer x = new Integer(A[1]);
System.out.print(a);
a.remove(x);
System.out.println(".remove(" + x + "): " + a);
// miscelleanous testing...
MySet n0 = new MySet();
MySet n1 = new MySet();
System.out.println(n0 + ".union(" + n1 + "): " + n0.union(n1));
System.out.println(n0 + ".intersection(" + n1 + "): " +
n0.intersection(n1));
System.out.println(n0 + ".difference(" + n1 + "): " + n0.difference(n1));
System.out.println(n0 + ".union(" + a + "): " + n0.union(a));
System.out.println(n0 + ".intersection(" + a + "): " +
n0.intersection(a));
System.out.println(n0 + ".difference(" + a + "): " + n0.difference(a));
System.out.println(a + ".union(" + n0 + "): " + a.union(n0));
System.out.println(a + ".intersection(" + n0 + "): " +
a.intersection(n0));
System.out.println(a + ".difference(" + n0 + "): " + a.difference(n0));
}
private static void dump(MySet set) {
System.out.println("...isEmpty(): " + set.isEmpty() + "; isFull(): " +
set.isFull() + "; size(): " + set.size() +
"; capacity(): " + set.capacity());
}
}
Explanation / Answer
Your query and the outputs expected are not very clear. I could see many 'To be Implemented' methods written.Which one do you want me to answer?
Anyway, one thing clearly mentioned was - asking for the logic to implement public void remove(Object obj) method. The logic for the same is as follows:(Assuming data as the set of elements, size as the size of your data set, obj as the element which has to be removed from the set)
public void remove(Object obj) {
for (unsigned int i = 0; i < size; i++) {
if (data[i].equals(obj)) {
for(unsigned int j = i;j < size - 1;j++) {
data[j] = data[j+1];
}
}
else{
System.out.println("Element not found in MySet");
}
}
}
Hope this was helpful.All the best!
- Manasa B
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.