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

Hi! when running the main method in ecplise I am getting an error which I have p

ID: 671396 • Letter: H

Question

Hi!

when running the main method in ecplise I am getting an error which I have provided directly below, I think it has something to do with my code in class Set method for intersection but I cant quite figure it out and I was wondering if someone could solve this for me! I appreciate the help!:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
   at java.util.Vector.elementAt(Unknown Source)
   at Main.print(Main.java:28)
   at Main.main(Main.java:17)

import java.util.Vector;

public class Main {
public static void main(String[] args) {
  
    Set a = new Set(), b = new Set();
    a.makeSet();
    print(a);
    b.makeSet();
    print(b);
   a = (Set) a.intersection(b);           //II
    print(a);
    System.out.print("Enter an integer: ");
    int m = new java.util.Scanner(System.in).nextInt();
    System.out.println(a.member(m));
}
  
private static void print(Set x){
    //This method is complete.
    int i;
    for(i = 0; i < x.size()-1; i++)
      System.out.print(x.elementAt(i) + ", ");
    System.out.println(x.elementAt(i));      
}
}

import java.io.*;
import java.util.*;

public class Set extends Vector{
   private Vector<Integer> vec;
   private Random rand;

public Set(){
   vec = new Vector<Integer>();
   rand = new Random();
   }

   public boolean subset(Set other){
       for (int i = 0; i < other.vec.size(); i++){
           if (this.vec.contains(other.vec.get(i)) == false)
               return false;
       }
       return true;
   }

   public Set intersection(Set other){
       Set result = (Set)clone();
       Set common = new Set();
       for (int i = 0; i < other.vec.size(); i++){
           if (result.vec.contains(other.vec.get(i)) == true)
            common.vec.add(other.vec.get(i));
       }
   return common;
   }

   public Set union(Set other){
       Set result = (Set)clone();
    for (int i = 0; i < other.vec.size(); i++){
        if (result.vec.contains(other.vec.get(i)) == false)
            result.vec.add(other.vec.get(i));
    }
    return result;
   }
   public boolean member(int a){
   if (this.vec.contains(a) == true) return true;
   return false;
}
public void makeSet(){
       int n = rand.nextInt(11) + 10;
       int i = 1;
       this.add(new Integer(rand.nextInt(20)));
       while(i <= n){
           int x = rand.nextInt(20);
           if(!this.contains(x)){
               this.addElement(x);
               i++;
           }
       }
}
}

Explanation / Answer

The problem is with the below method present in your Main .class file

private static void print(Set x){
    //This method is complete.
    int i;
    for(i = 0; i < x.size()-1; i++)

{
      System.out.print(x.elementAt(i) + ", ");    
    System.out.println(x.elementAt(i));       //At this line problem in your code, if we are not using parenthesis

}
}

Please put these two statements in parenthasis

Description:

Here you are trying to print the values stored in vector through for loop

Any control statements like if,while,for...etc. take the immediate statement to execute once condition is satisfied,and ignore the condition for next statement .

if we want to execute multiple statements keep it in block(using parenthesis)

example:

for()

{

multiple statements........

}

out put for the above program after execution:

3, 3

8, 8

11, 11

4, 4

17, 17

14, 14

5, 5

16, 16

18, 18

0, 0

7, 7

11, 11

19, 19

15, 15

13, 13

6, 6

17, 17

10, 10

5, 5

1, 1

18, 18

9, 9

3, 3

12, 12

2, 2

4, 4

16, 16

Enter an integer: 4

false

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