Question

need help with code it won't compile. import java.util.ArrayList; /** * Counts the ranks of cards...

need help with code it won't compile.

import java.util.ArrayList;
/**
* Counts the ranks of cards in a hand.
*
* @author (your name)
* @version (a version number or a date)
*/
public class CountRank
{
// instance variables - replace the example below with your own
private int rankCount[];
private Hand hand;
private int count;

/**
* Constructor for objects of class CountRank
*/
public CountRank(Hand h)
{
// initialise instance variables
hand = h;
rankCount = new int[15];
count = 15;
for(int i=0;i<15;i++)
{
rankCount[i] = 0;
}
for(Card card: h.cards)
{
rankCount[card.getRank()] += 1;
}
}
  
/**
* Returns true if any rank count is 4.
*/
public boolean fourOfAKind()
{
//
for(int i=2;i<count;i++)
{
if(rankCount[i] ==4)
{
return true;
}
}
return false;
}
  
private int countPairs()
{
int p = 0;
for(int i=2;i<count;i++)
{
if(rankCount[i] ==2)
{
p++;
}
}
return p;
}
  
public boolean onePair()
{
if(countPairs() == 1)
{
return true;
}
return false;
}
  
public boolean noPair()
{
return true;
}
  
public boolean twoPair()
{
if(countPairs() == 2)
{
return true;
}
return false;
}
  
public boolean threeOfAKind()
{
for(int i=2;i<count;i++)
{
if(rankCount[i] == 3)
{
return true;
}
}
return false;
}
  
//Returns true if consecutive rank counts of 1, at indexes min thru max, inclusive.
public boolean Straight(int min, int max)
{
for(int i=min;i<=max;i++)
{
if(rankCount[i] != 1)
{
return false;
}
}
return true;
}
  

public boolean FullHouse()
{
boolean isOnePair = onePair();
boolean isThreeOfAKind = threeOfAKind();
  
if(isOnePair == true && isThreeOfAKind == true)
{
return true;
}
return false;
}   
}
import java.util.ArrayList;
/**
* A hand must be validated during creation, to check that it does not contain duplicate cards.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Hand
{
// instance variables
private ArrayList<Card> cards;
  
public static final int SUITS = 4;
public static final int RANKs = 15;
  
public static final int NO_PAIR = 0;
public static final int ONE_PAIR = 1;
public static final int TWO_PAIR = 2;
public static final int THREE_OF_A_KIND = 3;
public static final int STRAIGHT = 4;
public static final int FLUSH = 5;
public static final int FULL_HOUSE = 6;
public static final int FOUR_OF_A_KIND = 7;
public static final int STRAIGHT_FLUSH = 8;
public static final int ROYAL_FLUSH = 9;

/**
* Constructor for objects of class Hand
*/
public Hand()
{
// initialise instance variables
cards = new ArrayList<Card>(5);
}

/**
* Create a full hand object from the 5 card objects.
*
*/
public Hand(Card first, Card second, Card third, Card fourth, Card fifth)
{
// put your code here
cards.add(first);
cards.add(second);
cards.add(third);
cards.add(fourth);
cards.add(fifth);
// cards[0] = first;
// cards[1] = second;
// cards[2] = third;
// cards[3] = fourth;
// cards[4] = fifth;
}
// return the card at index i from a hand.
public Card getCard(int i)
{
return cards.get(i);
}
  
public void addCard(Card card)
{
if (cards.size() != 5)
cards.add(card);
}
  
//return the category of a hand.
public int category()
{
int category = NO_PAIR;

if (royalFlush())
{
category = ROYAL_FLUSH;
}
else if (straightFlush())
{
category = STRAIGHT_FLUSH;
}
else if (fourOfAKind())
{
category = FOUR_OF_A_KIND;
}
else if (fullHouse())
{
category = FULL_HOUSE;
}
else if (Flush())
{
category = FLUSH;
}
else if (straight())
{
category = STRAIGHT;
}
else if (threeOfAKind())
{
category = THREE_OF_A_KIND;
}
else if ( twoPair())
{
category = TWO_PAIR;
}
else if (onePair());
{
category = ONE_PAIR;
}
  
return category;
}
// return true if this hand is a royal flush.
public boolean royalFlush()
{
boolean isFlush = Flush();
if(isFlush == false)
{
return isFlush;
}
  
if(Straight(10,14) == true)
{
return true;
}
return false;
  
}
  
public boolean Flush()
{
if(hand.getCard(0).getSuit() == hand.getCard(1).getSuit())
{
if(hand.getCard(0).getSuit() == hand.getCard(2).getSuit())
{
if(hand.getCard(0).getSuit() == hand.getCard(3).getSuit())
{
if(hand.getCard(0).getSuit() == hand.getCard(4).getSuit())
{
return true;
}
}
}
}
return false;
}
  
public boolean straightFlush()
{
int suit = hand.cards[0].getSuit();
  
for(Card c : hand.cards)
{
if(c.getSuit() != suit)
{
return false;
}
}
  
for(int i=2;i<10;i++)
{
if(Straight(i,i+4) == true)
{
return true;
}
}
return false;
}
//return true if this hand has exactly one pair.
/**
* Return true if this hand has exactly one of this 6 methods from CountRank class.
*/
public boolean onePair()
{
CountRank cr = new CountRank(this);
// check cr for exactly one pair
return cr.onePair();
}
  
public boolean twoPair()
{
CountRank cr = new CountRank(this);
return cr.twoPair();
}
  
public boolean threeOfAKind()
{
CountRank cr = new CountRank(this);
return cr.threeOfAKind();
}
  
public boolean straight()
{
CountRank cr = new CountRank(this);
return cr.Straight(10,14);
}
  
public boolean fullHouse()
{
CountRank cr = new CountRank(this);
return cr.FullHouse();
}
  
public boolean fourOfAKind()
{
CountRank cr = new CountRank(this);
return cr.fourOfAKind();
}
//return true if hand contains at least one Ace.
/**
* Return true if hand contains at least one Ace.
*/
private boolean hasAce()
{
return true;
  
}
//return true if the rank is in the hand.
//private boolean findrank(int rank)
//{
  
  
//}
/**
* Return a card to the hand.
*
* @return
*/
public String toString()
{
return cards.toString();
}
}   

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Errors in Hand.java:

===============

There is class named Card was used in the code which is not present in the above code. Please make sure that you have in your code and imported properly.

Inside the method Flush(), you are using a reference variable call hand, which is not declared and initialized. Please make sure you initialize it properly.

Straight method should be changed as below:

public boolean Straight(int num1, int num2)
{
    CountRank cr = new CountRank(this);
    return cr.Straight(num1, num2);
}

ArrayList of Cards should be like this:

ArrayList<Card> cards;

Errors in CountRank,java:

There is only one error that is Card class is not recognized here. Please make sure you have Card.java in your code.

Add a comment
Know the answer?
Add Answer to:
need help with code it won't compile. import java.util.ArrayList; /** * Counts the ranks of cards...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • package cards; import java.util.ArrayList; import java.util.Scanner; public class GamePlay { static int counter = 0; private...

    package cards; import java.util.ArrayList; import java.util.Scanner; public class GamePlay { static int counter = 0; private static int cardNumber[] = {1,2,3,4,5,6,7,8,9,10,11,12,13}; private static char suitName[] = { 'c', 'd', 'h', 's' }; public static void main(String[] args) throws CardException, DeckException, HandException { Scanner kb = new Scanner(System.in); System.out.println("How many Players? "); int numHands = kb.nextInt(); int cards = 0; if (numHands > 0) { cards = 52 / numHands; System.out.println("Each player gets " + cards + " cards\n"); } else...

  • Deck of Cards Program I need help printing a flush, which is showing the top 5...

    Deck of Cards Program I need help printing a flush, which is showing the top 5 cards of the same suite. Below is the code I already have that answers other objectives, such as dealing the cards, and finding pairs. Towards the end I have attempted printing a flush, but I cannot figure it out. public class Shuffler {    /**    * The number of consecutive shuffle steps to be performed in each call    * to each sorting...

  • I'm currently writing a program based on stub poker and trying to deal cards to the...

    I'm currently writing a program based on stub poker and trying to deal cards to the players, show their hands, and determine the winner, and lastly ask them to if they want to play another hand. I'm probably going to have to write a method to compare hands but i really need to just deal the cards and store in their hands Any help or suggestions? Here are my current classes. public class PlayingCard {    private final Suit suit;...

  • Java BlackJack Game: Help with 1-4 using the provided code below Source code for Project3.java: import...

    Java BlackJack Game: Help with 1-4 using the provided code below Source code for Project3.java: import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class Project3 extends JFrame implements ActionListener { private static int winxpos = 0, winypos = 0; // place window here private JButton exitButton, hitButton, stayButton, dealButton, newGameButton; private CardList theDeck = null; private JPanel northPanel; private MyPanel centerPanel; private static JFrame myFrame = null; private CardList playerHand = new CardList(0); private CardList dealerHand = new CardList(0);...

  • in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame {     /**...

    in java coorect this code & screenshoot your output ---------------------------------------------------------------------- public class UNOGame {     /**      * @param args the command line arguments      */         public static void main(String[] args) {       Scanner input=new Scanner(System.in);          Scanner input2=new Scanner(System.in);                             UNOCard c=new UNOCard ();                UNOCard D=new UNOCard ();                 Queue Q=new Queue();                           listplayer ll=new listplayer();                           System.out.println("Enter Players Name :\n Click STOP To Start Game..");        String Name = input.nextLine();...

  • Please use my code to implement the above instructions. My Grid class: import java.util.ArrayList; import java.util.Collections;...

    Please use my code to implement the above instructions. My Grid class: import java.util.ArrayList; import java.util.Collections; class Grid { private boolean bombGrid[][]; private int countGrid[][]; private int numRows; private int numColumns; private int numBombs; public Grid() { this(10, 10, 25); }    public Grid(int rows, int columns) { this(rows, columns, 25); }    public Grid(int rows, int columns, int numBombs) { this.numRows = rows; this.numColumns = columns; this.numBombs = numBombs; createBombGrid(); createCountGrid(); }    public int getNumRows() { return numRows;...

  • Please help to make the program working. Can not compile. import java.io.*; import java .util.*; public...

    Please help to make the program working. Can not compile. import java.io.*; import java .util.*; public class Puzzlee {                 static int NN = 9; // Grid Size // sample input static int myGrid[][] = {                                                                                                                                                                                                                                                                                {0,0,0,1,0,5,0,6,8},                                                                                                                 {0,0,0,0,0,0,7,0,1},                                                                                                                 {9,0,1,0,0,0,0,3,0},                                                                                                                 {0,0,7,0,2,6,0,0,0},                                                                                                                 {5,0,0,0,0,0,0,0,3},                                                                                                                 {0,0,0,8,7,0,4,0,0},                                                                                                                 {0,3,0,0,0,0,8,0,5},                                                                                                                 {1,0,5,0,0,0,0,0,0},                                                                                                                 {7,9,0,4,0,1,0,0,0}                                                                                                 };    static class myCell {                 int myRow, myColumn;                 public myCell(int myRow, int myColumn)                 {                                 super();                                ...

  • SIMPLE: PSEUDOCODE FOR THE CODE BELOW Bingo.java package bingo; import java.util.Scanner; import java.util.Random; public class Bingo...

    SIMPLE: PSEUDOCODE FOR THE CODE BELOW Bingo.java package bingo; import java.util.Scanner; import java.util.Random; public class Bingo {        public static BingoCard gameCard;     public static int totalGamesWon = 0;        public static void main(String[] args) {               //Use do-while loop to do the following:         //1. instantiate a gameCard         //2. call the method playGame()         //3. call the method determineWinner()         //4. Ask user if they wish to play again? (1 = yes; 2 = no)...

  • This is my code for my game called Reversi, I need to you to make the...

    This is my code for my game called Reversi, I need to you to make the Tester program that will run and complete the game. Below is my code, please add comments and Javadoc. Thank you. public class Cell { // Displays 'B' for the black disk player. public static final char BLACK = 'B'; // Displays 'W' for the white disk player. public static final char WHITE = 'W'; // Displays '*' for the possible moves available. public static...

  • *JAVA* Can somebody take a look at my current challenge? I need to throw a different...

    *JAVA* Can somebody take a look at my current challenge? I need to throw a different exception when a)(b is entered. It should throw "Correct number of parenthesis but incorrect syntax" The code is as follows. Stack class: public class ArrayStack<E> {    private int top, size;    private E arrS[];    private static final int MAX_STACK_SIZE = 10;    public ArrayStack() {        this.arrS = (E[]) new Object[MAX_STACK_SIZE];        this.top = size;        this.size = 0;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT