I have to create a month class in Java - java

One of the requirements is a getMonthName method that returns the name if the month, i.e. January is 1. And a toString that returns the month name (String represesentation) , and I'm just worried that there's an easier way for what I'm doing than this:
import java.util.Scanner;
import java.io.*;
public class Month {
private int monthNum = 0;
private String monthName;
String monthOne = "JANUARY";
String monthTwo = "FEBRUARY";
String monthThree = "MARCH";
String monthFour = "APRIL";
String monthFive = "MAY";
String monthSix = "JUNE";
String monthSeven = "JULY";
String monthEight = "AUGUST";
String monthNine = "SEPTEMBER";
String monthTen = "OCTOBER";
String monthEleven = "NOVEMBER";
String monthTwelve = "DECEMBER";
//CONSTRUCTORS
public Month()
{
monthNum = 1;
}
public Month(int monthNum)
{
this.monthNum = monthNum;
if ((monthNum > 12) || (monthNum <1))
{
monthNum = 1;
}
}
public Month(String monthOne, String monthTwo, String monthThree, String monthFour, String monthFive, String monthSix, String monthSeven,
String monthEight, String monthNine, String monthTen, String monthEleven, String monthTwelve)
{
monthName.toUpperCase();
if (monthName.equals(monthOne))
{
monthNum = 1;
}
else if (monthName.equals(monthTwo))
{
monthNum = 2;
}
else if (monthName.equals(monthThree))
{
monthNum = 3;
}
else if (monthName.equals(monthFour))
{
monthNum = 4;
}
else if (monthName.equals(monthFive))
{
monthNum = 5;
}
else if (monthName.equals(monthSix))
{
monthNum = 6;
}
else if (monthName.equals(monthSeven))
{
monthNum = 7;
}
else if (monthName.equals(monthEight))
{
monthNum = 8;
}
else if (monthName.equals(monthNine))
{
monthNum = 9;
}
else if (monthName.equals(monthTen))
{
monthNum = 10;
}
else if (monthName.equals(monthEleven))
{
monthNum = 11;
}
else
{
monthNum = 12;
}
}
//METHODS
public void setMonthNum(int monthNum)
{
this.monthNum = monthNum;
if ((monthNum >12) || (monthNum<1))
{
monthNum = 1;
}
}
public int getMonthNumber()
{
return monthNum;
}
public String getMonthName()
{
if (monthNum == 1)
{
return "January";
}
else if (monthNum == 2)
{
return "February";
}
else if (monthNum == 3)
{
return "March";
}
else if (monthNum == 4)
{
return "April";
}
else if (monthNum == 5)
{
return "May";
}
else if (monthNum == 6)
{
return "June";
}
else if (monthNum == 7)
{
return "July";
}
else if (monthNum == 8)
{
return "August";
}
else if (monthNum == 9)
{
return "September";
}
else if (monthNum == 10)
{
return "October";
}
else if (monthNum == 11)
{
return "November";
}
else
{
return "December";
}
}
public String toString()
{
if (monthNum == 1)
{
return "January";
}
else if (monthNum == 2)
{
return "February";
}
else if (monthNum == 3)
{
return "March";
}
else if (monthNum == 4)
{
return "April";
}
else if (monthNum == 5)
{
return "May";
}
else if (monthNum == 6)
{
return "June";
}
else if (monthNum == 7)
{
return "July";
}
else if (monthNum == 8)
{
return "August";
}
else if (monthNum == 9)
{
return "September";
}
else if (monthNum == 10)
{
return "October";
}
else if (monthNum == 11)
{
return "November";
}
else
{
return "December";
}
}
}
Am I missing something or is this the best way at my level to do this?

If you must implement a Month class yourself then look into enums, otherwise
check out the docs for java.time.Month – Try something like this...
import java.time.Month;
public Month getMonthName( int monthNum )
{
return Month.of( monthNum );
}
Or simply...
System.out.println( Month.of( monthNumber ) );

Here are some suggestions to improve your class:
use KeyValuePair(example, map) to store the month list, since, it would be easy to maintain and retrieve information.
If you use keyValuePair collection you can access the month using id directly rather than using nested if statements.(as you did in getMonthName() and toString() method)

