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: 3745280 • 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

Comments in a program make program understandable for a person who read it he may be your partner in programming or either you who wants to modify program after some time. Too many comments in a program are also is not a good way to write a program.

In Documentation spacing is also necessary for readibility of the program, since you ask for Documentation of code, I am only focous on this not the logic of program here is your code with documentation:-

//here write task of the program_module
//program is written by: Your_Name

public class TheCard
{
   String faceValue;    // card faceValue
   String suit;        // card suit

   public TheCard(String faceValue, String suit)   // Used to create the card
   {
       super();
       this.faceValue = faceValue;
       this.suit = suit;
   }

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


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

   public String tostring()   //returns a String representation of a card
   {
       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;
   }
}

//***************************************************************************************

//here write task of the program_module
//program is written by: Your_Name

import java.util.ArrayList;

public class TheDeck
{
   public static final int NCARDS = 52;   //number of card in a deck
   private ArrayList<TheCard> deckOfCards;   //saving the card information

   public TheDeck()
   {
       deckOfCards = new ArrayList<TheCard>();
       String suits[] = {"Spade", "Club", "Heart", "Diamond"};   //we have 4 type of suit
       String faceValue;//store the face value of the card

       // A for loop for 4 suit and its 13 card
       for (int i = 0; i < 4; i++)
       {
           for (int j = 1; j <= 13; j++)
           {
               // 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);
   }
}

//****************************************************************************************

//here write task of the program_module
//program is written by: Your_Name

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()   //printing the card on hand
   {
       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);
       }
   }
}

//*************************************************************

//here write task of the program_module
//program is written by: Your_Name

import java.util.Scanner;

public class TheTest
{
   static final int NUMBER_OF_CARDS_IN_HAND = 13;   //final number of cards in one hand
  
   public static void main(String[] args)
   {
       Scanner sc = new Scanner(System.in);
       String printAnother = "Y";
       while (printAnother.equalsIgnoreCase("Y"))
       {
           printHand();   //print details of card in hand
           System.out.println("Do you want to see another hand? <Y/N>");
           printAnother = sc.next();
       }
   }

   //print 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();
   }
}