-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCard.java
More file actions
120 lines (104 loc) · 3 KB
/
Card.java
File metadata and controls
120 lines (104 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Card.java
*
* A blueprint class for objects that represent a single playing card
* for a game in which cards have both colors and numeric values.
*
*/
public class Card {
public static final int MIN_VALUE = 0;
public static final String[] COLORS = {"blue", "green", "red", "yellow"};
public static final int MAX_VALUE = 9;
private String color;
private int value;
/*
* isValidColor - takes the name of a color and returns true if it is valid and false otherwise
* inputs:
* color - a String representing the name of a color
*/
public static boolean isValidColor(String color) {
for (int i = 0; i < COLORS.length; i++) {
if (COLORS[i].equals(color)) {
return true;
}
}
return false;
}
/*
* A constructor that calls the mutator methods to initialize the
* fields, so that they can perform the necessary error-checking.
*/
public Card(String color, int value) {
this.setColor(color);
this.setValue(value);
}
/*
* setColor - a mutator method that changes a Card's color
*
* precondition: color must be an element of COLORS
*/
public void setColor(String color) {
if (isValidColor(color) == false) {
throw new IllegalArgumentException();
}
this.color = color;
}
/*
* setValue - a mutator method that changes a Card's value
*
* precondition: value must be between MIN_VALUE and MAX_VALUE inclusive
*/
public void setValue(int value) {
if (value < MIN_VALUE || value > MAX_VALUE) {
throw new IllegalArgumentException();
}
this.value = value;
}
/*
* getColor - an accessor method for a Card's color
*/
public String getColor() {
return this.color;
}
/*
* getvalue - an accessor method for a Card's value
*/
public int getValue() {
return this.value;
}
/*
* toString - returns a string representation of the Card
* of the form "color value".
*/
public String toString() {
return this.color + " " + this.value;
}
/*
* matches - takes another Card and returns true if the called Card
* matches the color and/or value of the other Card, and false otherwise
*/
public boolean matches(Card other) {
if (other == null) {
return false;
}
if (this.color == other.color || this.value == other.value) {
return true;
} else {
return false;
}
}
/*
* equals - takes another Card and returns true if the two Card objects
* have the same color and the same value, and false otherwise
*/
public boolean equals(Card other) {
if (other == null) {
return false;
}
if (this.color == other.color && this.value == other.value) {
return true;
} else {
return false;
}
}
}