-1

Alright, My issue is that the 2nd if and 3rd if statement is throwing up an Operator '<' '>' is undefined.

It should be two int values and show be able to do lesser or greater, not sure why it's not working on my end. Here are the two codes:

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

    Die firstDie = new Die( );
    Die secondDie = new Die( );

    if (firstDie == secondDie) {
    System.out.println("First die is " + firstDie.getValue( ));
    System.out.println("Next die is " + secondDie.getValue( ));
    System.out.println("The two dice are the same!");
    }

        if (firstDie > secondDie) {
            System.out.println("First die is " + firstDie.getValue( ));
            System.out.println("Next die is " + secondDie.getValue( ));
            System.out.println("Die One: " + firstDie + " is greater than Die Two: " + secondDie);
        }
            if (firstDie < secondDie) {
                System.out.println("First die is " + firstDie.getValue( ));
                System.out.println("Next die is " + secondDie.getValue( ));
                System.out.println("Die One: " + firstDie + " is less than Die Two: " + secondDie);
            }

}

}

And:

public class Die {
private int value;
private static final int HIGHEST_DIE_VALUE = 6;
private static final int LOWEST_DIE_VALUE = 1;

   public Die() {
         value = ((int)(Math.random() * 100) % HIGHEST_DIE_VALUE + LOWEST_DIE_VALUE);
} public int getValue() { return value; } }
  • Die are not ints. You probaby want to compare `firstDie.getValue()` with same for secondDie. Also your `==` will fail since that checks for reference equality. And unless you give your Die class a decent `equals` and `hashCode` method the equals method will fail. Unless you also compare *values*. – DontKnowMuchBut Getting Better Nov 26 '16 at 17:55

3 Answers3

1

Die is a custom type (not primitive type like int, long, etc..), so in order to tell JVM that how two Die objects can be compared (i.e., they are equal or not equal), your Die class should implement Comparable as shown below (Also, as a side note, use else if for validating the same condition)

Die class:

public class Die implements Comparable<Die> {

        private int value;
        private static final int HIGHEST_DIE_VALUE = 6;
        private static final int LOWEST_DIE_VALUE = 1;

         public Die() {
                 value = ((int)(Math.random() * 100)% 
                     HIGHEST_DIE_VALUE + LOWEST_DIE_VALUE);
         } 

        public int getValue() { 
            return value; 
        }

        @Override
        public int compareTo(Die o) {
            if(this.value < o.getValue()) {
                return -1;
            } else if(this.value > o.getValue()) {
                return 1;
            }
            return 0;
        } 

         @Override
         public boolean equals(Object obj) {
           Die die = (Die)obj;
           if (this.getValue() == die.getValue()) {
            return true;
           } else {
            return false;
           }
       }

       @Override
       public int hashCode() {
         return value;
       }
    }

Usage:

public static void main(String[] args) {
        Die firstDie = new Die( );
        Die secondDie = new Die( );

        if (firstDie.compareTo(secondDie) == 0) {
            System.out.println("First die is " + firstDie.getValue( ));
            System.out.println("Next die is " + secondDie.getValue( ));
            System.out.println("The two dice are the same!");
        } else if (firstDie.compareTo(secondDie) > 0) {
             System.out.println("First die is " + firstDie.getValue( ));
            System.out.println("Next die is " + secondDie.getValue( ));
            System.out.println("Die One: " + firstDie + " 
                 is greater than Die Two: " + secondDie);
        } else if (firstDie.compareTo(secondDie) < 0) {
            System.out.println("First die is " + firstDie.getValue( ));
            System.out.println("Next die is " + secondDie.getValue( ));
            System.out.println("Die One: " + firstDie + " is 
                     less than Die Two: " + secondDie);
        }
    }

Also, as shown above, it is best practice to override equals() & hashcode() whenever you implement Comparable.

Vasu
  • 21,832
  • 11
  • 51
  • 67
  • Might as well also give the class an equals and a hashCode method so that the comparison result agrees with the functional equality and hashCode test. i.e., if compareTo returns 0, then the contract stipulates that the two classes ought to be equal and have the same hashcode. – DontKnowMuchBut Getting Better Nov 26 '16 at 17:59
  • Sure, updating that... – Vasu Nov 26 '16 at 18:00
0

you just need to add a get value after each of the Dice here is the fixed code

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

Die firstDie = new Die( );
Die secondDie = new Die( );

if (firstDie.getValue() == secondDie.getValue()) {
System.out.println("First die is " + firstDie.getValue( ));
System.out.println("Next die is " + secondDie.getValue( ));
System.out.println("The two dice are the same!");
}

    if (firstDie.getValue() > secondDie.getValue()) {
        System.out.println("First die is " + firstDie.getValue( ));
        System.out.println("Next die is " + secondDie.getValue( ));
        System.out.println("Die One: " + firstDie.getValue() + " is greater than Die Two: " + secondDie.getValue());
    }
        if (firstDie.getValue() < secondDie.getValue()) {
            System.out.println("First die is " + firstDie.getValue( ));
            System.out.println("Next die is " + secondDie.getValue( ));
            System.out.println("Die One: " + firstDie.getValue() + " is less than Die Two: " + secondDie.getValue());
        }

}

you need to add it after all of them because firstdie and second die only get you the seed used to generate the number not the actual number

Austin
  • 726
  • 4
  • 22
0

You can implement comparable like the javaguy's answer, or you can use firstDie.getValue == secondDie.getValue and firstDie.getValue > secondDie.getValue and so on so you are comparing ints instead of objects. It would look like this:

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

Die firstDie = new Die( );
Die secondDie = new Die( );

if (firstDie.getValue() == secondDie.getValue()) {
System.out.println("First die is " + firstDie.getValue( ));
System.out.println("Next die is " + secondDie.getValue( ));
System.out.println("The two dice are the same!");
}

    if (firstDie.getValue() > secondDie.getValue()) {
        System.out.println("First die is " + firstDie.getValue( ));
        System.out.println("Next die is " + secondDie.getValue( ));
        System.out.println("Die One: " + firstDie + " is greater than Die Two: " + secondDie);
    }
        if (firstDie.getValue() < secondDie.getValue()) {
            System.out.println("First die is " + firstDie.getValue( ));
            System.out.println("Next die is " + secondDie.getValue( ));
            System.out.println("Die One: " + firstDie + " is less than Die Two: " + secondDie);
        }

}
cat16
  • 33
  • 9