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

This is a simple assignment, but I don\'t understand the program very well. I ne

ID: 3649591 • Letter: T

Question

This is a simple assignment, but I don't understand the program very well. I need to comment (// or */) the program and explain what each line(s) of code does. Here's the program:


Write a program that evaulatuates a card game called Bridge. the input that represent the cards are :
2C QD TC AD 6C 3D TD 3H 5H 7H AS JH KH

represents the 2 of clubs, queen of diamonds, 10 of clubs, ace of diamonds, and so on. Each pair consists of a rank followed by a suit, where rank is A, 2, . . . , 9, T, J, Q, or K, and suit is C, D, H, or S. Then evaluate the hand by using the following standard bridge values:

Aces count 4
Kings count 3
Queens count 2
Jacks count 1
Voids (no cards in a suit) count 3
Singletons (one card in a suit) count 2
Doubletons (two cards in a suit) count 1
Long suits with more than 5 cards in the suit count 1 for each card over 5 in number


Here is the program



public class Card
{
private int rank;
private char suit;

public static final int ACE_RANK = 14;
public static final int KING_RANK = 13;
public static final int QUEEN_RANK = 12;
public static final int JACK_RANK = 11;

public static final char ACE_CHAR = 'A';
public static final char KING_CHAR = 'K';
public static final char QUEEN_CHAR = 'Q';
public static final char JACK_CHAR = 'J';
public static final char TEN_CHAR = 'T';

private static final String RANKS="..23456789TJQKA";


public Card(String s)
{
rank = RANKS.indexOf(s.charAt(0));
suit = s.charAt(1);
}

public int getRank()
{return rank;}


public char getSuit()
{return suit;}


public String getRankString()
{
if (rank > 10)
return String.valueOf(RANKS.charAt(rank));
else
return String.valueOf(rank);
}

public String toString()
{return "" + RANKS.charAt(rank) + suit;}


public static void main(String[] args)
{
Card c1 = new Card("2C");
Card c2 = new Card("9S");
Card c3 = new Card("TD");
Card c4 = new Card("JH");
Card c5 = new Card("QS");
Card c6 = new Card("KS");
Card c7 = new Card("AD");

System.out.println(c1);
System.out.println(c2);
System.out.println(c3);
System.out.println(c4);
System.out.println(c5);
System.out.println(c6);
System.out.println(c7);
}
}











public class Hand
{

private static final String SUIT_CHARS = "CDHS";
private static final String SUIT_NAMES[]
= { "Clubs", "Diamonds", "Hearts", "Spades" };

private Suit[] suits;


public Hand()
{
suits = new Suit[SUIT_NAMES.length];

for (int suit = 0; suit < suits.length; suit++)
suits[suit] = new Suit(SUIT_NAMES[suit]);
}


public void addCard(Card c)
{
int suitNum = SUIT_CHARS.indexOf(c.getSuit());

suits[suitNum].add(c);
}


public int getValue()
{
int total = 0;

for (int s = 0; s < suits.length; s++)
total += suits[s].getValue();

return total;
}


public void sort()
{
for (int s = 0; s < suits.length; s++)
suits[s].sort();
}


public String toString()
{
StringBuffer result = new StringBuffer();

for (int s = 0; s < suits.length; s++)
{
result.append(suits[s].toString());
result.append(" ");
}

result.append("Points = " + getValue());

return result.toString();
}
}






