1.Create class IntegerSet. Each IntegerSet object can hold integers in the range 0-100. The set is represented by an array of bools. Array element a[ i ] is true if integer i is in the set. Array element a[ j ] is false if integer j is not in the set. The parameterless constructor initializes the array to the "empty set" (i.e. a set whose array representation contains all false values).

public class IntegerSet {

    /** 
     * Creates a new instance of IntegerSet   
     */
    boolean a = false;
    int input;

    //implement the constructor
    public boolean IntegerSet(){
        int[]a = new int[101];
        for (int i=0; i < 101; i++)
            for (i = 0;i<a.length;i++){
                if (a[i] == input) {
                    return true;
                    }
                }
        return false;

    }

Can someone see if my understanding is correct?

Recommended Answers

Just go back to the problem definition and work through it one step at a time. It's written so as to help you. Start with the array of booleans with index values 0-100 inclusive. (The size is defined in the problem statement, so you don't need it as a parameter.)

Jump to Post

All 8 Replies

Sir, constructor does not have a return type. What you have there is a method instead of a constructor. From what you have writen, I think you are looking for an array of boolean with the size of the array as your parameter. It also mention that your default value should be false so your constructor should set the values in your array as false.

wow way off target.Hmmmm.

Any more tips to get me on the right track?

Just go back to the problem definition and work through it one step at a time. It's written so as to help you. Start with the array of booleans with index values 0-100 inclusive. (The size is defined in the problem statement, so you don't need it as a parameter.)

how about this?

public class IntegerSet { 
    final static int CAPACITY = 100; 
    int a[];
    public IntegerSet() { 
        a = new int[CAPACITY + 1]; 
        for (int i = 0; i <= CAPACITY; i++) 
            a[i] = 0; 
    }

anyone can make any improvement.i have tried my best.

ok..how to put a[i]= false?

The set is represented by an array of bools

You are still using int. You are suppose to create a boolean array not an int. Try using the code below.

private boolean [] a;

public SwitchStatement(){
    a = new boolean[CAPACITY + 1]; 
    for (int i = 0; i <= CAPACITY; i++) 
        a[i] = false;
}
commented: Giving solution rather then teaching -3

You do not need to initialise the array to false values. The Java Language Spec says:

4.12.5 Initial Values of Variables
Every variable in a program must have a value before its value is used:
• Each class variable, instance variable, or array component is initialized with a default value when it is created (§15.9, §15.10):
...
For type boolean, the default value is false.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.