Exercise 13-4 Work with an enumeration and documentation In this exercise, you’l
ID: 3589191 • 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.
THE FOLLOWING CODE HAS ALREADY BEEN GIVEN:
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("!");
}
}
AND
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
Hi,
PFB details of changes done in the project:
- Proper documentation and javadoc added wherever necessary.
- Created Enum for all four types of constants
- Made Card class enum driven
- Made Main class enum driven
- After making Card enum driven, we can't give illiegal number to suit, this is achieved!
PFB code:
Main Class:
package murach.ui;
import murach.card.Card;
import murach.card.Suit;
/**
* @author user
*
*/
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() == Suit.spades.ordinal()) {
System.out.print("spades");
} else if (card.getSuit() == Suit.hearts.ordinal()) {
System.out.print("hearts");
} else if (card.getSuit() == Suit.clubs.ordinal()) {
System.out.print("clubs");
} else if (card.getSuit() == Suit.diamonds.ordinal()) {
System.out.print("diamonds");
} else {
System.out.print("????");
}
System.out.println("!");
}
}
Card class:
package murach.card;
/**
* @author user
*
*/
public class Card {
/**
* fields declaration
*/
private Suit suit;
private int number;
/**
* @param suit
*/
public void setSuit(int suit) {
this.suit = Suit.values()[suit];
}
public int getSuit() {
return this.suit.ordinal();
}
/**
* @param number
*/
public void setNumber(int number) {
this.number = number;
}
/**
* @return
*/
public int getNumber() {
return this.number;
}
}
Enum Suit :
package murach.card;
/**
* @author user
*
*/
public enum Suit {
/**
* enumeration Suit having array of constants spades, hearts, diamonds and clubs
*/
spades,
hearts,
diamonds,
clubs
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.