# Thread: Finding the largest value.

1. Member
Join Date
Feb 2010
Posts
11
Rep Power
0

## 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
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 10:26 PM. Reason: color tags removed, code tags added

2. Originally Posted by Blacky777
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() {
}```
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. 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. Member
Join Date
Feb 2010
Posts
11
Rep Power
0
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. 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. Member
Join Date
Feb 2010
Posts
11
Rep Power
0
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)
{
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. Originally Posted by Blacky777
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. Member
Join Date
Feb 2010
Posts
11
Rep Power
0
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. :)

#### Posting Permissions

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