Results 1 to 10 of 10
Thread: Finding & Returning Duplicates
- 01-24-2011, 09:56 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
Finding & Returning Duplicates
Hi, I'm having some problems while working on a programming assignment.
I am trying to create a method to 1) find out if there are any duplicate cards in the pack and 2) print out the duplicate card if there is one.
The following is the card class:-
And this is what I have so far, which isn't working.Java Code:/* * A class to model a playing card. */ public class Card { private int number; private String suit; /* * Randomly creates a card numbered 1 to 13 (ace = 1!) and labelled "Hearts","Clubs","Diamonds" or "Spades". */ public Card() { double randomNum = Math.random() * 4.0; if (randomNum < 1.0) suit = "Hearts"; else if (randomNum < 2.0) suit = "Clubs"; else if (randomNum < 3.0) suit = "Diamonds"; else suit = "Spades"; randomNum = Math.random() * 13.0; number = (int) randomNum + 1; } /* * Creates a card with specified number and suit */ public Card (int n, String s) { number = n; suit = s; } public int getNumber() { return number; } public String getSuit () { return suit; } /* * Converts Card to String representation e.g. "Ace of Spades" */ public String cardString() { // System.out.println(number + " " + suit); String stringNum = ""; switch (number) { case 1: stringNum = "Ace"; break; case 2: stringNum = "Two"; break; case 3: stringNum = "Three"; break; case 4: stringNum = "Four"; break; case 5: stringNum = "Five"; break; case 6: stringNum = "Six"; break; case 7: stringNum = "Seven"; break; case 8: stringNum = "Eight"; break; case 9: stringNum = "Nine"; break; case 10: stringNum = "Ten"; break; case 11: stringNum = "Jack"; break; case 12: stringNum = "Queen"; break; case 13: stringNum = "King"; break; default: System.out.println("Error in Card - illegal number"); } return stringNum + " of " + suit; } }
A sidenote is that I cannot change the Card class in any way as that was provided for us. Any help would be appreciated. Thanks.Java Code:/* * Return true if there is a duplicate card in the pack */ public boolean hasDuplicate() { for (int index = 0; index < pack.size(); index++){ for(Card c: pack){ if(c.getNumber() && c.getSuit() == c[index].getNumber() && c[index].getSuit()); } } return true; } /* * Return any duplicate card, 'null otherwise */ public Card getDuplicate() { for(Card c: pack){ if(c.hasDuplicate = true);{ System.out.println(c.cardString); } } } }
- 01-24-2011, 10:11 PM #2
- 01-24-2011, 10:18 PM #3
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
Sorry about that, knew I forgot something when I previewed my post. ^^; I've highlighted the areas where Eclipse is giving me the error.
Multiple markers at this line.Java Code:/* * Return true if there is a duplicate card in the pack */ public boolean hasDuplicate() { for (int index = 0; index < pack.size(); index++){ for(Card c: pack){ if(c.getNumber() && c.getSuit() == [COLOR="Red"]c[index][/COLOR].getNumber() && [COLOR="red"]c[index][/COLOR].getSuit()); } } return true; }
The type of the expression must be an array type but it resolved to Card.
hasDuplicate cannot be resolved or is not a field.Java Code:/* * Return any duplicate card, 'null otherwise */ public Card getDuplicate() { for(Card c: pack){ if(c.[COLOR="red"]hasDuplicate[/COLOR] = true);{ System.out.println([COLOR="red"]c.cardString[/COLOR]); } } } }
cardString cannot be resolved or is not a field.
- 01-24-2011, 10:29 PM #4
hasDuplicate and cardString are methods. What are you missing?
This is an assignment and not a comparison. Even better there is no need to do a comparison with booleans.Java Code:c.hasDuplicate = true
Study that line very carefully and see if you can find what is semantically wrong (if you fix the other problems mentioned above).Java Code:if(c.hasDuplicate = true);{
- 01-24-2011, 10:30 PM #5
You are treating c as an array but what have you declared c has?Java Code:c[index].getNumber()
- 01-25-2011, 12:20 AM #6
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
So I don't need that statement at all...?
What I have now: -
The method hasDuplicate() is undefined for the type Card.Java Code:/* * Return any duplicate card, 'null otherwise */ public Card getDuplicate() { for(Card c: pack){ if([COLOR="Red"]c.hasDuplicate([/COLOR]) == true){ System.out.println(c.cardString()); } } } }
A variable? :S
- 01-25-2011, 12:49 AM #7
No what I mean is that it is pointless comparing booleans when they are booleans already.
Java Code:boolean bool = true; if(bool == true) {} // same as if(bool) {}Not sure about this. Make sure your spelling is correct and the method actually is in the Card class. Also try deleting the .class file(s) and compile again.The method hasDuplicate() is undefined for the type Card.
Yes it is a variable but what type is it. Is it an array? No so why are you treating it like an array?A variable? :S
- 01-25-2011, 01:00 AM #8
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
The hasDuplicate() is the method I am working on along with the getDuplicate() method. It's not in the Card class but in another class called Pack Cards which is the only class I am allowed to edit. Does this mean I can't call methods inside the same class?
I thought to find duplicates you would need an array to hold one of the cards in the pack and then compare it with the rest of the pack to see if there's any matching ones. Is this not the correct way to do it? :confused:
- 01-25-2011, 01:22 AM #9
So why are you trying to call that method on a Card object? It would seem you lack some basic understanding. You need to go back and read a textbook/your lecture notes/an online tutorial.It's not in the Card class but in another class called Pack Cards
Sounds reasonable but c is a single Card object and not an array of Card objects. Once again you need to revise material already covered to get a better understanding.I thought to find duplicates you would need an array to hold one of the cards in the pack and then compare it with the rest of the pack
- 01-25-2011, 11:24 AM #10
Member
- Join Date
- Jan 2011
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Getting rid of duplicates in my Array problem
By 'Rasta. in forum New To JavaReplies: 6Last Post: 11-20-2010, 05:51 PM -
Removing Duplicates.
By dashwall in forum New To JavaReplies: 9Last Post: 12-29-2009, 01:03 PM -
Duplicates in more than two sets
By JavaJ in forum New To JavaReplies: 8Last Post: 12-03-2009, 04:07 PM -
Duplicates
By Gambit17 in forum New To JavaReplies: 5Last Post: 11-08-2007, 09:56 AM -
duplicates in iReport
By Heather in forum Advanced JavaReplies: 1Last Post: 07-05-2007, 04:42 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks