Skip to main content
added 8 characters in body; edited tags
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

The Art and Science of JavaThe Art and Science of Java, a course book that uses the ACM library, has an exercise that reads like this.:

Now, this question is a little hard to understand, but nevertheless II've done a class that I think answers this:

Sorry about the commentaries, I. I speak portuguesePortuguese and II've written the commentaries in that language. I hope the code is easy enough to understand.

So my question isI want to know 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 Intconstants int types instead of StringsStrings. 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 acmacm libraries):

The Art and Science of Java, a course book that uses the ACM library has an exercise that reads like this.

Now, this question is a little hard to understand, but nevertheless I done a class that I think answers this:

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 types instead of Strings. 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):

The Art and Science of Java, a course book that uses the ACM library, has an exercise that reads like this:

Now, this question is a little hard to understand, but nevertheless I've done a class that I think answers this:

Sorry about the commentaries. I speak Portuguese and I've written the commentaries in that language. I hope the code is easy enough to understand.

I want to know 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 types instead of Strings. 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 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):

added 18 characters in body
Source Link

I am not sure if they wanted me to make the Constants Int valuestypes instead of Strings. 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.

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.

I am not sure if they wanted me to make the Constants Int types instead of Strings. 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.

minor corrections
Source Link

Now, this questionsquestion 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 toSringtoString.  */

public class Carta {

// Constantes do Naipe (Rank) 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 (Suit) 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;
}

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;
}

Now, this question 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 toString.  */

public class Carta {

// Constantes do Naipe (Rank) 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 (Suit) 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;
}
Source Link
Loading