Question

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?

Was it helpful?

Solution

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


}

OTHER TIPS

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]))

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

should be:

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top