Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Win a copy of
Experimentation for Engineers: From A/B testing to Bayesian optimization
this week in the
Design
forum!
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Ron McLeod
Tim Cooke
Paul Clapham
Liutauras Vilda
Sheriffs:
Junilu Lacar
Rob Spoor
Jeanne Boyarsky
Saloon Keepers:
Stephan van Hulst
Carey Brown
Tim Holloway
Piet Souris
Bartenders:
Forum:
Beginning Java
Overriding equals() method within a subclass
Jon Camilleri
Ranch Hand
Posts: 664
I like...
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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
Marshal
Posts: 77559
372
posted 11 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I have already given you information
here
, so shall we continue the discussion there?
Closing this
thread
.
When people don’t understand what you are doing they call you crazy. But this tiny ad just doesn't care:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Compare an Object to an Enum list
looping through an enum?
cannot read a Collection of a custom class as a parameter
Best way to represent cards?
Incrementer Issue: Card Shuffler
Low Tech Laboratory Movie Kickstarter - March 2023
More...