I need to submit this code as clean as possible, can someone help me to complete
ID: 3745495 • Letter: I
Question
I need to submit this code as clean as possible, can someone help me to complete the correct comments for the classes, the methods and the variables that I'm missing. I need to create a Javadoc and the documentation is one of the main subjects I'm gonna be evaluated for, so I need it to be as good as possible. Thanks.
This is the code:
public class TheCard
{
String faceValue; // variable declaration for card faceValue
String suit; // variable declaration for card suit
// Constructor for TheCard class that is used to create the card
public TheCard(String faceValue, String suit)
{
super();
this.faceValue = faceValue;
this.suit = suit;
}
// getter method used to get the face value of the card
public String getFaceValue()
{
return faceValue;
}
// getter method used to get the suit
public String getSuit()
{
return suit;
}
// this is toString method returns a String representation of a card
public String tostring()
{
if (suit.equals("Spade"))
{
suit = "u2660";
}
if (suit.equals("Club"))
{
suit = "u2663";
}
if (suit.equals("Heart"))
{
suit = "u2665";
}
if (suit.equals("Diamond"))
{
suit = "u2666";
}
return faceValue + suit;
}
}
***************************************************************************************
import java.util.ArrayList;
public class TheDeck
{
//variable for the number of card in a deck
public static final int NCARDS = 52;
//A array list for the saving the card information
private ArrayList<TheCard> deckOfCards;
public TheDeck()
{
deckOfCards = new ArrayList<TheCard>();
//variable for store the suit information which have 4 type
String suits[] = {"Spade", "Club", "Heart", "Diamond"};
//variable to store the face value of the card
String faceValue;
// A for loop for 4 suit and its 13 card
for (int i = 0; i < 4; i++)
{
for (int j = 1; j <= 13; j++)
{
// A switch case statement for the store the faceValue
switch (j)
{
case 1:
faceValue = "2";
break;
case 2:
faceValue = "3";
break;
case 3:
faceValue = "4";
break;
case 4:
faceValue = "5";
break;
case 5:
faceValue = "6";
break;
case 6:
faceValue = "7";
break;
case 7:
faceValue = "8";
break;
case 8:
faceValue = "9";
break;
case 9:
faceValue = "10";
break;
case 10:
faceValue = "J";
break;
case 11:
faceValue = "Q";
break;
case 12:
faceValue = "K";
break;
case 13:
faceValue = "A";
break;
// if there is another card like Joker than card is invalid
default:
faceValue = "Invalid";
break;
}
//Add the card in the Array list
deckOfCards.add(new TheCard(faceValue, suits[i]));
}
}
}
//printing the deck
public void printDeck()
{
for (int i = 0; i < NCARDS; i++)
{
System.out.println(deckOfCards.get(i).tostring());
}
}
//To get the number of card in the deck
public TheCard getCard(int num)
{
return deckOfCards.get(num);
}
}
****************************************************************************************
import java.util.ArrayList;
import java.util.Random;
class TheHand
{
private ArrayList<TheCard> handOfCards;
/* Keeping an arraylist containing integer value of all cards
*till added to hand
*/
private ArrayList<Integer> cardsToAdd;
int number;
int cardsInHand;
public TheHand(int number)
{
this.number = number;
TheDeck d = new TheDeck();
cardsToAdd = new ArrayList<Integer>();
TheCard cardToAdd;
handOfCards = new ArrayList<TheCard>();
Random random = new Random();
cardsInHand = 0;
boolean added = false;//keeps track whether card is added or not
int posToInsert = -1;
while (cardsInHand < number)
{
added = false;
posToInsert = -1; //pos to insert
int next_card_int = random.nextInt(52);
/* If the random number generated is already present in
*the hand, we will again generate a random number till we
*get a unique random number
*/
while (cardsToAdd.contains(next_card_int))
{
next_card_int = random.nextInt(52);
}
cardToAdd = d.getCard(next_card_int);
int pos;
if (cardsInHand == 0)//if the first card
{
handOfCards.add(cardToAdd);
cardsToAdd.add(next_card_int);
} else
{
//iterate to find exact position to insert
for (pos = 0; pos < cardsInHand; pos++)
{
TheCard tempCard = handOfCards.get(pos);
if (tempCard.getSuit().equals(cardToAdd.getSuit()))//if same suit
{
if (getValue(tempCard.getFaceValue()) <= getValue(cardToAdd.getFaceValue()))//if facevalue of previous card is less
{
posToInsert = pos + 1;
} else
{
posToInsert = pos;
//shiftCards(pos);
break;
}
} else if (posToInsert >= 0) //to sort within suit eq: 9 9 10
/*
*if cards in hand are 9 9 .We want to insert 10 .
*If this statement is not there card sequence becomes 9 10 9.
*So we need to iterate and find the exact location
*to make the sequence 9 9 10
*/
{
break;
}
}
if (!added)
{
handOfCards.add(pos, cardToAdd);
cardsToAdd.add(next_card_int);
added = true;
}
}
cardsInHand++;
}
}
public void printHandOfCards()
{
for (int i = 0; i < number; i++)
{
System.out.println(handOfCards.get(i).tostring());
}
}
private int getValue(String fv) { //get the exact numerical value of a card
switch (fv)
{
case "J":
return 11;
case "Q":
return 12;
case "K":
return 13;
case "A":
return 14;
default:
return Integer.parseInt(fv);
}
}
}
*************************************************************
import java.util.Scanner; // to use the Scanner class
public class TheTest
{
//set the final number of cards in one hand
static final int NUMBER_OF_CARDS_IN_HAND = 13;
//main method to execute
public static void main(String[] args)
{
//Scanner object for entering the value from keyboard
Scanner sc = new Scanner(System.in);
String printAnother = "Y";
while (printAnother.equalsIgnoreCase("Y"))
{
//call to printHand method
printHand();
System.out.println("Do you want to see another hand? <Y/N>");
printAnother = sc.next();
}
}
//printHnad method for information of hands and containing cards.....
public static void printHand()
{
TheHand hand = new TheHand(NUMBER_OF_CARDS_IN_HAND);
System.out.println("Hand: ");
hand.printHandOfCards();
}
}
Explanation / Answer
//class TheCard
public class TheCard
{
String faceValue; // variable declaration for card faceValue
String suit; // variable declaration for card suit
// Constructor for TheCard class that is used to create the card
public TheCard(String faceValue, String suit)
{
super();//calling super class constructor without parameters
this.faceValue = faceValue;//setting value to the variable faceValue
this.suit = suit;//setting value to the variable suit
}
// getter method used to get the face value of the card
public String getFaceValue()
{
return faceValue;//returning faceValue
}
// getter method used to get the suit
public String getSuit()
{
return suit;//returning suit value
}
// this is toString method returns a String representation of a card
public String tostring()
{
if (suit.equals("Spade"))//comparing suit with spade
{
suit = "u2660";//setting suit
}
if (suit.equals("Club"))//comparing suit with club
{//setting suit
suit = "u2663";
}
if (suit.equals("Heart"))//comparing suit with heart
{//setting suit
suit = "u2665";
}
if (suit.equals("Diamond"))//comparing suit with diamond
{//setting suit
suit = "u2666";
}
return faceValue + suit;//returning face value + suit value
}
}
//***************************************************************************************
import java.util.ArrayList;
//class TheDeck
public class TheDeck
{
//variable for the number of card in a deck
public static final int NCARDS = 52;
//A array list for the saving the card information
private ArrayList<TheCard> deckOfCards;
//constructor... without parameters
public TheDeck()
{
//intilizing deckOfCards
deckOfCards = new ArrayList<TheCard>();
//variable for store the suit information which have 4 type
String suits[] = {"Spade", "Club", "Heart", "Diamond"};
//variable to store the face value of the card
String faceValue;
// A for loop for 4 suit and its 13 card
for (int i = 0; i < 4; i++)
{
for (int j = 1; j <= 13; j++)
{
// A switch case statement for the store the faceValue
switch (j)//for each j setting face value.....using case statements
{
case 1:
faceValue = "2";//setting value to the variable faceValue
break;
case 2:
faceValue = "3";//setting value to the variable faceValue
break;
case 3:
faceValue = "4";//setting value to the variable faceValue
break;
case 4:
faceValue = "5";//setting value to the variable faceValue
break;
case 5:
faceValue = "6";//setting value to the variable faceValue
break;
case 6:
faceValue = "7";//setting value to the variable faceValue
break;
case 7:
faceValue = "8";//setting value to the variable faceValue
break;
case 8:
faceValue = "9";//setting value to the variable faceValue
break;
case 9:
faceValue = "10";//setting value to the variable faceValue
break;
case 10:
faceValue = "J";//setting value to the variable faceValue
break;
case 11:
faceValue = "Q";//setting value to the variable faceValue
break;
case 12:
faceValue = "K";//setting value to the variable faceValue
break;
case 13:
faceValue = "A";//setting value to the variable faceValue
break;
// if there is another card like Joker than card is invalid
default:
faceValue = "Invalid";//setting value to the variable faceValue to invalid
break;
}
//Add the card in the Array list
deckOfCards.add(new TheCard(faceValue, suits[i]));
}
}
}
//printing the deck
//method to print deck
public void printDeck()
{
for (int i = 0; i < NCARDS; i++)
{
System.out.println(deckOfCards.get(i).tostring());//printing all cards in deck
}
}
//To get the number of card in the deck
public TheCard getCard(int num)
{
return deckOfCards.get(num);//returns the number of cards in deck
}
}
//****************************************************************************************
import java.util.ArrayList;
import java.util.Random;
//class TheHand
class TheHand
{
//data members
private ArrayList<TheCard> handOfCards;
/* Keeping an arraylist containing integer value of all cards
*till added to hand
*/
private ArrayList<Integer> cardsToAdd;
int number;
int cardsInHand;
//constructor with parameter number
public TheHand(int number)
{
this.number = number;//setting number
TheDeck d = new TheDeck();//initializing TheDeck
cardsToAdd = new ArrayList<Integer>();//initializing cardsToAdd
TheCard cardToAdd;//declaring TheCard variable
handOfCards = new ArrayList<TheCard>();//initializing handOfCards
Random random = new Random();//declaring and initialing random variable
cardsInHand = 0;//setting value to 0
boolean added = false;//keeps track whether card is added or not
int posToInsert = -1;
while (cardsInHand < number)
{
added = false;
posToInsert = -1; //pos to insert
int next_card_int = random.nextInt(52);
/* If the random number generated is already present in
*the hand, we will again generate a random number till we
*get a unique random number
*/
while (cardsToAdd.contains(next_card_int))
{
next_card_int = random.nextInt(52);//generating random number b/w 0-52
}
cardToAdd = d.getCard(next_card_int);//adding card
int pos;
if (cardsInHand == 0)//if the first card
{
handOfCards.add(cardToAdd);
cardsToAdd.add(next_card_int);
} else
{
//iterate to find exact position to insert
for (pos = 0; pos < cardsInHand; pos++)
{
TheCard tempCard = handOfCards.get(pos);
if (tempCard.getSuit().equals(cardToAdd.getSuit()))//if same suit
{
if (getValue(tempCard.getFaceValue()) <= getValue(cardToAdd.getFaceValue()))//if facevalue of previous card is less
{
posToInsert = pos + 1;
} else
{
posToInsert = pos;
//shiftCards(pos);
break;
}
} else if (posToInsert >= 0) //to sort within suit eq: 9 9 10
/*
*if cards in hand are 9 9 .We want to insert 10 .
*If this statement is not there card sequence becomes 9 10 9.
*So we need to iterate and find the exact location
*to make the sequence 9 9 10
*/
{
break;
}
}
if (!added)
{
handOfCards.add(pos, cardToAdd);
cardsToAdd.add(next_card_int);
added = true;
}
}
cardsInHand++;
}
}
//method to print cards in hand
public void printHandOfCards()
{
for (int i = 0; i < number; i++)
{
System.out.println(handOfCards.get(i).tostring());//printing all cards in hamd
}
}
//method to get numerical value
private int getValue(String fv) { //get the exact numerical value of a card
switch (fv)
{
//returning numerical value for each case
case "J":
return 11;
case "Q":
return 12;
case "K":
return 13;
case "A":
return 14;
default:
return Integer.parseInt(fv);
}
}
}
*************************************************************
import java.util.Scanner; // to use the Scanner class
public class TheTest
{
//set the final number of cards in one hand
static final int NUMBER_OF_CARDS_IN_HAND = 13;
//main method to execute
public static void main(String[] args)
{
//Scanner object for entering the value from keyboard
Scanner sc = new Scanner(System.in);
String printAnother = "Y";
while (printAnother.equalsIgnoreCase("Y"))
{
//call to printHand method
printHand();
System.out.println("Do you want to see another hand? <Y/N>");
printAnother = sc.next();
}
}
//printHnad method for information of hands and containing cards.....
public static void printHand()
{
TheHand hand = new TheHand(NUMBER_OF_CARDS_IN_HAND);
System.out.println("Hand: ");
hand.printHandOfCards();
}
}