Cael
Cael

Reputation: 556

Trouble with Java Public Enum

I am attempting to simulate a BB84 protocol using Java programming language. Anyway, I am having some trouble with getting the complimentary/invert set of data from the result.

In my program, there is a total of 3 steps involved.

1. Generate 5 random binary number **check** 
---> exp: 10010 
2. Random bases to represent each bits (either rectilinear or diagonal) **check** 
---> exp: RECTILINEAR, RECTILINEAR, DIAGONAL, DIAGONAL, RECTILINEAR  
3. Complimentary bases (Invert bases used in second step) **not check** 
---> exp: DIAGONAL, DIAGONAL, RECTILINEAR, RECTILINEAR, DIAGONAL 

This is my runnable program: here

As you can see, I've attempted to write a class Basis complimentary() in Basis.java that will take in random basis generated and invert the bases used.

public enum Basis {
    RECTILINEAR,
    DIAGONAL;

    public static Basis random() {
        int i = (int)(Math.random()*2);
        if(i==0)
            return Basis.RECTILINEAR;
        else
            return Basis.DIAGONAL;
    }


    public static Basis complimentary() {

            if (Basis.random()==Basis.RECTILINEAR)
            {
                return Basis.DIAGONAL;
            }
            else
            {
                return Basis.RECTILINEAR;
            }
        }

}

But I notice it is generating random bases all over again and my third step does not seem to output the invert set used in the second step. Help is appreciated.

Edited:

So, in the FilterScheme2.java, i referenced FilterScheme1.java in the constructor like this.

public class FilterScheme2 extends AbstractScheme{
    private Filter[] filters;

    public FilterScheme2(int size) {
        super(size);

        filters = new Filter[size];

        FilterScheme1 f = new FilterScheme1(size);//reference to FilterScheme1.java

        for(int i=0;i<size;i++) {
             filters[i] = new Filter(filters[i].getBasis().complimentary()); //generate the second set of complimentary bases (rectilinear/diagonal)  
                }

    }

I tried to output System.out.println(f.toString()); to make sure that I get the same data as FilterScheme1 but it seems like it generate random bases again. What could be the problem?

Upvotes: 0

Views: 97

Answers (1)

jensgram
jensgram

Reputation: 31498

You need to reference what exact instance you want the compliment of. This can be done by passing as argument, or by making complimentary non-static:

public static Basis complimentary(Basis subject) {
  if (subject == Basis.RECTILINEAR)
  {
    return Basis.DIAGONAL;
  }
  else
  {
    return Basis.RECTILINEAR;
  }
}
…
Basis.complimentary(Basis.DIAGONAL); // RECTILINEAR

Or

public Basis complimentary() {
  if (this == Basis.RECTILINEAR)
  {
    return Basis.DIAGONAL;
  }
  else
  {
    return Basis.RECTILINEAR;
  }
}
…
Basis.DIAGONAL.complimentary(); // RECTILINEAR

Upvotes: 1

Related Questions