Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8925839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:48:25+00:00 2026-06-15T07:48:25+00:00

I need help with this project. I cannot seem to find a way to

  • 0

I need help with this project. I cannot seem to find a way to get the test code to run. I am not allowed to change the test code, only the IntegerSet class. I am still learning, please help me in figuring this out.

I know the codes are long so I placed the comments PROBLEM! near the two spots in the test and current code where there seems to be a red flag.
Maybe toString() has to do with it but not sure.

Current code:

  public class IntegerSet {
  private 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);
  }
  // PROBLEM!!!!

  // The union of this set and another set
  public IntegerSet union(IntegerSet otherSet) {
    IntegerSet newSet = new IntegerSet(this);

    // newSet is now a copy of the current set,  Next we
    // want to union with set a

    for(int i=0; i<a.length; i++) {
      if (otherSet.isSet(i))
        newSet.insertElement(i);
    }

    return newSet;
  }
  // PROBLEM!!!
  // The intersection of this set and another set
  public IntegerSet intersection(IntegerSet otherSet) {
    IntegerSet newSet = new IntegerSet(this);

    // newSet is now a copy of the current set,  Next we
    // want to intersect with set a

    for(int i=0; i<a.length; i++) {
      if (!otherSet.isSet(i))
        newSet.deleteElement(i);
    }

    return newSet;
  }


  // 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 cardinality() {
    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: // PROBLEM!   
                    System.out.println("The intersection of setA and setB is: " + IntegerSet.intersection(setA, setB));
                    break;
                case 6: // PROBLEM!
                    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);
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:48:26+00:00Added an answer on June 15, 2026 at 7:48 am
     public static IntegerSet union(IntegerSet otherSet, IntegerSet nextSet) {
    
        for(int i=0; i<a.length; i++) {
          if (otherSet.isSet(i))
            nextSet.insertElement(i);
        }
    
        return nextSet;
      }
    
      public static IntegerSet intersection(IntegerSet otherSet, IntegerSet nextSet) {
    
        for(int i=0; i<a.length; i++) {
          if (!otherSet.isSet(i))
            nextSet.deleteElement(i);
        }
    
        return nextSet;
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey Guys...need help. Working on a project and get this error on the Output
Not exactly about programming, but I need help with this. I'm running a development
Sorry to bother again, but I really need help transforming this Python2 code into
I really need help this time - Joomla 2.5 and latest K2. I'm using
i need help with this. I'am using JQuery's Dynamic Form plug-in, it duplicates my
I need help finishing this statement. It is frustrating that two of the PHP
I really need help with this last part of my program. I need to
I Really need help in this ... I'm building an application where I need
I really need help on this one. I am having a simple login form
I am new to android and need help on this. I have two views

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.