The Art and Science of Java, a course book that uses the ACM library has an exercise that reads like this.
Implement a new class called Card that includes the following entries:
• Named constants for the four suits (CLUBS, DIAMONDS, HEARTS, SPADES) and the four ranks that are traditionally represented in words (ACE, JACK, QUEEN, KING). The values of the rank constants should be 1, 11, 12, and 13, respectively.
• A constructor that takes a rank and a suit and returns a Card with those values. • Accessor methods getRank and getSuit to retrieve the rank and suit components of a card.
• An implementation of the toString method that returns the complete name of the card.
Now, this questions is a little hard to understand, but nevertheless I done a class that I think answers this:
/** Ficheiro: Carta.java
*
* Classe que vai representar os diferentes valores e naipes de uma carta de um baralho normal
* de 52 cartas. Exercício para construir uma classe completa juntamente com construtores,
* gets de Valores e Naipes e de método toSring. */
public class Carta {
// Constantes do Naipe das Cartas
public static final String AS = "Ás";
public static final String REI = "Rei";
public static final String DAMA = "Dama";
public static final String VALETE = "Valete";
// Constantes do Valor Facial das Cartas
public static final String COPAS = "Copas";
public static final String PAUS = "Paus";
public static final String OUROS = "Ouros";
public static final String ESPADAS = "Espadas";
/**
* Cria uma nova carta de um baralho regular de 52 cartas.
* @param valor O valor facial da carta (ex. Ás, Rei, 10...)
* @param naipe O naipe da carta
*/
public Carta (int valor, int naipe) {
valorCarta = valor;
numNaipe = naipe;
}
/**
* Obtém o valor facial da carta (existem 13 valores faciais)
* @return O valor facial da carta em String
*/
public String getValorCarta() {
switch (valorCarta) {
case 1: return AS;
case 11: return REI;
case 12: return DAMA;
case 13: return VALETE;
default: return "" + valorCarta;
}
}
/**
* Obtém o naipe da carta (existem 4 naipes)
* @return O naipe da carta em String
*/
public String getNaipeCarta () {
switch (numNaipe) {
case 1: return COPAS;
case 2: return PAUS;
case 3: return OUROS;
case 4: return ESPADAS;
default: return ("Naipe Inválido");
}
}
/**
* Cria uma representação em String da Carta (Valor e Naipe)
* @return Representação em String da Carta (Valor e Naipe)
*/
public String toString () {
return getValorCarta() + " de " + getNaipeCarta();
}
// variáveis de instância que registam o Valor e o Naipe da Carta em integers.
private int valorCarta;
private int numNaipe;
}
Sorry about the commentaries, I speak portuguese and I written the commentaries in that language. I hope the code is easy enough to understand.
So my question is if I have written well the class, according to specifications, especially the Public Constants and the Get Methods.
I am not sure if they wanted me to make the Constants Int values. Also I didn't assign any parameters to the Get Methods, so they will only give me the rank or suit of a new Card Object. I am not sure if they wanted me to attribute an int param to these methods.
As a sidenote, I wrote then a short program that demonstrates this class in action, in choosing and displaying a random card (note that the run method is the main method in acm libraries):
import acm.program.*;
import acm.util.*;
public class testeCarta extends ConsoleProgram{
// Escolhe uma carta aleatóriade um baralho de 52 cartas e imprime no ecrã
public void run () {
println ("Este programa vai seleccionar uma carta aleatória");
Carta cartaAleat = new Carta (rGen.nextInt(1,13),rGen.nextInt(1,4));
println (cartaAleat);
}
// variável de instància que permite a utilização de gerador de números aleatórios
public RandomGenerator rGen = RandomGenerator.getInstance();
}