Another approach you could use is HashMap where key would be the month's number and value is its name. Using HashMap you can also get the key based on the value and vice-versa which seems you want to do.
Also try avoiding too much else ifs. You can use switch instead.

It is simply
class Month {
private static String monthNames[] = {
"January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December"};
private int monthNumber;
public Month(int n) {
if (n < 1 || n > 12) {
monthNumber = 1;
} else {
monthNumber = n;
}
}
public String getMonthName() {
return monthNumber[monthNumber - 1];
}
#Override
public String toString() {
return getMonthName();
}
}

Related

Getting a return line error for my method?

public static boolean isValidDate(int month, int day) {
if (month >= 3 && month <= 5) {
if (month == 3) {
if (day >= 1 && day <= 31) {
return true;
} else {
return false;
}
} else if (month == 4) {
if (day >= 1 && day <= 30) {
return true;
} else {
return false;
}
} else if (month == 5) {
if (day >= 1 && day <= 15) {
return true;
} else {
return false;
}
}
} else {
return false;
}
}
Get these errors:not sure how to fix them im returning everything.
BoxOffice.java:81: error: missing return statement
}
BoxOffice.java:85: error: missing return statement
}
The compiler isn't smart enough to deduce that the inner if covers every scenario in the range of the outer if. Just change
else if(month == 5) {
to
else { // month must be 5 here
shmosel's answer describes the problem and the least disruptive fix.
Personally, I'd write this as a switch, and avoid writing the long if/else statements to check the day:
switch (month) {
case 3:
return (day >= 1 && day <= 31);
case 4:
return (day >= 1 && day <= 30);
case 5:
return (day >= 1 && day <= 15);
default:
return false;
}
you need to transforme the else if (month == 5) to else ....
public static boolean isValidDate(int month, int day)
{
if (month >= 3 && month <= 5)
{
if (month == 3)
{
if (day >= 1 && day <= 31)
{
return true;
}
else
{
return false;
}
}
else if (month == 4)
{
if (day >= 1 && day <= 30)
{
return true;
}
else
{
return false;
}
}
else
{
if (day >= 1 && day <= 15)
{
return true;
}
else
{
return false;
}
}
}
else
{
return false;
}
}
Try making a boolean, and instead of returning in the if statement, change the boolean and return at the end of the method. Here is an example with your code.
public static boolean isValidDate (int month, int day) {
boolean result = true; //This must be set to a value to avoid compiler errors
if(month >= 3 && month <= 5) {
if(month == 3) {
if(day >= 1 && day <= 31) {
result = true;
}
else {
result = false;
}
}
else if(month == 4) {
if(day >= 1 && day <= 30) {
result = true;
}
else {
result = false;
}
}
else if(month == 5) {
if(day >= 1 && day <= 15) {
result = true;
}
else {
result = false;
}
}
}
else {
result = false;
}
return result;
}
Hope that helps!

Looping through array just printing classname#hascode

I am trying to work through an assignment and currently stumped as to what my issue could be. I've read through several post and attempted to resolve without success. I know a lot of my code could be simplified, but this is a progression through the text so a lot of stuff has not yet been covered...
This is my Card class
import java.util.Random;
public class Card {
private int suit,face;
private String cardS,cardF;
// Default constructor to generate random number for face and suit
public Card()
{
Random rand = new Random();
suit = rand.nextInt(4)+1;
face = rand.nextInt(13)+1;
}
// Receive card and suit values
public Card(int cardSuit, int cardFace) {
suit = cardSuit;
face = cardFace;
// Define card face and suit values
if(suit == 1){
cardS = "Clubs";
}
else if(suit == 2){
cardS = "Hearts";
}
else if(suit == 3){
cardS = "Spades";
}
else if(suit == 4){
cardS = "Diamonds";
}
if(face == 1){
cardF = "Ace";
}
else if(face == 2){
cardF = "2";
}
else if(face == 3){
cardF = "3";
}
else if(face == 4){
cardF = "4";
}
else if(face == 5){
cardF = "5";
}
else if(face == 6){
cardF = "6";
}
else if(face == 7){
cardF = "7";
}
else if(face == 8){
cardF = "8";
}
else if(face == 9){
cardF = "9";
}
else if(face == 10){
cardF = "10";
}
else if(face == 11){
cardF = "Jack";
}
else if(face == 12){
cardF = "Queen";
}
else if(face == 13){
cardF = "King";
}
//return cardF + " of " + cardS;
}
// Get numerical face value
public int getNumericFace(){
return face;
}
// Get numerical suit value
public int getNumericSuit(){
return suit;
}
}
This is my DeckOfCards class
public class DeckOfCards {
public static final int MAXDECK = 52;
int remainingDeck,suit,face;
String cardS,cardF;
private int myCard;
int cardIndex;
Card[] cardDeck;
public DeckOfCards() {
cardDeck = new Card[MAXDECK];
// Create array of 52 cards
// outer loop cardSuit
// inner loop cardFace
int index = 0;
int maxSuit = 4;
int maxFace = 13;
for (int cardSuit = 1 ; cardSuit <= maxSuit ; cardSuit++)
{
for (int cardFace = 1 ; cardFace <= maxFace ; cardFace++)
{
cardDeck[index] = new Card(cardSuit,cardFace);
index++;
}
}
}
public String getArray()
{
System.out.println(cardDeck[0]);
for (int i = 0 ; i < cardDeck.length ; i++)
{
System.out.println(cardDeck[i]);
}
return "";
}
public String toString()
{
String deckAsString = "";
int i=0;
for (int s = 1; s <= 4 ; s++)
{
for (int c = 1 ; c <= 13 ; c++ )
{
deckAsString += (cardDeck[i] + "");
i++;
}
deckAsString += "\n";
}
return (deckAsString);
}
}
This is my main class... running both methods here have the issue.
public class MainDeck {
public static void main(String[] args) {
DeckOfCards game = new DeckOfCards();
System.out.println("test getArray method");
System.out.println(game.getArray());
System.out.println("test toString method");
System.out.println(game.toString());
}
}
Thanks for your help!
As your toString method in DeckOfCards is using cardDeck array objects and adding them to the returned String:
deckAsString += (cardDeck[i] + "");
So you need to override toString method in Card class also and use it in string manipulations.
Please implement toString() method in your Card class also.

Poker Game Java Logic Error

if (first == 11)
{card1s = "Jack";
}else {
if (second == 11)
{card2s = "Jack";
} else {
if (third == 11)
{card3s = "Jack";
}
else {
if (fourth == 11)
{card4s = "Jack";
}
else {
if (fifth == 11)
{card5s = "Jack";
}
else {
if (first == 12)
{card1s = "Queen";
}
else {
if (second == 12)
{card2s = "Queen";
}
else {
if (third == 12)
{card3s = "Queen";
}
else {
if (fourth == 12)
{card4s = "Queen";
}
else {
if (fifth == 12)
{card5s = "Queen";
}
else {
if (first == 13)
{card1s = "King";
}
else {
if (second == 13)
{card2s = "King";
}
else {
if (third == 13)
{card3s = "King";
}
else {
if (fourth == 13)
{card4s = "King";
}
else {
if (fifth == 13)
{card1s = "King";
}
else {
if (first == 1)
{card1s = "Ace";
}
else {
if (second == 1)
{card2s = "Ace";
}
else {
if (third == 1)
{card3s = "Ace";
}
else {
if (fourth == 1)
{card4s = "Ace";
}
else {
if (fifth == 1)
{card5s = "Ace";
}
else {
card1s = null;
card2s = null;
card3s = null;
card4s = null;
card5s = null;
}}}}}}}}}}}}}}}}}}}}
if ((first >= 11) | (first == 1))
System.out.println("The first card is: " + card1s + first);
This is part of my poker game program that is used to translate card in words (ex. 11 is a Jack and 12 is a Queen), the result should be printing out the translated word along with the number that is used to determine the word. The program compiles without issue, but the problem is, when I run this program, it would come up with "The first card is: Jack13" or "Queen13" things like that.
You have a lot of duplicate code, and a switch is more apporpriate here. Also you should use methods. Example:
public static void main(String[] args) {
// ...
String card1s = getCardAsString(first);
// ...
if ((first >= 11) || (first == 1))
System.out.println("The first card is: " + card1s + first);
}
private static String getCardAsString(int i) {
switch (i) {
case 1:
return "Ace";
case 11:
return "Jack";
case 12:
return "Queen";
case 13:
return "King";
default:
return String.valueOf(i); // or return null; not sure what you need
}
}
With ifs:
private static String getCardAsString2(int i) {
if (i == 1)
return "Ace";
if (i == 11)
return "Jack";
if (i == 12)
return "Queen";
if (i == 13)
return "King";
return String.valueOf(i);// or return null;
}

Why isn't this public String function working?

It is saying that it must return a string but I don't see anything wrong with it? I think numericDayOfWeek should be working fine?
public String getDayOfWeek(){
if(numericDayOfWeek==0){
return "Saturday";
}
if(numericDayOfWeek==1){
return "Sunday";
}
if(numericDayOfWeek==2){
return "Monday";
}
if(numericDayOfWeek==3){
return "Tuesday";
}
if(numericDayOfWeek==4){
return "Wednesday";
}
if(numericDayOfWeek==5){
return "Thursday";
}
if(numericDayOfWeek==6){
return "Friday";
}
}
Here is the full code
public class DayOfWeek {
int myMonth, myDayOfMonth, myYear, myAdjustment, numericDayOfWeek;
public DayOfWeek(int month, int dayOfMonth, int year){
myMonth = month;
myDayOfMonth = dayOfMonth;
myYear = year;
}
public int getNumericDayOfWeek(){
if(myMonth==1){
myAdjustment = 1;
if(myYear%4==0){
myAdjustment-=1;
}
}
if(myMonth==2){
myAdjustment = 4;
if(myYear%4==0){
myAdjustment-=1;
}
}
if(myMonth==3){
myAdjustment = 4;
}
if(myMonth==4){
myAdjustment = 0;
}
if(myMonth==5){
myAdjustment = 2;
}
if(myMonth==6){
myAdjustment = 5;
}
if(myMonth==7){
myAdjustment = 0;
}
if(myMonth==8){
myAdjustment = 3;
}
if(myMonth==9){
myAdjustment = 6;
}
if(myMonth==10){
myAdjustment = 1;
}
if(myMonth==11){
myAdjustment = 4;
}
if(myMonth==12){
myAdjustment = 6;
}
int fourDivides = myYear / 4;
numericDayOfWeek = myAdjustment + myDayOfMonth + (myYear-1900) + fourDivides;
return numericDayOfWeek;
}
public String getDayOfWeek(){
if(numericDayOfWeek==0){
return "Saturday";
}
if(numericDayOfWeek==1){
return "Sunday";
}
if(numericDayOfWeek==2){
return "Monday";
}
if(numericDayOfWeek==3){
return "Tuesday";
}
if(numericDayOfWeek==4){
return "Wednesday";
}
if(numericDayOfWeek==5){
return "Thursday";
}
if(numericDayOfWeek==6){
return "Friday";
}
}
public int getMonth(){
}
public String getMonthString(){
}
public int getDayOfMonth(){
}
public int getYear(){
}
}
Sotirios is correct, but a better solution here would be to use a case statement:
switch(numericDayOfWeek)
{
case 0:
return "Saturday";
case 1:
return "Sunday";
case 2:
return "Monday";
case 3:
return "Tuesday";
case 4:
return "Wednesday";
case 5:
return "Thursday";
case 6:
return "Friday";
default:
return "Error";
}
If none of the conditions passes, ie. they all evaluate to false, the method wouldn't return anything. Add a default return at the end
public String getDayOfWeek(){
if(numericDayOfWeek==0){
return "Saturday";
}
if(numericDayOfWeek==1){
return "Sunday";
}
if(numericDayOfWeek==2){
return "Monday";
}
if(numericDayOfWeek==3){
return "Tuesday";
}
if(numericDayOfWeek==4){
return "Wednesday";
}
if(numericDayOfWeek==5){
return "Thursday";
}
if(numericDayOfWeek==6){
return "Friday";
}
return "Error";
}
The compiler considers all paths. If none if the if statements was executed it wouldn't have anything to return. In that case, it won't be able to compile because the method wouldn't guarantee the contract specified by its definition, ie. to return a String.
Follow the comments or the other answer on how to possibly makes this perform better or make it easier to read (switch-case).
This should work:
public String getDayOfWeek(){
if(numericDayOfWeek==0){
return "Saturday";
}
else if(numericDayOfWeek==1){
return "Sunday";
}
else if(numericDayOfWeek==2){
return "Monday";
}
else if(numericDayOfWeek==3){
return "Tuesday";
}
else if(numericDayOfWeek==4){
return "Wednesday";
}
else if(numericDayOfWeek==5){
return "Thursday";
}
else if(numericDayOfWeek==6){
return "Friday";
}
else{
return "Error";
}
}
The reason for the compiler error is that the compiler cannot be certain that your code will always return a String from your method.
In the event that numericDayOfWeek is not in the range of 0 to 6, your function does not specify what value should be returned, and there is no way for the compiler to know or guarantee that numericDayOfWeek will always be within the desired range.
Unfortunately, the compiler is limited in its ability to ensure a return statement even in simple cases. Take the trivial (and useless) method below:
// I have a compiler error!
public boolean testReturn()
{
final boolean condition = true;
if (condition) return true;
if (!condition) return false;
}
The above will result in a compiler error saying the method must return a type of boolean. We could easily fix it by changing the second if statement to be an else clause since that is one of the few ways that allow the compiler to ensure one or the other blocks of code are guaranteed to be executed.
// I compile!
public boolean testReturn()
{
final boolean condition = true;
if (condition) return true
else return false;
}
The rules are that a method with a return type must not complete normally and must instead complete abruptly (abruptly here indicating via a return statement or an exception) per JLS 8.4.7. The compiler looks to see whether normal termination is possible based on the rules defined in JLS 14.21 Unreachable Statements as it also defines the rules for normal completion.
In the case of your specific example, I would suggest considering throwing an IllegalArgumentException as the last line of your method and replacing your if statement with a switch statement. E.g.
public String getDayOfWeek()
{
switch(numericDayOfWeek)
{
case 0: return "Saturday";
case 1: return "Sunday";
case 2: return "Monday";
case 3: return "Tuesday";
case 4: return "Wednesday";
case 5: return "Thursday";
case 6: return "Friday";
}
throw new IllegalArgumentException("numericDayOfWeek is out of range: " + numericDayOfWeek);
}
You can also throw the exception in a default clause of the switch statement, but in this case I would say that is just a matter of personal preference and I prefer outside of the switch here.

How to test my Deck class in Java

I would like to print my ArrayList FullDeckArray to see if my Deck has all 52 cards and values.
This is my Card and Deck Classes below
package blackjack;
/**
*
* #author mvisser
*/
public class Card
{
private int rank;
private int suit;
public String tostring(Card card1)
{
String result = "";
if (rank == 1) {
result = "Ace";
}
if (rank == 2) {
result = "Two";
}
if (rank == 3) {
result = "Three";
}
if (rank == 4) {
result = "Four";
}
if (rank == 5) {
result = "Five";
}
if (rank == 6) {
result = "Six";
}
if (rank == 7) {
result = "Seven";
}
if (rank == 8) {
result = "Eight";
}
if (rank == 9) {
result = "Nine";
}
if (rank == 10) {
result = "Ten";
}
if (rank == 11) {
result = "Jack";
}
if (rank == 12) {
result = "Queen";
}
if (rank == 13) {
result = "King";
}
if (suit == 1) {
result = result + " of Clubs ";
}
if (suit == 2) {
result = result + " of Diamonds ";
}
if (suit == 3) {
result = result + " of Hearts ";
}
if (suit == 4) {
result = result + " of Spades ";
}
return result;
}
public Card(int rank, int suit)
{
this.rank = rank;
this.suit = suit;
}
}
As you can see in my Deck Class I hava a ArrayList FullDeckArray and all I want to do is
print it out so see what value is bringing back
public class Deck
{
// private Card[][] fullDeck = new Card[0][0];
private Random shuffle = new Random();
public ArrayList<Card> FullDeckArray = new ArrayList<Card>();
// private int numberOfCards = 52;
public Deck()
{
for (int rank = 1; rank <= 13; rank++) {
for (int suit = 1; suit <= 4; suit++)
{
FullDeckArray.add(new Card(rank, suit));
}
}
}
public void shuffle() {
Collections.shuffle(FullDeckArray);
}
public Card DrawCard() {
int cardPosition = shuffle.nextInt(FullDeckArray.size()+1);
return FullDeckArray.remove(cardPosition);
}
public int TotalCards() {
return FullDeckArray.size();
}
public void test() {
System.out.println( ArrayList<Card>( FullDeckArray ) );
}
}
I would use enums, and with the enum.values() method, you can easily loop through all values of the enumeration.
public class Card {
public enum Rank {
ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING;
public String toString() {
switch(this) {
case ACE: return "Ace";
case TWO: return "Two";
case THREE: return "Three";
case FOUR: return "Four";
case FIVE: return "Five";
case SIX: return "Six";
case SEVEN: return "Seven";
case EIGHT: return "Eight";
case NINE: return "Nine";
case TEN: return "Ten";
case JACK: return "Jack";
case QUEEN: return "Queen";
case KING: return "King";
default: return "ERROR: no valid rank";
}
}
}
public enum Suit {
CLUBS, DIAMONDS, HEARTS, SPADES;
public String toString() {
switch(this) {
case CLUBS: return "Clubs";
case DIAMONDS: return "Diamonds";
case HEARTS: return "Hearts";
case SPADES: return "Spades";
default: return "ERROR: no valid suit";
}
}
}
private Rank rank;
private Suit suit;
public Card(Rank rank, Suit suit) {
this.rank = rank;
this.suit = suit;
}
public String toString() {
return rank.toString() + " of " + suit.toString();
}
public boolean equals(Object other) {
if (!(other instanceof Card)) return false;
Card card = (Card) other;
if (card.rank == this.rank && card.suit == this.suit) return true;
return false;
}
}
while in your deck class, you add all cards with this simple loop:
public void fill() {
for (Rank rank : Card.Rank.values()) {
for (Suit suit : Card.Suit.values()) {
Card card = new Card(rank, suit)
cards.add(card);
System.out.println(card.toString());
}
}
}
The rest of your deck class, you can maintain.
If you want to check for the cards being only added once, you can use a HashSet, as in a set, Objects can only occur once (but you need the equals() method):
HashSet<Card> set = new HashSet<Card>(cards);
cards = new ArrayList<Card>(set);
After that, you can check size with 'cards.size()'.
Update: Here's some code for evading the usage of Arrays in Card and for evading enums:
public class Deck {
private Random shuffle = new Random();
public ArrayList<Card> fullDeck = new ArrayList<Card>();
public Deck() {
for (int rank = 1; rank <= 13; rank++) {
for (int suit = 1; suit <= 4; suit++) {
fullDeck.add(new Card(rank, suit));
}
}
}
public void print() {
String deckOutput = "";
for (Card card : fullDeck) {
deckOutput += card.toString() + "\n";
}
System.out.println(deckOutput);
}
public static void main(String[] args) {
Deck deck = new Deck();
deck.print();
}
}
And for Card, use this:
public class Card {
private int rank;
private int suit;
public Card(int rank, int suit) {
this.rank = rank;
this.suit = suit;
}
public String toString() {
String Srank = "", Ssuit = "";
switch(rank) {
case 1: Srank = "Ace"; break;
case 2: Srank = "Two"; break;
case 3: Srank = "Three"; break;
case 4: Srank = "Four"; break;
case 5: Srank = "Five"; break;
case 6: Srank = "Six"; break;
case 7: Srank = "Seven"; break;
case 8: Srank = "Eight"; break;
case 9: Srank = "Nine"; break;
case 10: Srank = "Ten"; break;
case 11: Srank = "Jack"; break;
case 12: Srank = "Queen"; break;
case 13: Srank = "King"; break;
}
switch(suit) {
case 1: Ssuit = "Clubs"; break;
case 2: Ssuit = "Diamonds"; break;
case 3: Ssuit = "Hearts"; break;
case 4: Ssuit = "Spades"; break;
}
return Srank + " of " + Ssuit;
}
}
To test, you can still use the HashMap/ArrayList method stated above (only if you implement an equals method in Card as well) and check with the fulldeck.size() if there are 52 cards (which will be all different, because of the HashMap).
You should try this
public class DeckTest{
public static void main(String []args){
System.out.println(new Deck().FullDeckArray);
}
}
Your Card class should be like below code
public class Card
{
private int rank;
private int suit;
public String tostring()
{
String result = "";
if (rank == 1) {
result = "Ace";
}
if (rank == 2) {
result = "Two";
}
if (rank == 3) {
result = "Three";
}
if (rank == 4) {
result = "Four";
}
if (rank == 5) {
result = "Five";
}
if (rank == 6) {
result = "Six";
}
if (rank == 7) {
result = "Seven";
}
if (rank == 8) {
result = "Eight";
}
if (rank == 9) {
result = "Nine";
}
if (rank == 10) {
result = "Ten";
}
if (rank == 11) {
result = "Jack";
}
if (rank == 12) {
result = "Queen";
}
if (rank == 13) {
result = "King";
}
if (suit == 1) {
result = result + " of Clubs ";
}
if (suit == 2) {
result = result + " of Diamonds ";
}
if (suit == 3) {
result = result + " of Hearts ";
}
if (suit == 4) {
result = result + " of Spades ";
}
return result;
}
public Card(int rank, int suit)
{
this.rank = rank;
this.suit = suit;
}
}
you should override your tostring method like:
#override
public String toString(Card card1){
.....
}
and should just pass the arraylist name to System.out.println(). there is no need for type of the array list.
hope this helps...
There are two ways to do this: static and dynamic testing. Static is simpler and less prone to error, but it cannot be an action performed for any other purpose other than to simply verify that this part of your program works properly before moving on with the rest. Dynamic testing is slightly more complicated, but you can test a deck whenever you need to (for when the user modifies the deck in some fashion and you must validate it).
Static testing
The static testing method and the simplest way to do this would be to print out all the cards in a deck and verify it manually. To do that, you would first have to make an addition and a correction. First, you should modify the signature of your tostring method in your Card class to be "toString()" without parameters. This overrides the object method toString() which automatically converts an object to a String. Second, you need to override toString() in your Deck class:
#Override
public void toString() {
StringBuilder sb = new StringBuilder();
for(Card card : FullDeckArray) {
sb.append(card); // calls Card class's toString() method automatically.
sb.append('\n'); // newline character after each card
}
return sb.toString();
}
Now all you have to do is use Deck's toString() method.
public void test() {
System.out.println(this); // calls Deck class's toString() method automatically
}
Dyanmic testing
Testing it dynamically is a little more complicated, but not that much. To test this, how would you go about doing it manually? You'd look for missing cards or duplicates. A good way to test such things are Sets. But before you can use Sets, you must first override hashCode() and equals() methods to redefine how a Card class is considered unique. A card is only equal to another if both rank and suit match.
So add these to your Card class:
#Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + rank;
result = prime * result + suit;
return result;
}
#Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Card other = (Card) obj;
if (rank != other.rank)
return false;
if (suit != other.suit)
return false;
return true;
}
Now that we have that, we proceed with the actual logic. A simple test would go something like this:
For each card in your deck
If card is not in set
Add card to set
Else
Flag not valid! Duplicate card!
If set does not have exactly 52 cards
Flag not valid! Extra or missing cards!
So it naturally flows that the the code would then be:
public boolean test() {
boolean valid = true;
Set<Card> cardSet = new HashSet<Card>();
for(Card card : FullDeckArray) {
if(!cardSet.contains(card)) {
cardSet.add(card);
} else {
valid = false;
}
}
if(cardSet.size() != 52) {
valid = false;
}
return valid;
}
Also, you should look here for using enums. In addition to making code more readable, it also allows you to add methods like a class, and like a class, add toString() method. Also see jUnit for a decent testing library for Java.

Categories

Resources