I just need to make sure that the snippet of code is correct. I don't have all the code as I'm still writing it. If it's possible I need help on the assignment. Maybe clues as to how to do it and maybe point to where I can find the answer. I'll add code as I finish it.
1st snippet.
1. A constructor which accepts a suit and a value and, if these are acceptable, initializes the
instance variables accordingly. If the supplied values are not acceptable, default values are used
instead.
2. A toString method which returns the card value followed by a space, followed by the suit
abbreviation in parentheses. For example, the ace of spades would be A (S). If the value to be
used is not “10”, add an extra trailing space. This will allow a printout of the whole deck to line
up nicely in rows and columns since the 10-cards toString result will have an extra character.
3. Methods called isRed and isBlack that return true if the suit of the card is a red one (hearts,
diamonds) or a black one (spades, clubs), respectively.
4. A method called isFaceCard that returns true if the card is a 10, jack, queen, king or ace.
5. A method called isPair that takes in a Card object as a parameter and returns true if the calling
Card object (this) has the same value as the parameter.
6. A method called equals that returns true if the calling Card and the Card parameter have the
same suit and the same value.
7. A method called compareTo that returns the distance between the suits of the calling Card and
the Card parameter, but if they are the same suits then it should return the distance between
the card values. An easy way to do this is to use the indexOf method (below) for suits or values
and subtract the results.
8. Private methods called validSuit and validValue that takes in a String parameter and return true
if the given String is in the constant array of card suits or values, respectively.
9. A private method called indexOf that takes in a String parameter and an array of Strings and
returns the index in the array where the String parameter is found. If it is not found it should
return -1.
public class Card
{
public static final String[] SUITS = { "S", "H", "C", "D"};
public static final String[] VALUES = { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K",};
public static final String DEFAULT_SUIT= SUIT[0];
public static final String DEFAULT_VALUE= VALUES[0];
private String suit;
private String value;
public String Card ( String x, String y)
{
suit = s;
value = v;
if(v != VALUES)
v = DEFAULT_VALUE;
if(s != SUIT)
s = DEFAULT_SUIT;
}
public String toString()
{
String str = v + " " + s;
return str;
}
public boolean isRed()
{
for(int i = 0; i<SUIT.length;i+=2)
return true;
}
public boolean isBlack()
{
for(int i = 0; i<VALUE.length;i+=1)
return true;
}
public boolean isFaceCard()
{
}
public boolean equals(Card)
{
}
public int compareTo(Card)
{
}
public boolean isPair(Card)
{
}
private boolean validSuit(Card)
{
}
private boolean validValue ( String z)
{
}
private int indexOf(String a, String[])
{
}
}