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 8927087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:08:26+00:00 2026-06-15T08:08:26+00:00

So I fixed most of the problems for this, with help, however now when

  • 0

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);
    }

}
  • 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-15T08:08:27+00:00Added an answer on June 15, 2026 at 8:08 am

    When you try to output your object…

    System.out.println("setA: " + setA);
    

    You’re attempting to convert an object to a String. In order to do that:

    String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. (docs.oracle.com)

    … you need to override the toString(...) method in class IntegerSet:

        public String toString() {
            // Maybe you should use java.lang.StringBuilder instead
            String returnValue = "[Set:";
    
            if (isEmpty())
              returnValue += "---";
    
            for (int i=0; i<a.length; i++) {
              if (isSet(i))
                returnValue += " " + i;
            }
    
            returnValue += "]\n";
    
            return returnValue;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having problems with this website http://cspa.wa.edua.au . The website is fine in most
PROBLEM FIXED FOR NOW. I fixed it by deleting my javascript section for a
Clearly, frames (and frames) have some serious problems when used without discretion; however, they
I fixed all the problems described here (and one additional), and posted the modified
I try to create a table with a fixed column but I faced all
We've been avoiding this PMD warning by moving most of our constructor code into
I'm not entirely sure what has happened. I was debugging a script that worked
My problem does not look like a rare case (even though I could find
FIXED! The problem was I didn't know you had to initialize a connection to
How do people switch fixed elements when scrolling down the page? Here's a perfect

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.