Introduction to OOP: Chapter 3: Classes and Methods:
[next]
[previous]
[audio]
[real]
[text]
Card in Java
class Card {
// static values for colors and suits
final public static int red = 0;
final public static int black = 1;
final public static int spade = 0;
final public static int heart = 1;
final public static int diamond = 2;
final public static int club = 3;
// data fields
private boolean faceup;
private int r;
private int s;
// constructor
Card (int sv, int rv) { s = sv; r = rv; faceup = false; }
// access attributes of card
public int rank() { return r; }
public int suit() { return s; }
public int color () {
if (suit() == heart || suit() == diamond)
return red;
return black ; }
public boolean faceUp() { return faceup; }
// perform actions
public void draw (Graphics g, int x, int y) {
... /* omitted */ ...
}
public void flip() { faceup = ! faceup; }
}
Intro OOP, Chapter 3, Slide 28