4

I have to make an array of 100 numbers and then shuffle the first 20 randomly to have 2 different arrays; A and B.

For this assignment I have to check wether the first 20 numbers from Array A are a subset of the first 20 numbers af Array B

Up until now I have this:

import java.util.Random;

public class opgave6 {
 public static void main(String[] args){

  Verzameling a = new Verzameling(20, 3);
  Verzameling b = new Verzameling(20, 4);

  System.out.println(Verzameling.deelverzamelingVan());
 }
}



class Verzameling {
 int[] elementen;
 int elementen2;
 static int aantal2;

 Verzameling(int aantal , int seed) {
  elementen = new int[100];
  int aantal2 = aantal;

  for(int i = 0; i < 100; i++){
   elementen[i] = i;
  }

  Random random1 = new Random(seed);

  for(int i = 0; i < 100; i++){
   int r = random1.nextInt(100);
   int temp;
   temp = elementen[i];
   elementen[i] = elementen[r];
   elementen[r] = temp;
  } 

  printVerzameling(aantal);


 }

 Verzameling(int seed) {

 }

 void printVerzameling(int aantal){
  for (int i = 0; i < aantal; i++){
   System.out.print(elementen[i] + " ");
  } 
  System.out.println();
 }

 static boolean deelverzamelingVan() {
  while (true) {
   for(i = 0; i < aantal2; i++){
    for(j = 0; j < aantal2; j++){
     if(Verzameling.a.elementen[i] = Verzameling.b.elementen[j])
     break;
    }
   }

  } 
 }


}

However, it doesnt work at all because I cannot figure out how to compare the elementen[i] from object A to element[j] from object B. How can i compare the different elements from both objects by using the static method in the same class.

(So Verzameling A and B are both instances of the Verzameling class, with a static method to check if A is subset of B. How can I get the numbers in the array from Verzameling A and B?)

If something is not clear please let me know! I don't need whole solutions, just how I can access the value of elementen[i] from object A and B. thanks!

EDIT:

this is the problem line:

if(Verzameling.a.elementen[i] == Verzameling.b.elementen[j])

thanks for the comment, however it is still erroring when i compile. It says it cannot find symbol about verzameling.a.elementen, i, verzameling.b.elementen and j. I think i am naming it wrong, is it ok to call the variable by saying: classname.objectname.variable of object?

7
  • thanks @ hvgotcodes for the layout edit, I never seem to get it to work? how do you do it? Commented Nov 3, 2010 at 18:51
  • Did you copy and paste this code? Or type it in SO? You shouldn't have a single =; it shuld be ==. But then, it wouldn't compile, so that probably doesn't help you. Commented Nov 3, 2010 at 18:51
  • @javaa, i used the code formatting icon in the editor -- its a bunch of 1s and 0s. Commented Nov 3, 2010 at 18:54
  • I am wondering why cant you use List.containsAll(...) and Arrasy.asList(....) Commented Nov 3, 2010 at 19:08
  • @Pangea: Arrays.asList doesn't work like you expect when given a primitive array. List<int[]> list = Arrays.asList(someIntArray) Commented Nov 3, 2010 at 19:15

4 Answers 4

2

your if statement is jacked up:

if(Verzameling.a.elementen[i] = Verzameling.b.elementen[j])

you are doing assignment, you are not testing anything.

try

Verzameling.a.elementen[i].equals(Verzameling.b.elementen[j])

or

Verzameling.a.elementen[i] == Verzameling.b.elementen[j]

note that you will need to make sure Verzameling.a.elementen[i] is not null if you use the equals method. Also note that for numbers == is ok, but for objects you want to use equals unless you are sure you want == (== means something very specific for objects)

PREEDIT --

Your code is not compiling for a few reasons. The first of which is that your a and b variables are declared in the scope of main, so they are only accessible in main. Moving them up to the class and making them public and static means they can be accessed from anywhere.

EDIT -- I can't believe Im doing someone's homework, but -- this does not work, but at least it compiles

import java.util.Random;

public class opgave6 {
    public static Verzameling a = new Verzameling(20, 3);
    public static Verzameling b = new Verzameling(20, 4);

    public static void main(String[] args) {
        System.out.println("before something");
        System.out.println(Verzameling.deelverzamelingVan());
        System.out.println("after something");
    }
}


class Verzameling {
    int[] elementen;
    int elementen2;
    static int aantal2;

    Verzameling(int aantal, int seed) {
        elementen = new int[100];
        int aantal2 = aantal;

        for (int i = 0; i < 100; i++) {
            elementen[i] = i;
        }

        Random random1 = new Random(seed);

        for (int i = 0; i < 100; i++) {
            int r = random1.nextInt(100);
            int temp;
            temp = elementen[i];
            elementen[i] = elementen[r];
            elementen[r] = temp;
        }

        printVerzameling(aantal);
    }

    // you arent using it -- do so or delete it
    Verzameling(int seed) {
    }

    void printVerzameling(int aantal) {
        for (int i = 0; i < aantal; i++) {
            System.out.print(elementen[i] + " ");
        }
        System.out.println();
    }

    static boolean deelverzamelingVan() {
        for (int i = 0; i < aantal2; i++) {
            for (int j = 0; j < aantal2; j++) {
                int aElementen = opgave6.a.elementen[i];
                int bElementen = opgave6.b.elementen[j];
                System.out.println(String.format("Comparing a[i] to b[i] [%d, %d]", aElementen, bElementen));
                if (aElementen == bElementen)
                    break;
            }
        }

        // need to return something, i dont know what.
        return true;
    }


}
Sign up to request clarification or add additional context in comments.

10 Comments

thanks for the comment, however it is still erroring when i compile. It says it cannot find symbol about verzameling.a.elementen, i, verzameling.b.elementen and j. I think i am naming it wrong, is it ok to call the variable by saying: classname.objectname.variable of object?
@javaaaa if the object is static and the variable is public
the object cant be static right? the method static boolean deelVerzameling is static, but objects cant be static per definition right?
it gives 10 error right now all to do with: opgave63.java:64: cannot find symbol symbol : variable b location: class Verzameling if(Verzameling.a.elementen[i] = Verzameling.b.elementen[j]) ^
@javaaaa you could make Verzameling a and b public static properties on opgave6. I put your code in my IDE and it had more problems than that though....
|
1
if(Verzameling.a.elementen[i] = Verzameling.b.elementen[j])

is assigning, not compaing. I suppose you want ==, but an evenbetter option would to override the equals()

if(Verzameling.a.elementen[i] == Verzameling.b.elementen[j])

or

if(Verzameling.a.elementen[i].equals(Verzameling.b.elementen[j]))

Comments

0

Verzameling.a.elementen[i] = Verzameling.b.elementen[j]

should be:

Verzameling.a.elementen[i] == Verzameling.b.elementen[j]

1 Comment

thanks for the comment, however it is still erroring when i compile. It says it cannot find symbol about verzameling.a.elementen, i, verzameling.b.elementen and j. I think i am naming it wrong, is it ok to call the variable by saying: classname.objectname.variable of object?
0

Sort each of the arrays, compare. Or better yet, put the integers into a set type, and check if one is a subset of the other. Look up the Set interface, use the containsAll() method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.