7 Replies - 1360 Views - Last Post: 24 May 2013 - 09:21 PM Rate Topic: -----

#1 applemad20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 22-November 12

Card Game Task Using Classes

Posted 03 December 2012 - 01:25 PM

Ive began to write a Write a program which will represents a standard playing card.

Every card has a value and a suit.

Every Playing Card Has a Value And A Suit

Values are either in the range 2 to 10 or one of the face cards Ace, King, Queen and Jack.

The standard suits are Clubs, Diamonds, Hearts and Spades.
The PlayingCard class must have the following public methods

Now i need to set the values of the faces:

  • Jack has the value eleven
  • Queen has the value twelve
  • King has the value thirteen
  • Ace has the value fourteen


The way i have done this is use the final method:

This is my code for the PlayingCard.java

 

/*
 Program Title: PlayingCard
 Author: applemad20
 Created: 02 December 2012
 Version: 1.0
 */



/**
 * DESCRIPTION OF CLASS HERE()
 *
 * @author GethynJones
 * @version 1.0
 */

public class PlayingCard
{

    private int value;
    private int suit;
    
    public final int JACK = 11;
    public final int QUEEN = 12;
    public final int KING = 13;
    public final int ACE = 14;
     

    /**
     * Gets The Current Card Value
     *
     * @return The Current Card
     */
    public int getValue()
    {
        return value;
    }

    /**
     * Gets The Current Value Of The Suit
     *
     * @return The Current Suit
     */
    public int getSuit()
    {
        return suit;
    }

    
    /**
     * Returns The toString Representation Of The Class Data
     * @return String For Printing
     */
    @Override
    public String toString()
    {
        return "PlayingCard [ value = " + getValue() + " suit = " + getSuit() + "]";
    }

    /**
     * Returns The string Format On How The Data WIll Be Printed On Console
     * @return String.format
     */
    public String format()
    {

        return String.format("%-30s %8.2f", "Gethyn", 2.33);
    }
    
    /**
     * Tests Whether This Card Is Equal To Some Other Card
     * @param card The Card To Be Tested
     * 
     * @return returns true If The Suit And Value Of The Test Card Is 
     * Equal To The Suit And Value Of This Card Otherwise false If returned
     */
//    public boolean equals(PlayingCard card)
//    {
//        
//    }
//    
    public PlayingCard(int value, int suit)
    {
        this.value = value;
        this.suit = suit;
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        // TODO code application logic here
    }
}



Now i need another class called PlayingCardTester.java.

I have created this but it has no content so far.



When i call the to String method it should bring up the results like this:

PlayingCard[value=King, suit=Hearts]

The part that i am on is the equals method:

/**
     Tests whether this card is equal to some other card
     @param card the card to be tested
@return returns true if the suit and value of the test card is equal to the suit and value of this card otherwise false is returned
*/
public boolean equals(PlayingCard card)
{ ... }



Im not sure on what information do i put here.

Then the Constructor its self

public PlayingCard(int value, int suit)
{
   this.value = value;
   this.suit = suit;
}


How do i change the int value to its card representative.

Thanks

##ADMIN - - Could You Delete This Thread Please I Clicked Post And Went Back By Mistake And I Have 2 Threads on Same Subject

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Card Game Task Using Classes

#2 ryanisis  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 12
  • Joined: 24-September 12

Re: Card Game Task Using Classes

Posted 24 May 2013 - 07:14 AM

First off, I'd suggest moving your main() to a separate class, like the tester class you're supposed to create. If you'll be creating more than one instance of a card, it doesn't make sense to have a main in the card's class.
Second, as far as an equals method goes you have all the necessary information for a comparison. So you should only have to compare them. The card being passed as a parameter is obviously "card" right? And the card it's going to be compared to is the current instance of a PlayingCard or "this". Follow? So if I'm understanding what you're looking for correctly, the end result of the method should be something along the lines of this:

public boolean equals(PlayingCard card)
{
if(card.value == this.value && card.suite == this.suite)
{
return true;
}
return false;
}


Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,681
  • Joined: 19-March 11

Re: Card Game Task Using Classes

Posted 24 May 2013 - 07:25 AM

View Postryanisis, on 24 May 2013 - 09:14 AM, said:

public boolean equals(PlayingCard card)
{
if(card.value == this.value && card.suite == this.suite)
{
return true;
}
return false;
}



Notice that this won't work as written because the value and suit are private, as they should be. You'll need to use methods to get the value and suit from the other card. You've already written those methods.
Was This Post Helpful? 0
  • +
  • -

#4 ryanisis  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 12
  • Joined: 24-September 12

Re: Card Game Task Using Classes

Posted 24 May 2013 - 07:32 AM

View Postjon.kiparsky, on 24 May 2013 - 09:25 AM, said:

Notice that this won't work as written because the value and suit are private, as they should be. You'll need to use methods to get the value and suit from the other card. You've already written those methods.


Yep, you're right. Thank you for pointing that out. Hopefully this is correct:

public boolean equals(PlayingCard card)
{
if(card.getValue() == this.value && card.getSuite() == this.suite)
{
return true;
}
return false;
}


Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,681
  • Joined: 19-March 11

Re: Card Game Task Using Classes

Posted 24 May 2013 - 07:38 AM

Yeah, that should do the work. You can make it a little shorter if you want:

public boolean equals(PlayingCard card)
{
   return card.getValue() == this.value && card.getSuit() == this.suit;
}


Was This Post Helpful? 1
  • +
  • -

#6 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1939
  • View blog
  • Posts: 4,027
  • Joined: 11-December 07

Re: Card Game Task Using Classes

Posted 24 May 2013 - 08:42 AM

You don't need the getters in the equals() method. Private means accessible only from that class. The catch is that separate instances are still the same class. This compiles, for example:

public class Card {

	private int value;
	private int suit;

	public Card(int value, int suit) {
		this.value = value;
		this.suit = suit;
	}

	public boolean equals(Card other) {
		return (value == other.value) && (suit == other.suit);
	}

}


Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2640
  • View blog
  • Posts: 11,155
  • Joined: 20-September 08

Re: Card Game Task Using Classes

Posted 24 May 2013 - 11:33 AM

cfoley is right. And actually the real problem is something quite different: there's no point in producing an equals method that is not usable as a callback method by Collection classes in the usual way. In its current form, it won't be, as it has the wrong prototype. It needs to be

    @Override
    public boolean equals(Object other) {
        PlayingCard otherCard = (PlayingCard) other;

        return (suit == otherCard.suit) && (value == otherCard.value);
    }



You can make your code stronger by using type-safe enum btw. The Sun/Oracle Java enum tutorials have an example of a Card class iirc

This post has been edited by g00se: 24 May 2013 - 11:35 AM
Reason for edit:: typo

Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Card Game Task Using Classes

Posted 24 May 2013 - 09:21 PM

View Postapplemad20, on 03 December 2012 - 04:25 PM, said:

Every card has a value and a suit.

Every Playing Card Has a Value And A Suit

What is the difference between a Card and a Playing Card
What is the name of the differet classes implementing this subtil difference ?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1