Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need to submit this code as clean as possible, can someone help me to create t

ID: 3745395 • Letter: I

Question

I need to submit this code as clean as possible, can someone help me to create the correct comments for the classes, the methods and the variables. 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

Most of your comments are spot on...
I changed some of the comments and added some of my own comments.
If you need all the comments to be new, please reply so I can do it asap.
I formatted the code so it looks even better.

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

    // method to find the face value of the card
    public String getFaceValue()
    {
        return faceValue;
    }

    // method used to get the suit
    public String getSuit()
    {
        return suit;
    }

    // it returns the 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
{

    //total number of cards in a deck
    public static final int NCARDS = 52;
   
    //An array list for saving the card information
    private ArrayList<TheCard> deckOfCards;

    public TheDeck()
        {

        deckOfCards = new ArrayList<TheCard>();
        //variable to store the suit information which has 4 types
        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 to 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;
                }
                //Adding 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 added to hand
    private ArrayList<Integer> cardsToAdd;
  
    int number;
    int cardsInHand;


    public TheHand(int number)
    {
        this.number = number;
        TheDeck d = new TheDeck();        // new Deck
        cardsToAdd = new ArrayList<Integer>();    // define the cards to add
        TheCard cardToAdd;
        handOfCards = new ArrayList<TheCard>();    //define the hand of cards
        Random random = new Random();            // generating a random number
        cardsInHand = 0;
        boolean added = false;                            //keeps track whether card is added or not
        int posToInsert = -1;
        while (cardsInHand < number)
        {
            added = false;                                    // initialising the variable
            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++;
        }
    }
  
    // prints the hand of cards
    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();
            //asking user whether he wants to another set or not
            System.out.println("Do you want to see another hand? <Y/N>");
            printAnother = sc.next();
        }
    }
  
    //printHand 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();
    }
}

********************************************************

***********************NOTE************************
If there is any change you want me to make to the answer, please reply..
If everything is good please rate accordingly :)