public class Suit
{

private Card[] cards;
private int count;
private String name;
public Suit(String n)

{
cards = new Card[13];
count = 0;
name = n;
}

public void add(Card c)

{
cards[count] = c;
count++;}





public int getValue()

{
int total = 0;

for (int c = 0; c < count; c++)
{
if (cards[c].getRank() > 10)
total += cards[c].getRank() - 10;
}

if (count < 3)
{
total += 3 - count;
}
else if (count > 5)
{
total += count - 5;
}

return total;
}




public void sort()
{
for (int pass = 0; pass < count - 1; pass++)
{
for (int first = 0; first < count - 1 - pass; first++)
{
if (cards[first].getRank() < cards[first + 1].getRank())
{
Card temp = cards[first];
cards[first] = cards[first + 1];
cards[first + 1] = temp;
}
}
}
}




public String toString()
{
StringBuffer result = new StringBuffer(name + ":");

for (int c = 0; c < count; c++)
result.append(" " + cards[c].getRankString());

return result.toString();
}

public static void main(String[] args)
{
Card c1 = new Card("2C");
Card c2 = new Card("9C");
Card c3 = new Card("TC");
Card c4 = new Card("JC");
Card c5 = new Card("QC");
Card c6 = new Card("KC");
Card c7 = new Card("AC");

Suit clubs = new Suit("Clubs");
clubs.add(c1);
clubs.add(c2);
clubs.add(c3);
clubs.add(c4);
clubs.add(c5);
clubs.add(c6);
clubs.add(c7);

clubs.sort();

System.out.println(clubs);
System.out.println(clubs.getValue());
}
}









import java.util.Scanner;

public class BridgeTest
{
public static void main(String[] args)
{

Hand h = new Hand();

String s = "2C QD TC AD 6C 3D TD 3H 5H 7H AS JH KH";

Scanner scan = new Scanner(s);
while (scan.hasNext())

{h.addCard(new Card(scan.next()));}

h.sort();

System.out.println(h);
}
}

Explanation / Answer

public class Card//definition of class Card { private int rank;//private data can not be manipulated directly by user private char suit;//but can be manipulated with mutator function/methods and such //enumeration for card ranks public static final int ACE_RANK = 14; //static final makes variables unchangable public static final int KING_RANK = 13;//public lets user access these variable directly public static final int QUEEN_RANK = 12; public static final int JACK_RANK = 11; public static final char ACE_CHAR = 'A'; public static final char KING_CHAR = 'K'; public static final char QUEEN_CHAR = 'Q'; public static final char JACK_CHAR = 'J'; public static final char TEN_CHAR = 'T'; private static final String RANKS="..23456789TJQKA";// .. used to shift numbers so that the rank numbers match // the index number (since index starts with 0 in java) //enumerations ends public Card(String s)//definition of mutator Card that takes in card s { rank = RANKS.indexOf(s.charAt(0));//gets rank for card s from first character entered by user //by setting rank equal to the index of the first occurace of that character in RANKS suit = s.charAt(1);//and sets suit of card s by second character input by user } public int getRank()//returns rank {return rank;} public char getSuit()//returns suit {return suit;} public String getRankString()//returns card rank as a string { if (rank > 10)//if rank is less than 10, just return the rank as it is in RANK to valueOf return String.valueOf(RANKS.charAt(rank)); else return String.valueOf(rank);//otherwise, send ranks directly into valueOf } public String toString()//concatenate string {return "" + RANKS.charAt(rank) + suit;} public static void main(String[] args)//main function { Card c1 = new Card("2C");//makes function calls to create a new object called "c1" of class Card Card c2 = new Card("9S"); Card c3 = new Card("TD"); Card c4 = new Card("JH"); Card c5 = new Card("QS"); Card c6 = new Card("KS"); Card c7 = new Card("AD"); System.out.println(c1);//output variables to console System.out.println(c2); System.out.println(c3); System.out.println(c4); System.out.println(c5); System.out.println(c6); System.out.println(c7); } } public class Hand//definition of class Hand { private static final String SUIT_CHARS = "CDHS";//unchangable variables that are not accessible directly by user private static final String SUIT_NAMES[] = { "Clubs", "Diamonds", "Hearts", "Spades" }; private Suit[] suits;//array of type Suit public Hand()//default constructor for class Hand { suits = new Suit[SUIT_NAMES.length];//create Suit array of size 4 for (int suit = 0; suit
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote