Question: CODE IN JAVA private boolean wellFormed() { // Invariant: // (1) dummy is not null, but has null suit and rank. // (2) the cards
CODE IN JAVA
private boolean wellFormed() { // Invariant: // (1) dummy is not null, but has null suit and rank. // (2) the cards are linked in a cycle starting and ending // at the dummy. // (3) Whenever c1.next == c2 is in the cycle, the c2.prev == c1 // (4) no cards except the dummy have null suit or rank. // TODO: Implement the invariant: // This code must terminate and must not crash even if there are problems. return true; } USING CLASSES
public class Card { public enum Suit { CLUB, DIAMOND, HEART, SPADE }; public enum Rank { ACE(1), DEUCE(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT (8), NINE(9), TEN(10), JACK(11), QUEEN(12), KING(13);
private final int rank; private Rank(int r) { rank = r; }
public int asInt() { return rank; } }
private final Suit suit; private final Rank rank; private Card prev, next;
private Card() { rank = null; suit = null; } public Card(Rank r, Suit s) { if (r == null || s == null) throw new IllegalArgumentException("Not a legal card"); rank = r; suit = s; }
// getters for all fields:
public Suit getSuit() { return suit; }
public Rank getRank() { return rank; }
public Card getPrevious() { if (prev == null || prev.suit == null) return null; return prev; }
public Card getNext() { if (next == null || next.suit == null) return null; return next; }
// no setters!
@Override /** Return true if the suit and rank are the same. * Caution: do not use this method to check if you have the same card! */ public boolean equals(Object x) { if (!(x instanceof Card)) return false; Card other = (Card)x; return suit == other.suit && rank == other.rank; }
@Override public String toString() { return rank + " of " + suit + "S"; }
/** * An endogenous DLL of card objects. */ public static class Group { private Card dummy;
private Group(boolean ignored) {} // do not change private static boolean reportErrors = true; // do not change private boolean report(String s) { if (reportErrors) System.err.println("Invariant error: " + s); return false; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
