Results 1 to 5 of 5

Thread: Java: How To Program (e-Book) exercise

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2007
    Posts
    12

    Java: How To Program (e-Book) exercise

    Hi guys,

    I am using the sixth edition of Java: How To Program, written by Deitel, and now I'm trying to handle the exercises. But one of them is bugging me:

    Set of Integers) Create class IntegerSet. Each IntegerSet object can hold integers in the range 0100. The set is represented by an array of booleans. 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 no-argument constructor initializes the Java array to the "empty set" (i.e., a set whose array representation contains all false values).

    Provide the following methods: Method union creates a third set that is the set-theoretic union of two existing sets (i.e., an element of the third set's array is set to TRue if that element is true in either or both of the existing setsotherwise, the element of the third set is set to false). Method intersection creates a third set which is the set-theoretic intersection of two existing sets (i.e., an element of the third set's array is set to false if that element is false in either or both of the existing setsotherwise, the element of the third set is set to true). Method insertElement inserts a new integer k into a set (by setting a[k] to true). Method deleteElement deletes integer m (by setting a[m] to false). Method toSetString returns a string containing a set as a list of numbers separated by spaces. Include only those elements that are present in the set. Use --- to represent an empty set. Method isEqualTo determines whether two sets are equal. Write a program to test class IntegerSet. Instantiate several IntegerSet objects.

    Anyone who knows how to handle this one, or even better, knows where to find the answers for these exercises?

    Thanks in advance!

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Java: How To Program (e-Book) exercise

    Code:
    public class IntegersSet {
    
        private boolean[] integers;
    
        public IntegersSet() {
            integers = new boolean[100];
        }
    
        public static IntegersSet union(IntegersSet set1, IntegersSet set2) {
            IntegersSet result = new IntegersSet();
            for (int i = 0; i < 100; i++) {
                if (set1.getValue(i) || set2.getValue(i)) {
                    result.insertElement(i);
                }
            }
            return result;
        }
    
        public static IntegersSet intersection(IntegersSet set1, IntegersSet set2) {
            IntegersSet result = new IntegersSet();
            for (int i = 0; i < 100; i++) {
                if (set1.getValue(i) && set2.getValue(i)) {
                    result.insertElement(i);
                }
            }
            return result;
        }
    
        private boolean getValue(int i) {
            return integers[i];
        }
    
        public void insertElement(int i) {
            integers[i] = true;
        }
    
        public void deleteElement(int i) {
            integers[i] = false;
        }
    
        public String toSetString() {
            String result = "";
            for (int i = 0; i < 100; i++) {
                result += ((integers[i]) ? i : "---") + " ";
            }
    
            return result.substring(0, result.length() - 1);
        }
    
        public boolean isEqualTo(IntegersSet anotherSet) {
            for (int i = 0; i < 100; i++) {
                if (anotherSet.getValue(i) != this.getValue(i)) {
                    return false;
                }
            }
            return true;
        }
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2007
    Posts
    12

    Re: Java: How To Program (e-Book) exercise

    Thanks bro!!

  4. #4
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: Java: How To Program (e-Book) exercise

    Quote Originally Posted by scientist_nl
    Anyone who knows how to handle this one, or even better, knows where to find the answers for these exercises?
    You haven't tried?

  5. #5
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Java: How To Program (e-Book) exercise

    Quote Originally Posted by oceanebelle
    You haven't tried?
    here's your answer
    Quote Originally Posted by scientist_nl
    I am using the sixth edition of Java: How To Program, written by Deitel, and now I'm trying to handle the exercises. But one of them is bugging me:
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width