make a pseudocode for the new code program and answer PART B original code import java.util.Scanner; public
Question:
make a pseudocode for the new code program and answer PART B
original code
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); final int ROUND = 10; int user, genInput(), score = 0; for (int r=1; r < ROUND +1; ++r) { comp = WarGame.genInput(); user = getInput(scan); printRoundInfo(r, user, comp); score = printRoundResult(score, user, comp); } System.out.println("End of Game!"); scan.close(); }
public static int getInput(Scanner input){ // TODO: prompt user to enter a number between 1 and 52, // then validate and return it }
public static int genInput(){ //TODO : generate and return a random integer number between 1 and 52 }
public static void printRoundInfo(int r, int user, int comp){ System.out.print("R" + r + ": "); System.out.print("Computer card is " + getRank(comp) + " of " + getSuit(comp)); System.out.println("; User card is " + getRank(user) + " of " + getSuit(user)); }
public static int printRoundResult(int score, int user, int comp){ int winner = findWinner(user, comp); switch (winner){ case 0 : score *=2; System.out.println("It's tie - user score is " + score); break; case 1 : score +=1; System.out.println("User wins - user score is " + score); break; default : System.out.println("Computer wins - user score is " + score); break; } return score; }
public static int findWinner(int user, int comp){ //TODO: find the winner and return -1, 1 or 0 if it is tie, //user wins or computer wins }
// add more methods if needed. }
NEW CODE
import java.util.Scanner; import java.lang.Math; public class Main{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); final int ROUND = 10; int user, comp, score = 0; for (int r=1; r < ROUND +1; ++r) { comp = genInput(); user = getInput(scan); printRoundInfo(r, user, comp); score = printRoundResult(score, user, comp); } System.out.println("End of Game!"); scan.close(); } /** * Prompt the user for input and validate it and return it * @param input - The scanner to take input from * @return The user input */ public static int getInput(Scanner input){ System.out.print("Enter your card: "); int userCard = input.nextInt(); while(userCard < 1 || userCard > 52){ System.out.println("Invalid card input. Please try again."); System.out.print("Enter your card: "); userCard = input.nextInt(); } return userCard; }
public static int genInput(){ return (int)(Math.random()*52) + 1;
}
public static void printRoundInfo(int r, int user, int comp){ System.out.print("R" + r + ": "); System.out.print("Computer card is " + getRank(comp) + " of " + getSuit(comp)); System.out.println("; User card is " + getRank(user) + " of " + getSuit(user));
}
public static int printRoundResult(int score, int user, int comp){ int winner = findWinner(user, comp); switch (winner){ case 0 : score *=2; System.out.println("It's tie - user score is " + score); break; case 1 : score +=1; System.out.println("User wins - user score is " + score); break; default : System.out.println("Computer wins - user score is " + score); break; } return score;
}
/** * Calculates and returns the Winner of the round * @param user - The card selected by the user * comp - The card selected by the computer * @return 0, if there is a tie, * 1, if user wins, * -1, if computer wins */ public static int findWinner(int user, int comp){
int userCardRank = user % 13; if(userCardRank == 0){ userCardRank = 13; } int compCardRank = comp % 13; if(compCardRank == 0){ compCardRank = 13; } if(compCardRank == userCardRank){ return 0; } if(userCardRank > compCardRank){ return 1; } return -1; }
/** * Calculates and returns the rank of given card * @param card - The card whose Rank is to be found * @return The Rank of the card */ public static String getRank(int card){ int rank = card % 13; if(rank == 0){ return "King"; } if(rank == 1){ return "Ace"; } if(rank > 1 && rank <=10){ return Integer.toString(rank); } if(rank == 11){ return "Jack"; } return "Queen"; } /** * Calculates and returns the suit of given card * @param card - The card whose Suit is to be found * @return The suit of the card */ public static String getSuit(int card){ int suit = card / 13 ; if(card%13 == 0){ suit --; } switch(suit){ case 0: return "Hearts"; case 1: return "Diamonds"; case 2: return "Spades"; default: return "Clubs"; } } // add more methods if needed.
}
Introduction To Programming With Java A Problem Solving Approach
ISBN: 9781260575248
3rd International Edition
Authors: John Dean