Hey guys I had some homework in a Java class on Friday. I completed it so I am not looking for answers (I also don't think it will be graded). I was just curious, as a learning exercise, how would you guys improve this and why? Now remember I'm a java newb so go easy; I still haven't learned what an object is. I'm sure their has to be a better, prettier way of doing this though so I figured I would ask the pros. Here's the code:
/*
* This program generates and prints a
* random card from a 52 card deck
*/
package randomcard;
/**
* @author 24x24
*/
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int cardRank = randomRank(); // calls the randomRank method
int suitNumber = randomSuit(); // calls the randomSuit method
String cardSuit = suits(suitNumber); // calls the suits method
cards(cardRank, cardSuit); // calls the cards method
} // end of main method
/**
* This method takes the variable suitNumber and
* uses it to find variable cardSuit
* @param suitNumber
* @return cardSuit
*/
public static String suits(int suitNumber) {
// Declare variable cardSuit
String cardSuit = "null";
if (suitNumber == 1) {
cardSuit = "Spades";
} else if (suitNumber == 2) {
cardSuit = "Hearts";
} else if (suitNumber == 3) {
cardSuit = "Clubs";
} else if (suitNumber == 4) {
cardSuit = "Diamonds";
}
return cardSuit;
} // end of suits method
/**
* This method tells the user which card they have recieved
* by using variables cardRank and cardSuit. If they cardRank
* is 1,11,12, or 13 it gives a face value
* @param cardRank
* @param cardSuit
*/
public static void cards(int cardRank, String cardSuit) {
// if statement set finds face cards and gives them a name
if (cardRank == 1) {
System.out.println("Your card is an Ace of " + cardSuit);
} else if (cardRank == 13) {
System.out.println("Your card is a King of " + cardSuit);
} else if (cardRank == 12) {
System.out.println("Your card is a Queen of " + cardSuit);
} else if (cardRank == 11) {
System.out.println("Your card is a Jack of " + cardSuit);
} else if (cardRank == 8) { // 8 is the only number that requires 'an'
System.out.println("Your card is an " + cardRank + " of " + cardSuit);
} else {
// Print out the randomly selected card
System.out.println("Your card is a " + cardRank + " of " + cardSuit);
}
} // end of cards method
/**
* This method randomly creates the variable cardRank
* @return cardRank
*/
public static int randomRank() {
int cardRank = (int) ((int) 1 + (Math.random() * (13 - 1 + 1))); // creates a random card rank
return cardRank;
} // end of randomRank method
/**
* This method randomly creates the variable suitNumber
* @return suitNumber
*/
public static int randomSuit() {
int suitNumber = (int) ((int) 1 + (Math.random() * (4 - 1 + 1))); // creates a random card rank
return suitNumber;
} // end of randomRank method
} // end of Main class
It's long which is mostly what I think could be improved, but I'm not too sure how one would do that. I promise this is complete; I am not trying to get answers. If you don't believe me look at my other posts where I've been trying to learn and not just ask for the answer. I just want to know how to do this better since I finished in record time and didn't need any help!
Thanks for any input!