Results 1 to 8 of 8
  1. #1
    Blacky777 is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default Finding the largest value.

    Hi, I need some help in solving a particular question for an assignment. The program creates a pack of random playing cards of any size. Below 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;
    	}
    }



    However, I need to create a method inside another pre-determined class which finds the highest value card (ace being 1 and king being 13). If two cards have the same numerical value then it is decided based on suit (Diamonds lowest, then Clubs, then Hearts, then Spades). Any help would be appreciated. Thanks.

    public Card findLargest()
    Last edited by Fubarable; 02-01-2010 at 09:26 PM. Reason: color tags removed, code tags added

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Blacky777 View Post
    However, I need to create a method inside another pre-determined class which finds the highest value card (ace being 1 and king being 13). If two cards have the same numerical value then it is decided based on suit (Diamonds lowest, then Clubs, then Hearts, then Spades).
    Add the following little method to your Card class and compare the 'ranks' of the cards:

    Java Code:
    public int rank() {
       return 42*"DiamondsClubsHeartsSpades".indexOf(suit)+number;
    }
    The rank number will be a strange number but it increases monotomously so it'll do its job.

    kind regards,

    Jos

    ps. also have a look at enums; they're made for this job.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Also, your card string method can be greatly improved using an array:
    Java Code:
    public class Card {
    
      public static final String[] CARD_STRINGS = {
          // the "" is there as there is no "Zero" card and arrays are zero-based.
          "", "Ace", "Two", "Three", "Four", "Five", "Six",
          "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve",
          "Thirteen" };
    
      private int number;
      private String suit;
    
      public String getCardString() {
        return CARD_STRINGS[number] + " of " + suit;
      }
    Note though that if you use enums as Jos suggests above, even the array would be unnecessary as the enum would take care of it.

  4. #4
    Blacky777 is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default

    Thanks for the responses, however the code I initially quoted is what I have to create the method with, it cannot be altered. This is what I have so far, it does not take into account suit (not yet)

    Java Code:
          public Card findLargest() {
          Card largest = null;
          int largest_value = 0;
          for (Card c : pack){
          if (c.getNumber() > largest_value){
              largest = c.cardString();
    }
    }
          return largest;
    }
    However it gives me the found string/ expected Card error. It is also irritating because I cannot change the method type either :(

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Here:
    Java Code:
    if (c.getNumber() > largest_value) {
        largest = c.cardString();
    }
    You are setting largest, a Card object, = to c.cardString(), a String object, and the compiler is correctly telling you that this just won't fly. Why not simply set largest = to c?
    Java Code:
    if (c.getNumber() > largest_value) {
        largest = c;
    }

  6. #6
    Blacky777 is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default

    Thanks, it's now working thanks to your help, but I've came across another problem trying to find the highest suit if there are 2 cards of the same highest value, my code is as follows...

    Java Code:
          public Card findLargest() {
          Card largest = null;
          int largest_value = 0;
          boolean identical = false;
          
          for (Card c : pack){
          if (c.getNumber() > largest_value){
              largest_value = c.getNumber();
              largest = c;
    
          if (c.getNumber() > largest_value && c.getNumber() = largest_value)
              {    
              if (c.getSuit().contains("Spades")){
                  largest = c;
                  largest_value = c.getNumber();}
              else {
              if (c.getSuit().contains("Hearts")){
                  largest = c;
                  largest_value = c.getNumber();}
              else {
              if (c.getSuit().contains("Clubs")){
                  largest = c;
                  largest_value = c.getNumber();}
              else {
              if (c.getSuit().contains("Diamonds")){
                  largest = c;
                  largest_value = c.getNumber();}              
                   }
                   }
                   }
             }
                                             }
    
    
          return largest;
                               }
           }
    However I get the error "operator && cannot be applied to boolean, int".

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Blacky777 View Post
    Java Code:
          if (c.getNumber() > largest_value && c.getNumber() = largest_value)
    However I get the error "operator && cannot be applied to boolean, int".
    Use '==' instead of '='; a few parentheses here and there won't hurt either, maybe you'll see that the entire expression doesn't make sense.

    kind regards,

    Jos

  8. #8
    Blacky777 is offline Member
    Join Date
    Feb 2010
    Posts
    11
    Rep Power
    0

    Default

    Thanks, problem solved!
    I'm trying now to create a duplicate card finder, I have a fair idea on how to get started but any advice would be greatly appreciated. :)

Similar Threads

  1. Finding Largest Prime Factor
    By perito in forum New To Java
    Replies: 7
    Last Post: 11-08-2010, 08:25 PM
  2. Finding the largest number in an array
    By starchildren3317 in forum New To Java
    Replies: 14
    Last Post: 11-03-2010, 06:49 AM
  3. Finding largest and smallest integer
    By mlhazan in forum New To Java
    Replies: 2
    Last Post: 01-12-2008, 10:30 PM
  4. ArrayList problem (finding largest no)
    By bugger in forum New To Java
    Replies: 3
    Last Post: 12-12-2007, 12:47 PM
  5. Finding largest no
    By bugger in forum New To Java
    Replies: 11
    Last Post: 11-29-2007, 12:49 PM

Posting Permissions

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