Results 1 to 10 of 10
  1. #1
    Snowdrift is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default 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:-

    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;
    	}
    }
    And this is what I have so far, which isn't working.

    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);
    		}
    		}
    		
    	}
    
    }
    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.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by Snowdrift View Post
    which isn't working.
    I wanted to drive my car this morning but it didn't start. So I rang the mechanic and told him it wasn't working. Surprise surprise he couldn't help me until I gave him more details.

  3. #3
    Snowdrift is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    Sorry about that, knew I forgot something when I previewed my post. ^^; I've highlighted the areas where Eclipse is giving me the error.

    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;
    		 
    	}
    Multiple markers at this line.
    The type of the expression must be an array type but it resolved to Card.

    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]);
    		}
    		}
    		
    	}
    
    }
    hasDuplicate cannot be resolved or is not a field.
    cardString cannot be resolved or is not a field.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    hasDuplicate and cardString are methods. What are you missing?

    Java Code:
    c.hasDuplicate = true
    This is an assignment and not a comparison. Even better there is no need to do a comparison with booleans.
    Java Code:
    if(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).

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Java Code:
    c[index].getNumber()
    You are treating c as an array but what have you declared c has?

  6. #6
    Snowdrift is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post

    Java Code:
    c.hasDuplicate = true
    This is an assignment and not a comparison. Even better there is no need to do a comparison with booleans.
    So I don't need that statement at all...?

    What I have now: -

    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());
    		}
    		}
    		
    	}
    
    }
    The method hasDuplicate() is undefined for the type Card.

    Quote Originally Posted by Junky View Post
    Java Code:
    c[index].getNumber()
    You are treating c as an array but what have you declared c has?
    A variable? :S

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by Snowdrift View Post
    So I don't need that statement at all...?
    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) {}
    The method hasDuplicate() is undefined for the type Card.
    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.
    A variable? :S
    Yes it is a variable but what type is it. Is it an array? No so why are you treating it like an array?

  8. #8
    Snowdrift is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    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 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?

    Quote Originally Posted by Junky View Post
    Yes it is a variable but what type is it. Is it an array? No so why are you treating it like an array?
    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:

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    It's not in the Card class but in another class called Pack Cards
    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.
    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
    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.

  10. #10
    Snowdrift is offline Member
    Join Date
    Jan 2011
    Posts
    5
    Rep Power
    0

    Default

    I will do that. However have you got any other solutions I could try out just now? Since the assignment is due tomorrow and I haven't gotten any closer to solving it....:(

Similar Threads

  1. Getting rid of duplicates in my Array problem
    By 'Rasta. in forum New To Java
    Replies: 6
    Last Post: 11-20-2010, 05:51 PM
  2. Removing Duplicates.
    By dashwall in forum New To Java
    Replies: 9
    Last Post: 12-29-2009, 01:03 PM
  3. Duplicates in more than two sets
    By JavaJ in forum New To Java
    Replies: 8
    Last Post: 12-03-2009, 04:07 PM
  4. Duplicates
    By Gambit17 in forum New To Java
    Replies: 5
    Last Post: 11-08-2007, 09:56 AM
  5. duplicates in iReport
    By Heather in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 04:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •