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

Help completing the last two methods for consecutive straight and consecutive st

ID: 3712547 • Letter: H

Question

Help completing the last two methods for consecutive straight and consecutive straight flush.
Java code:
public class Card {
//INSTANCE VARIABLES
private String suit; //Suit of a card, it goes between Diamond, Spade, Club or Heart
private int rank; // Rank of a card, it goes between 1 till 13 (In order of A, 2, 3,..., 10, J, Q, K)
/**
* A constructor that requires a suit and rank input to initialize a card
* @param suit
* @param rank
*/
//CONSTRUCTORS
public Card(String suit, int rank) {
this.suit = suit;
this.rank = rank;
}
/**
* A constructor that uses a different Card to initialize the new card.
* @param otherC
*/
public Card(Card otherC) {
//Add code
this.rank = otherC.getRank();
this.suit = otherC.getSuit();
}
//OVERRIDED METHODS
/**
* Override of the method toSting this helps us control "how to print a card object."
* @return The string "[R,S]" where R is rank and S is suit
*/
@Override
public String toString() {
return "[" + rank + "," + suit + "]";
}
/**
* Override of the equals method to control how card objects are to be compared.
* @return comparison
*/
@Override
public boolean equals(Object c2) {
if (!(c2 instanceof Card)) {
throw new RuntimeException("Illegal argument to Card.equals()");
}
Card card2 = (Card) c2;
return ((this.getSuit().equals(card2.getSuit())) && // this.suit same as this.getsuit() ??
(this.getRank() == card2.getRank()));
}
//GETTERS AND SETTERS
/**
* Suit Getter
* @return suit
*/
public String getSuit() {
return suit;
}
/**
* Rank Getter
* @return rank
*/
public int getRank() {
return rank;
}
/**
* Suit Setter
* @return suit
*/
public void setSuit(String suit) {
this.suit = suit;
}
/**
* Rank Setter
* @return rank
*/
public void setRank(int rank) {
this.rank = rank;
}
//METHODS
/**
* Checks if two cards have the same suit
* @param card2
* @return boolean
*/
public boolean sameSuitAs(Card card2) {
return ( this.getSuit().equals(card2.getSuit())); //difference of this.suit.getSuit() to this.getSuit(); ?
//this.suit.getSuit(); is undefined???
}
/**
* Checks if two cards have the same rank
* @param card2
* @return boolean
*/
public boolean sameRankAs(Card card2) {
return (this.getRank() == card2.getRank());
}
/**
* Returns if a card is an ace
* @return boolean
*/
public boolean isAnA() {
//Add code
if (this.suit == "Spade" && this.rank==1) {
return true;
}
else return false;
//return this.suit == ("Spade"); // difference between == and equals ?

}
/**
* Checks if two cards have the same rank
* @param c
* @return boolean
*/
public boolean isPair(Card c) {
//Add code

if (sameRankAs(c)){
return true;
}

else {
return false; //Temporary Return
}
}
  
/**
* Checks if three cards have the same suit or rank // instructions were wrong. Its or instead of and
* @param c1, c2
* @return boolean
*/
public boolean isTrio(Card c1, Card c2) {


//Add code
if(this.sameRankAs(c1) && this.sameRankAs(c2) || this.sameSuitAs(c1) && this.sameSuitAs(c2)) {
return true;
}

else {
return false;
}

}
/**
* Checks if four cards have the same suit and rank
* @param c1, c2, c3
* @return boolean
*/
public boolean isFourTuple(Card c1, Card c2, Card c3) {
//Add code

if(this.equals(c1) && this.equals(c2)&&this.equals(c3)) {
return true;
}

else {
return false; //Temporary Return
}
}
  

/**
* A method that checks if the target card c1 is a rank
* higher than the object card.
* @return boolean
*/
public boolean isConsecutive(Card c1) {
return rank + 1 == c1.getRank();
}
  
/**
* A method that returns true as soon a it has found
* that the given card exist in the deck
* @param target
* @return boolean
*/
public boolean cardExistsIn(Card[] deck) {
//Add code
for(int i=0; i<deck.length; i++) {
if(this.equals(deck[i])) {
return true;
}
}
return false;
}
/**
* A method that returns true as soon as it has found an existing pair
* IN THE DECK!!
* @return boolean
*/
public boolean pairExists(Card[] deck) {
for(int i = 0;i<deck.length;i++){
for(int j = i+1;j<deck.length;j++){
if(deck[i].isPair(deck[j])){
return true;
}
}
}
return false;
}
/**
* A method that returns a boolean as soon as it knows if a
* deck is a flush.
* Note: When we say flush, we mean that all the cards in
* the deck are of the same suit.
* @return boolean
*/
public boolean isFlush(Card[] deck) {
//Add code
for(int i=0; i<deck.length; i++) {
if(!deck[0].sameSuitAs(deck[i])) { //if one card is the same as the rest then all are the same
return false; //if i use getsuit.equals it wont check rank??
}
}
return true; //Temporary Return
}
/**
* A method that returns a boolean as soon as it knows if a
* deck is a consecutive straight.
* Note: When we say consecutive straight, we mean that all
* the cards in the deck are in ascending order without
* ordering the cards themselves.
* @return boolean
*/
public boolean isConsecutiveStraight(Card[] deck) {
//Add code
}
/**---------------------------------------------------------
* A method that checks if the deck is a consecutive
* straight flush with the previously mentioned restrains.
* @return
*/
public boolean isConsecutiveStraightFlush(Card[] arr) {
//Add code
return false; //Temporary Return
}
}

Explanation / Answer

Below is your complete code..

public class Card {

// INSTANCE VARIABLES

private String suit; // Suit of a card, it goes between Diamond, Spade, Club

// or Heart

private int rank; // Rank of a card, it goes between 1 till 13 (In order of

// A, 2, 3,..., 10, J, Q, K)

/**

* A constructor that requires a suit and rank input to initialize a card

*

* @param suit

* @param rank

*/

// CONSTRUCTORS

public Card(String suit, int rank) {

this.suit = suit;

this.rank = rank;

}

/**

* A constructor that uses a different Card to initialize the new card.

*

* @param otherC

*/

public Card(Card otherC) {

// Add code

this.rank = otherC.getRank();

this.suit = otherC.getSuit();

}

// OVERRIDED METHODS

/**

* Override of the method toSting this helps us control "how to print a card

* object."

*

* @return The string "[R,S]" where R is rank and S is suit

*/

@Override

public String toString() {

return "[" + rank + "," + suit + "]";

}

/**

* Override of the equals method to control how card objects are to be

* compared.

*

* @return comparison

*/

@Override

public boolean equals(Object c2) {

if (!(c2 instanceof Card)) {

throw new RuntimeException("Illegal argument to Card.equals()");

}

Card card2 = (Card) c2;

return ((this.getSuit().equals(card2.getSuit())) && // this.suit same as

// this.getsuit() ??

(this.getRank() == card2.getRank()));

}

// GETTERS AND SETTERS

/**

* Suit Getter

*

* @return suit

*/

public String getSuit() {

return suit;

}

/**

* Rank Getter

*

* @return rank

*/

public int getRank() {

return rank;

}

/**

* Suit Setter

*

* @return suit

*/

public void setSuit(String suit) {

this.suit = suit;

}

/**

* Rank Setter

*

* @return rank

*/

public void setRank(int rank) {

this.rank = rank;

}

// METHODS

/**

* Checks if two cards have the same suit

*

* @param card2

* @return boolean

*/

public boolean sameSuitAs(Card card2) {

return (this.getSuit().equals(card2.getSuit())); // difference of

// this.suit.getSuit()

// to

// this.getSuit(); ?

// this.suit.getSuit(); is undefined???

}

/**

* Checks if two cards have the same rank

*

* @param card2

* @return boolean

*/

public boolean sameRankAs(Card card2) {

return (this.getRank() == card2.getRank());

}

/**

* Returns if a card is an ace

*

* @return boolean

*/

public boolean isAnA() {

// Add code

if (this.suit == "Spade" && this.rank == 1) {

return true;

} else

return false;

// return this.suit == ("Spade"); // difference between == and equals ?

}

/**

* Checks if two cards have the same rank

*

* @param c

* @return boolean

*/

public boolean isPair(Card c) {

// Add code

if (sameRankAs(c)) {

return true;

}

else {

return false; // Temporary Return

}

}

/**

* Checks if three cards have the same suit or rank // instructions were

* wrong. Its or instead of and

*

* @param c1,

* c2

* @return boolean

*/

public boolean isTrio(Card c1, Card c2) {

// Add code

if (this.sameRankAs(c1) && this.sameRankAs(c2) || this.sameSuitAs(c1) && this.sameSuitAs(c2)) {

return true;

}

else {

return false;

}

}

/**

* Checks if four cards have the same suit and rank

*

* @param c1,

* c2, c3

* @return boolean

*/

public boolean isFourTuple(Card c1, Card c2, Card c3) {

// Add code

if (this.equals(c1) && this.equals(c2) && this.equals(c3)) {

return true;

}

else {

return false; // Temporary Return

}

}

/**

* A method that checks if the target card c1 is a rank higher than the

* object card.

*

* @return boolean

*/

public boolean isConsecutive(Card c1) {

return rank + 1 == c1.getRank();

}

/**

* A method that returns true as soon a it has found that the given card

* exist in the deck

*

* @param target

* @return boolean

*/

public boolean cardExistsIn(Card[] deck) {

// Add code

for (int i = 0; i < deck.length; i++) {

if (this.equals(deck[i])) {

return true;

}

}

return false;

}

/**

* A method that returns true as soon as it has found an existing pair IN

* THE DECK!!

*

* @return boolean

*/

public boolean pairExists(Card[] deck) {

for (int i = 0; i < deck.length; i++) {

for (int j = i + 1; j < deck.length; j++) {

if (deck[i].isPair(deck[j])) {

return true;

}

}

}

return false;

}

/**

* A method that returns a boolean as soon as it knows if a deck is a flush.

* Note: When we say flush, we mean that all the cards in the deck are of

* the same suit.

*

* @return boolean

*/

public boolean isFlush(Card[] deck) {

// Add code

for (int i = 0; i < deck.length; i++) {

if (!deck[0].sameSuitAs(deck[i])) { // if one card is the same as

// the rest then all are the

// same

return false; // if i use getsuit.equals it wont check rank??

}

}

return true; // Temporary Return

}

/**

* A method that returns a boolean as soon as it knows if a deck is a

* consecutive straight. Note: When we say consecutive straight, we mean

* that all the cards in the deck are in ascending order without ordering

* the cards themselves.

*

* @return boolean

*/

public boolean isConsecutiveStraight(Card[] deck) {

int[] n = new int[deck.length];

for (int i = 0; i < deck.length; i++) {

n[i] = -1;

}

for (int i = 0; i < deck.length; i++) {

n[deck[i].getRank()] = 1;

}

for (int i = 0; i < n.length; i++) {

if (n[i] == -1) {

return false;

}

}

return true;

}

/**

* --------------------------------------------------------- A method that

* checks if the deck is a consecutive straight flush with the previously

* mentioned restrains.

*

* @return

*/

public boolean isConsecutiveStraightFlush(Card[] arr) {

if (arr.length > 1) {

long result = 0;

String suit = arr[0].getSuit();

for (int i = 1; i < arr.length; i++) {

if (suit == arr[i].getSuit()) {

result++;

}

}

if (result == arr.length - 1) {

int count = 0;

int rank = arr[0].getRank();

for (int j = 1; j < arr.length; j++) {

if (rank - arr[j].getRank() == -1 || (rank == 14 && arr[j].getRank() == 2)) {

count++;

rank = arr[j].getRank();

if (count == arr.length - 1) {

return true;

}

}

}

}

}

return false;

}

}