So I fixed most of the problems for this, with help, however now when I try to properly print the IntegerSet it does not change.
I am not sure why this type of problem would occur, maybe because the test is awful?
This is how it remains.
setA: IntegerSet@1893c911 // Any ideas why this happens?
setB: IntegerSet@e7587b2 // And this?
1) insertElement into setA
2) deleteElement from setA
3) insertElement into setB
4) deleteElement from setB
5) intersection of setA and setB
6) union of setA and setB
7) equality of setA and setB
Select from the menu above (or 0 to exit):
This is the class code:
int [] a; // holds a set of numbers from 0 - 100
public IntegerSet () {
// an empty set, all a[i] are set to 0
a = new int [101];
}
// A constructor that copies from an existing set.
public IntegerSet (IntegerSet existingSet) {
a = new int [101];
for(int i=0; i<a.length; i++)
a[i] = existingSet.a[i];
}
public void deleteElement(int i) {
if ((i >= 0) && (i < a.length))
a[i] = 0; // set to 1
}
public void insertElement(int i) {
if ((i >= 0) && (i < a.length))
a[i] = 1; // set to 1
}
public boolean isSet(int i) {
return (a[i] == 1);
}
public int lengthOfArray(){
return this.a.length;
}
public static IntegerSet union(IntegerSet otherSet, IntegerSet nextSet) {
for(int i=0; i<otherSet.length(); i++) {
if (otherSet.isSet(i))
nextSet.insertElement(i);
}
return nextSet;
}
public static IntegerSet intersection(IntegerSet otherSet, IntegerSet nextSet) {
for(int i=0; i<otherSet.length(); i++) {
if (!otherSet.isSet(i))
nextSet.deleteElement(i);
}
return nextSet;
}
// return true if the set has no elements
public boolean isEmpty() {
for (int i=0; i<a.length; i++)
if (isSet(i)) return false;
return true;
}
// return the 'length' of a set
public int length() {
int count = 0;
for (int i=0; i<a.length; i++)
if (isSet(i))
count++;
return count;
}
// Print a set to System.out
public void setPrint() {
System.out.print("[Set:");
if (isEmpty())
System.out.print("---");
for (int i=0; i<a.length; i++) {
if (isSet(i))
System.out.print(" " + i);
}
System.out.print("]\n");
}
// return true if two sets are equal
public boolean isEqualTo(IntegerSet otherSet) {
for(int i=0; i<a.length; i++) {
if (otherSet.isSet(i) != isSet(i))
return false;
}
return true;
}
Test Code :
import java.util.Scanner;
public class IntegerSetTest {
public static void main(String args[]) {
IntegerSet setA = new IntegerSet();
IntegerSet setB = new IntegerSet();
Scanner scan = new Scanner(System.in);
int input;
do {
// Just for formatting purposes...
System.out.println();
System.out.println("setA: " + setA);
System.out.println("setB: " + setB);
System.out.println("1) insertElement into setA");
System.out.println("2) deleteElement from setA");
System.out.println("3) insertElement into setB");
System.out.println("4) deleteElement from setB");
System.out.println("5) intersection of setA and setB");
System.out.println("6) union of setA and setB");
System.out.println("7) equality of setA and setB");
System.out.println("Select from the menu above (or 0 to exit): ");
input = scan.nextInt();
switch(input) {
case 1:
System.out.print("Enter an element to insert into setA: ");
setA.insertElement(scan.nextInt());
break;
case 2:
System.out.print("Enter an element to delete from setA: ");
setA.deleteElement(scan.nextInt());
break;
case 3:
System.out.print("Enter an element to insert into setB: ");
setB.insertElement(scan.nextInt());
break;
case 4:
System.out.print("Enter an element to delete from setB: ");
setB.deleteElement(scan.nextInt());
break;
case 5:
System.out.println("The intersection of setA and setB is: " + IntegerSet.intersection(setA, setB));
break;
case 6:
System.out.println("The union of setA and setB is: " + IntegerSet.union(setA, setB));
break;
case 7:
System.out.println("setA and setB are " + (setA.isEqualTo(setB) ? "" : "un") + "equal");
break;
default:
if (input != 0) {
System.out.println("\n*** Error, invalid input! ***\n");
}
}
}while(input != 0);
}
}
When you try to output your object…
You’re attempting to convert an object to a String. In order to do that:
… you need to override the
toString(...)method in classIntegerSet: