Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Section C: Programming [20 marks] All answers must be written in valid Java code

ID: 3606622 • Letter: S

Question

Section C: Programming [20 marks]

All answers must be written in valid Java code. Though you are not obligated to provide comments in your code, all other Programming Standards must be observed. You may choose to first write your solution in an IDE, and then copy/paste it into the space provided below - ensure that your code is properly indented if you do.

Do not use any built-in Java classes unless they were covered in the online course notes, or if the question specifically says otherwise. For example, you are permitted to use ArrayList, String and Exceptions.

Question 27 (10 points)

Provide the code for the IntSet class in the text-box below. An IntSet object stores a set of unique integers and is further described as follows:

The IntSet class must have at least two private instance variables: data which is an array of int and numDatawhich is of type int. The IntSet class does not store duplicate data items; thus before a new data item is inserted, the data must be checked to determine if that value already exists in the set. The following publicmethods must be present in the class:

A constructor accepting a parameter of type int, which will set the maximum size of the set. The array will never be expanded, and will never be able to hold more than this many elements. In the beginning, the set will have no elements.

boolean contains(int item) - Returns true if item appears in the list and false otherwise.

int cardinality() - Return the number of data items in this set.

boolean add(int item) - Adds item to the list if it is not already in the set. This method returns true if itemwas successfully added, and false if not (i.e. item was already in the set, or the set was full).

IntSet union(IntSet other) - Creates and returns a new IntSet containing all the elements of this set and the IntSet other. The maximum size of the new set is to be the sum of the cardinalities of this set and other.

Explanation / Answer

import java.util.*;

class IntSet {

private ArrayList<Integer> arr;

private int numData;

  

public IntSet(int size) {

numData = size;

}

  

public boolean contains(int num) {

Iterator<Integer> iterator = arr.iterator();

  

boolean yes = false;

  

while(iterator.hasNext()) {

int data = iterator.next();

if ( data == num ) {

yes = true;

break;

}

}

return yes;

}

  

public int cardinality() {

return arr.size();

}

  

public boolean add(int item) {

boolean yes = false;

  

if ( arr.size() < numData) {

arr.add(item);

yes = true;

}

return yes;

}

  

public IntSet union(IntSet other) {

int cardinl = numData + other.numData;

  

IntSet ans = new IntSet(cardinl);

  

  

Iterator<Integer> iterator = arr.iterator();

  

  

while(iterator.hasNext()) {

add( iterator.next());

}

iterator = other.arr.iterator();

while(iterator.hasNext()) {

add( iterator.next());

}

return ans;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote