Lab4: IntegerSet class

Create a class that implements an IntegerSet. The valid members of the IntegerSet cannot be duplicates and must be in the range of 0 to 100 inclusive. An easy way to implement this is with an array of booleans. Each element of an array of booleans is initialized to false. Integers that are members of the IntegerSet are true in the array. You can also use an ArrayList<Integer> to hold the IntegerSet members, but be careful to avoid duplicates. Also, to match Mimir I/O Test Cases, you will have to sort the IntegerSet members.

 

The model code for the IntegerSet class uses an array of booleans that is initialized to false before any members are added. When a new member is added to the IntegerSet, first check that it is a valid entry, then set the array element using the member as the index to true. Any element in the array that is true is a member of the IntegerSet. When the IntegerSet is converted to a String, it ends up being in sorted order. To match Mimir, you will have to do the same regardless of your implementation.

 

IntegerSet Constructors

public IntegerSet()             

// Implement the default constructor for an empty set.

public IntegerSet(int[] array)  

// Implement the constructor that initializes the set from an array.

 

Implement the default constructor as an empty set. Implement the parameterized constructor to initialize the IntegerSet. Make sure not to include any duplicates.

 

The constructors create an instance of the IntegerSet object that allows us to perform certain operations such as union, intersection insertMember, deleteMember.

 

IntegerSet Operations

 

Two operations that should be possible are Union and Intersection. Implement the two methods that perform the union and intersection operations. Remember that these operations should not change either the invoking object or the parameter. A new IntegerSet should be returned with the included members.

The Union of two Sets has all members in both Sets but no duplicates.

The Intersection of the two sets has only the members that are in both Sets.

 

public IntegerSet union(IntegerSet set)

// Performs the union of the invoking Set and given Set to produce a new

// Set that is the union and is returned.

// The new IntegerSet contains all unique members of both IntegerSets.

 

public IntegerSet intersection(IntegerSet set)

// Performs the intersection of the invoking Set and given Set to produce a new

// Set that is the intersection and returned.

// The new IntegerSet contains only the members that are in BOTH IntegerSets.

 

Mutator Methods for IntegerSet

Two mutator methods are required. One mutator method inserts members and the other deletes members. They are mutator methods because they change the invoking object. Keep in mind that depending on the internal implementation, the input may have to be converted to an Integer object.

 

public void insertMember(int member)

// Inserts the given member to the invoking object if it is a valid entry and not

// a duplcate.

 

public void deleteMember(int member)

// Deletes the given member from the invoking object if it is a valid entry and

// in the IntegerSet.

 

Additional Methods

Additional methods are those that are expected because of Java conventions. Those methods include toString and equals.

 

public String toString() 

// Print the IntegerSet as a list of members surrounded by curly braces { } with // a space between each one e.g. { 3 5 8 29 }

 

When defining the equals method for Collections, one can perform a shallow equals or a deep equals. For Collections containing objects, a shallow equals would only compare references wheras a deep equals would use the Object.equals(obj) method to compare. If the equals method is properly implemented to compare contents, then it is a deep equals. If not, it is still a shallow equals. It would be a deep equals using Java's Integer class equals(Integer) method.

 

public boolean equals(IntegerSet set)

// Compare the invoking object to the parameter IntegerSet and return true if

// all members are equal and false if not.

 

public boolean validEntry(int value)

// Checks to see if the value parameter is within the range of the valid

// IntegerSet values which is between 0 and 100 inclusive.

 

IntegerSetTest class

Implement a class that tests the IntegerSet class implemented above. The model code uses two static methods. YOU MUST IMPLEMENT public static void main(String[] args). 

Another method you may want to implement is a public static IntegerSet inputSet() method. It would isolate the input logic from the rest of the testing logic.

import java.util.Scanner;

public class IntegerSetTest {
    public static final Scanner input = new Scanner(System.in);

   // TODO: Follow the prompts and implement the program.
   public static void main(String[] args)

  {
      // initialize twoi sets
      System.out.println("Input Set A");


      System.out.println("Input Set B");


      // TODO: Perform the union of setA and setB to create a third IntegerSet.


      // TODO: Perform the intersection of setA and setB to create a third IntegerSet


      // TODO: Call your IntegerSet toString() methods below
      System.out.println("Set A contains:");


      System.out.println("Set B contains:");

      // TODO: Call your union method to produce a third set.
      System.out.println("Union of A and B contains:");



      // TODO: Call your intersection method to produce a fourth set.
      System.out.println("Intersection of A and B contains:");


      // TODO: Compare two sets to see whether they are equal, see the test cases for
      // output formatting.


      // TODO: test insert of the integer 77
      System.out.println("Inserting 77 into set A...");


      // TODO: try to insert the integer 105
      System.out.println("Inserting 105 into set A...");



      // TODO: print the changed set
      System.out.println("Set A now contains:");



      // TODO: test delete of the integer 77
      System.out.println("Deleting 77 from set A...");



      // TODO: try to delete the integer 105
      System.out.println("Deletinging 105 into set A...");



      // TODO: print the changed set.
      System.out.println("Set A now contains:");


      // TODO: A unit test will be constructed that calls validEntry.


      // TODO: test array parameter constructor
      nt[] intArray = {25, 67, 2, 9, 99, 105, 45, -5, 100, 1, 99, 25};


      // TODO: print out the new set
      System.out.println("New Set contains:");


   }


}

 

Sample Output

Input Set A
Enter number (-1 to end): 36
Enter number (-1 to end): 12
Enter number (-1 to end): 43
Enter number (-1 to end): 25
Enter number (-1 to end): 54
Enter number (-1 to end): 79
Enter number (-1 to end): 83
Enter number (-1 to end): 63
Enter number (-1 to end): 90
Enter number (-1 to end): -1
Input Set B
Enter number (-1 to end): 93
Enter number (-1 to end): 63
Enter number (-1 to end): 83
Enter number (-1 to end): 53
Enter number (-1 to end): 43
Enter number (-1 to end): 103
Enter number (-1 to end): 43
Enter number (-1 to end): 79
Enter number (-1 to end): 13
Enter number (-1 to end): 93
Enter number (-1 to end): -1
Set A contains:
{ 12 25 36 43 54 63 79 83 90 }
Set B contains:
{ 13 25 43 53 63 79 83 93 }
Union of A and B contains:
{ 12 13 25 36 43 53 54 63 79 83 90 93 }
Intersection of A and B contains:
{ 25 43 63 79 83 }
Set A is not equal to set B
Inserting 77 into set A...
Inserting 105 into set A...
Set A now contains:
{ 12 25 36 43 54 63 77 79 83 90 }
Deleting 77 from set A...
Deletinging 105 into set A...
{ 12 25 36 43 54 63 79 83 90 }
Deleting 88 from set A...
Set A now contains elements:
{ 12 25 36 43 54 63 79 83 90 }
New Set contains elements:
{ 1 2 9 25 45 67 99 100 }