I’ve pasted a question before on java programming.But never quite got the answer I was expecting.This time,I’m approaching the other way around.Instead of asking for a code,I would like somebody to guess what this code*below*is all about…:)Ten points to whoever gets it right,it’s education.And if anyone’s learn Java before,try it.I might be helping you remember your skills again.And please,be more specific.HERE WE GO!
public class Cards {
public static void main(String[] argv) {
Card a = randomCard(), b = randomCard();
int comp = a.compare(b);
if(comp == 0)
System.out.println(a.toString() + ” = ” + b.toString());
else if(comp < 0)
System.out.println(a.toString() + " < " + b.toString());
else
System.out.println(a.toString() + " > ” + b.toString());
}
/**
* Generates a random card
* @return returns a randomly generated card object
*/
static Card randomCard(){
int suit = (int)(Math.random() * 100) % 4;
int value = (int)(Math.random() * 100) % 13;
return new Card(suit, value);
}
}
class Card {
/**
* Create a card has Suit s, value n
* @param s suit of the card with, possible values are:
0(club), 1(diamond), 2(heart), 3(spade)
* @param n value of the card 0 <= n <= 12
*/
public Card(int s, int n) {
suit = s;
num = n;
}
/**
* @return a string representation of the heart object.
*/
public String toString() {
String str = SuitString[suit] + ” ” + numStr[num];
return str;
}
public int toInt(){
return suit*100+num;
}
/**
* Compares the value of this card to the card c
* @param c the card to be compared to
* @return 0 if equal, less than 0 if less than, greater 0 otherwise.
*/
public int compare(Card c){
return this.toInt() – c.toInt();
}
int suit, num;
private static String SuitString[] = {“Club”, “Diamond”, “Heart”, “Spade”};
private static String numStr[] = {“A”, “2″, “3″,
“4″, “5″, “6″, “7″, “8″, “9″, “10″, “J”,
“K”, “Q”
};
}