A friendly place for programming greenhorns!
Big Moose Saloon
Search
|
Java FAQ
|
Recent Topics
Register / Login
Win a copy of
The Mikado Method
this week in the
Agile and other Processes
forum!
JavaRanch
»
Java Forums
»
Java
»
Beginning Java
Author
Overriding equals() method within a subclass
Jon Camilleri
Ranch Hand
Joined: Apr 25, 2008
Posts: 659
I like...
posted
Apr 23, 2011 08:00:56
0
I would like to override the equals() method within a sub-class, how do I go about it?
package poker; import java.awt.Image; import java.io.BufferedInputStream; import java.io.File; import java.util.*; import java.io.*; import javax.imageio.ImageIO; public class Deck extends Card { public Deck() { short iCount = 0; for (short suit = 1; suit <= 4; suit++){ for (short rank = 1; rank <= 13; rank++) { this.deckOfCards.add(new Card(suit, rank)); iCount++; }} this.readIcons(); } public Deck (boolean _shuffled) { this(); if (_shuffled == true) { Collections.shuffle(deckOfCards, new Random()); } } public ArrayList<Card> getDeckOfCards() {return this.deckOfCards;} public ArrayList<Image> getCardIcons() {return this.CardIcons;} public Card getTopCard() { Card _cardRemoved = deckOfCards.get(1); deckOfCards.remove(1); return _cardRemoved; } public static final short MAX_CARDS_IN_DECK = 52; @Override public String toString() { return rank.toString() + " of " + suit.toString(); } @Override public String toString() { return rank.toString() + " of " + suit.toString(); } @Override public boolean equals (Object other) { if (other == null) return false; if (!(other instanceof Card)) return false; if (this == other) return true; if (getClass() != other.getClass()) return false; Card card = (Card) other; return card.suit == this.suit && card.rank == this.rank; } @Override public boolean equals (Object other) { if (other == null) return false; if !(other instanceof Deck)) return false; //hmm...I want to add this line to the sub-class.. if (!(other instanceof Card)) return false; if (this == other) return true; if (getClass() != other.getClass()) return false; Card card = (Card) other; return card.suit == this.suit && card.rank == this.rank; } private void readIcons(){ String _filename; //read images from files Image _image = null; File _file = null; InputStream _is = null; for (int i = 1; i < MAX_CARDS_IN_DECK + 1; i++) { try { /* Security information: filenames should not be altered manually by an Administrator, and, should be available within the same directory where the source code runs. */ if (i < 10) {_filename = "0" + Integer.toString(i);} else {_filename = Integer.toString(i);} String _temp = _filename; _filename = _temp + ".GIF"; //NOTE: Relative path might change when implementing? _filename = System.getProperty("user.dir") + "\\img\\" + _filename; _file = new File(_filename); //read from an input stream _is = new BufferedInputStream (new FileInputStream(_filename)); _image = ImageIO.read(_is); if (_file.exists()) { CardIcons.add(_image); System.out.println("DEBUG: " + _filename + " loaded."); } } catch (IOException e) { System.out.println(e.getMessage()); } } } private ArrayList<Image> CardIcons = new ArrayList<Image>(); private ArrayList<Card> deckOfCards = new ArrayList<Card>(); } package poker; public class Card { public Card() { /* null method */ } public Card(Suit suit, Rank rank) { this.suit = suit; this.rank = rank; } public Card(short _suit, short _cardnumber) { switch (_suit) { case 1 : this.suit = Suit.CLUBS; break; case 2 : this.suit = Suit.DIAMONDS; break; case 3 : this.suit = Suit.HEARTS; break; case 4 : this.suit = Suit.SPADES; break; default : break; } switch (_cardnumber) { case 1 : this.rank = Rank.ACE; break; case 2 : this.rank = Rank.TWO; break; case 3 : this.rank = Rank.THREE; break; case 4 : this.rank = Rank.FOUR; break; case 5 : this.rank = Rank.FIVE; break; case 6 : this.rank = Rank.SIX; break; case 7 : this.rank = Rank.SEVEN; break; case 8 : this.rank = Rank.EIGHT; break; case 9 : this.rank = Rank.NINE; break; case 10 : this.rank = Rank.TEN; break; case 11 : this.rank = Rank.JACK; break; case 12 : this.rank = Rank.QUEEN; break; case 13 : this.rank = Rank.KING; break; default : break; } } @Override public String toString() { return rank.toString() + " of " + suit.toString(); } @Override public boolean equals (Object other) { if (other == null) return false; if (!(other instanceof Card)) return false; if (this == other) return true; if (getClass() != other.getClass()) return false; Card card = (Card) other; return card.suit == this.suit && card.rank == this.rank; } @Override public int hashCode() { int result = 17; result = result * 31 + suit.hashCode(); result = result * 31 + rank.hashCode(); return result; } protected enum Suit { CLUBS("Clubs"), DIAMONDS("Diamonds"), HEARTS("Hearts"), SPADES("Spades"); private String name; Suit(String name) { this.name = name; } public String getName() { return name; } } protected enum Rank { ACE("Ace"), TWO("Two"), THREE("Three"), FOUR("Four"), FIVE("Five"), SIX("Six"), SEVEN("Seven"), EIGHT("Eight"), NINE("Nine"), TEN("Ten"), JACK("Jack"), QUEEN("Queen"), KING("King"); private String name; Rank(String name) { this.name = name; } public String getName() { return name; } } protected Suit getSuit() { return this.suit;} protected Rank getRank() { return this.rank; } protected Suit suit; protected Rank rank; }
Jon
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32654
4
posted
Apr 23, 2011 11:05:16
0
I have already given you information
here
, so shall we continue the discussion there?
Closing this thread.
I agree. Here's the link:
http://zeroturnaround.com/jrebel
- it saves me about five hours per week
subject: Overriding equals() method within a subclass
Similar Threads
Best way to represent cards?
Compare an Object to an Enum list
Arraylist insights
cannot read a Collection of a custom class as a parameter
looping through an enum?
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter