Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7724609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:43:58+00:00 2026-06-01T04:43:58+00:00

This is a programming exercise from Chapter 6 of The Art and Science of

  • 0

This is a programming exercise from Chapter 6 of The Art and Science of Java.

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.

• Getter methods named 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 as in exercise 1. Remember that you can use the +
operator to connect the different parts of the string together, as
shown in the toString implementation for the Rational class in Figure
6-9.

My code:

/*
 * File: Card.java
 * 
 */

public class Card {

    /* Named Constants for ranks*/
    public static final int ACE = 1;
    public static final int JACK = 11;
    public static final int QUEEN = 12;
    public static final int KING = 13;

    /* Named Constants for suits*/
    public static final int CLUBS = 1;
    public static final int DIAMONDS  = 2;
    public static final int HEARTS = 3;
    public static final int SPADES = 4;

    public Card(int rank, int suit) {
        cardRank = rank;
        cardSuit = suit;
    }

    public int getRank() {
        return cardRank;
    }

    public int getSuit() {
        return cardSuit;
    }

    public String toString() {      
        return (toRankName() + " of " + toSuitName());
    }

    private String toRankName() {
        switch (cardRank) {
        case 1:
            return ("Ace");
        case 11:
            return ("Jack");
        case 12:
            return ("Queen");
        case 13:
            return ("King");
        default:
            return ("" + cardRank);
        }
    }

    private String toSuitName() {
        switch (cardSuit) {
        case 1:
            return ("Clubs");
        case 2:
            return ("Diamonds");
        case 3:
            return ("Hearts");
        case 4:
            return ("Spades");
        default:
            return ("Invalid");
        }
    }

    private int cardRank;
    private int cardSuit;
}

The part that I am unsure of is “Getter methods named getRank and getSuit to retrieve the rank and suit components of a card.”

Is the question asking me to return the int value or a String value(Name)?

This is the first time I’ve implemented a class. Apart from the lack of JavaDoc comments, is there anything else wrong with my code?

edit:
Many thanks to everyone that replied!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:43:59+00:00Added an answer on June 1, 2026 at 4:43 am

    Your code looks spot-on as far as the requirements are concerned.

    The only suggestion I have is to use the named constants in the two switch statements:

    private String toRankName() {
        switch (cardRank) {
        case ACE:
            return ("Ace");
        case JACK:
            return ("Jack");
        ...
    

    The requirements seem a little odd in that they force you to use named constants where enums would arguably be more appropriate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is an interview question that I am using as a programming exercise. Input:
As a programming exercise, I've written a Ruby snippet that creates a class, instantiates
I'd like to implement a big int class in C++ as a programming exercise—a
Recently I did a Java programming exercise successfully which was sent by a recruiting
In K.N Kings "C programming: A modern approach", chapter 10 exercise 7, the task
This is code from the book - Programming Interviews Exposed 2nd Edition - Pages
I have been trying to solve this Concurrent Programming exam exercise (in C#): Knowing
I'm doing this programming exercise out of a textbook where we are given an
I am new to functional programming and just switched from haskell (Didn't like it
I am new to functional programming, and now learn Haskell. As an exercise I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.