Exercise 13-4 Work with an enumeration and documentation In this exercise, you’l
ID: 3590074 • Letter: E
Question
Exercise 13-4 Work with an enumeration and documentation
In this exercise, you’ll create an enumeration. Then, you’ll retrofit some existing code to use the enumeration.
Review the code
1. Open the project named ch13_ex4_CardSuitTester in the extra_ex_starts folder.
2. Review the code. Note that the Main class creates a Card object and sets its suit to 0 and its number to 1.
3. Run the application. It should display a message that says, “Ace of spades!”.
4. In the Main class, modify the code so it sets the suit to 5.
5. Run the application again. Since there is no suit that corresponds to 5, the application should display a message that says, “Ace of ????!”.
Create an enumeration and use it
6. In the murach.card package, create an enumeration named Suit. This enumeration should specify four suits: spades, hearts, diamonds, and clubs.
7. In the Card class, modify the code so it uses the Suit enumeration as the type for the suit instance variable.
8. In the Main class, modify the displayCard method so it uses the Suit enumeration to check the suit of the card.
9. In the Main class, experiment by modifying the code that sets the suit and number for the Cart object. Note that you can’t set an illegal suit, but you can set an illegal number.
10. Run the application to make sure it works correctly.
Add javadoc comments and generate documentation
11. In the Card class, add javadoc comments. Make sure these comments include @param and @return tags wherever those tags are appropriate.
12. Generate the documentation for the project.
13. View the documentation for the Card class. This documentation should include descriptions for all of the constructors and methods as well as information about all parameters and return values.
14. View the documentation for the Suit enumeration. Note that this documentation includes a method named values returns an array of the constants that are in the Suit enumeration.
Code:
Listed below are the 2 classes
package murach.ui;
import murach.card.Card;
public class Main {
public static void main(String[] args) {
System.out.println("Welcome to the Suit Tester ");
Card card = new Card();
card.setSuit(0);
card.setNumber(1);
displayCard(card);
}
public static void displayCard(Card card) {
if (card.getNumber() == 1) {
System.out.print("Ace");
} else if (card.getNumber() == 11) {
System.out.print("Jack");
} else if (card.getNumber() == 12) {
System.out.print("Queen");
} else if (card.getNumber() == 13) {
System.out.print("King");
} else if (card.getNumber() > 13) {
System.out.print("????");
} else {
System.out.print(card.getNumber());
}
System.out.print(" of ");
if (card.getSuit() == 0) {
System.out.print("spades");
} else if (card.getSuit() == 1) {
System.out.print("hearts");
} else if (card.getSuit() == 2) {
System.out.print("clubs");
} else if (card.getSuit() == 3) {
System.out.print("diamonds");
} else {
System.out.print("????");
}
System.out.println("!");
}
}
package murach.card;
public class Card {
private int suit;
private int number;
public void setSuit(int suit) {
this.suit = suit;
}
public int getSuit() {
return this.suit;
}
public void setNumber(int number) {
this.number = number;
}
public int getNumber() {
return this.number;
}
}
Explanation / Answer
Given below are the modified files and output of Main. Please don't forget to rate the answer if it helped. Thank you.
To indent code eclipse, select code by pressing Ctrl+A and then press Ctrl + i
package murach.ui;
import murach.card.Card;
import murach.card.Suit;
public class Main {
public static void main(String[] args) {
System.out.println("Welcome to the Suit Tester ");
Card card = new Card();
card.setSuit(Suit.HEARTS);
card.setNumber(1);
displayCard(card);
card.setSuit(Suit.CLUBS);
card.setNumber(5);
displayCard(card);
card.setSuit(Suit.SPADES);
card.setNumber(12);
displayCard(card);
card.setSuit(Suit.DIAMONDS);
card.setNumber(13);
displayCard(card);
}
public static void displayCard(Card card) {
if (card.getNumber() == 1) {
System.out.print("Ace");
} else if (card.getNumber() == 11) {
System.out.print("Jack");
} else if (card.getNumber() == 12) {
System.out.print("Queen");
} else if (card.getNumber() == 13) {
System.out.print("King");
} else if (card.getNumber() > 13) {
System.out.print("????");
} else {
System.out.print(card.getNumber());
}
System.out.print(" of ");
if (card.getSuit() == Suit.SPADES) {
System.out.print("spades");
} else if (card.getSuit() == Suit.HEARTS) {
System.out.print("hearts");
} else if (card.getSuit() == Suit.CLUBS) {
System.out.print("clubs");
} else if (card.getSuit() == Suit.DIAMONDS) {
System.out.print("diamonds");
} else {
System.out.print("????");
}
System.out.println("!");
}
}
============================
package murach.card;
/**
* @author raji
*
*/
public class Card {
private Suit suit;
private int number;
/**
* Set the suit of this card to specified parameter.
* @param suit one of the values defined in the enum Suit. Can be SPADES, DIAMONDS, HEARTS, CLUBS
*/
public void setSuit(Suit suit) {
this.suit = suit;
}
/**
* Returns the suit of this card
* @return A Value indicating the suit of this card, value returned is one from enum Suit
*/
public Suit getSuit() {
return this.suit;
}
/**
* The card's face value.
* @param number - should be a number from 1 to 13 representing Ace, 2, ...Jack, Queen, King respectively
*/
public void setNumber(int number) {
this.number = number;
}
/**
* Returns the card's face value
* @return a number from 1-13 representing Ace, 2, ...Jack, Queen, King respectively
*/
public int getNumber() {
return this.number;
}
}
====================
package murach.card;
/**
* Enumeration to represent one the the suits for the Card.
*
*/
public enum Suit {
SPADES, HEARTS, CLUBS, DIAMONDS
}
===============
output
Welcome to the Suit Tester
Ace of hearts!
5 of clubs!
Queen of spades!
King of diamonds!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.