static
methodsThe Java construct for implementing functions is known as the static
method.
Same name as the class, no return type
public – accessible from all methods of the class and in the scope of where the object is declared
private – known only in the class
Declare:
private
after class definition and create public methods to set and access variablespublic
no return type (do not use static
)public
(do not use static
), but private
helper methods are often usefulExercise from The Java™ Tutorials
What is the output from the following code:
IdentifyMyParts a = new IdentifyMyParts();
IdentifyMyParts b = new IdentifyMyParts();
a.y = 5;
b.y = 6;
a.x = 1;
b.x = 2;
System.out.println("a.y = " + a.y);
System.out.println("b.y = " + b.y);
System.out.println("a.x = " + a.x);
System.out.println("b.x = " + b.x);
System.out.println("IdentifyMyParts.x = " + IdentifyMyParts.x);
Add static
after public
for one of the instance variable (to make it a class variable). What is the side effect?
public class IdentifyMyParts {
private int x = 7;
private int y = 3;
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
Variable scope: what’s the difference between this.x
and x
?
public class UseMyParts {
public static void main(String[] args) {
IdentifyMyParts a = new IdentifyMyParts();
IdentifyMyParts b = new IdentifyMyParts();
a.setX(5);
a.setY(10);
b.setX(2);
b.setY(3);
System.out.println(a.getY());
System.out.println(b.getY());
System.out.println(a.getX());
System.out.println(b.getX());
}
}
Card
classA card has a suit and a rank
Suits The four suits are clubs (♣), diamonds (♦), hearts (♥), and spades (♠).
Ranks The ranks are ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, and king. The ace is the highest card, and the 2 is the lowest, but the ace can also be used as a low card with a value of 1.
Card
class solutionmain
Create a PlayGame
class with a main
method to create an instance of a card and to print it.
What changes to Card
class would you make?
public class PlayGame {
public static void main(String[] args) {
Card card1 = new Card("ace", "hearts");
card1.print();
}
}
And add a method to Card
:
Card
class (demonstration)Card
class – solutionpublic class Card {
private String rank;
private String suit;
private final static String[] RANKS = {"ace", "two", "three", "four",
"five", "six", "seven",
"eight", "nine", "ten",
"jack", "queen", "king"};
public final static String[] SUITS = {"diamonds", "clubs", "hearts", "spades"};
public Card(String rank, String suit) {
if (validRank(rank)) this.rank = rank;
if (validSuit(suit)) this.suit = suit;
}
public Card(int rank, String suit) {
if (rank >= 1 && rank <= 13) this.rank = RANKS[rank-1];
if (validSuit(suit)) this.suit = suit;
}
private Boolean inArray(String value, String[] arr) {
for (String element : arr) {
if (element.equals(value)) return true;
}
return false;
}
private Boolean validRank(String rank) {
return inArray(rank, RANKS);
}
private Boolean validSuit(String suit) {
return inArray(suit, SUITS);
}
public void setRank(String rank) {
if (validRank(rank)) this.rank = rank;
}
public void setRank(int rank) { // overloading
if (rank >= 1 && rank <= 13) this.rank = RANKS[rank-1];
}
public void setSuit(String suit) {
if (validSuit(suit)) this.suit = suit;
}
public String getSuit() {
return suit;
}
public String getRank() {
return rank;
}
public void print() {
System.out.println(rank + " of " + suit);
}
}
PlayGame
classDeck
classCreate a Deck
class that has a color, and all 52 possible cards (use ArrayList
)
Deck
class solutionimport java.util.ArrayList;
public class Deck {
private ArrayList<Card> cards = new ArrayList<Card>();
private String color;
public Deck(String color) {
this.color = color;
for (String s : Card.SUITS) {
for (int i = 1; i <= 13; i++) {
Card thisCard = new Card(i, s);
cards.add(thisCard);
}
}
}
public void print() {
for (int i = 0; i < cards.size(); i++) {
String thisSuit = cards.get(i).getSuit();
String thisRank = cards.get(i).getRank();
System.out.println(thisRank + " of " + thisSuit);
}
}
public Card getCard(int index) {
return cards.get(index);
}
public String getColor() {
return color;
}
}
PlayGame
Random
Use the Random
class to draw a random card from myDeck
An enum type is a special data type that enables for a variable to be a set of predefined constants
public enum suitOptions {
DIAMONDS, HEARTS, SPADES, CLUBS;
}
private suitOptions suit;
// constructor
public Card(suitOptions suit, String rank) {
this.suit = suit;
this.rank = rank;
}
Example of usage:
Read documentation