subject

// rockpaperscissors. java// plays rock paper scissors with the computer import java. util. scanner; public class rockpaperscissors{ // static class constants static final int rand_limit = 3; static final int num_error = 0; static final int num_rock = 1; static final int num_paper = 2; static final int num_scissors = 3; static final string str_error = "error"; static final string str_rock = "rock"; static final string str_paper = "paper"; static final string str_scissors = "scissors"; static final int player_tie = 0; static final int player_1 = 1; static final int player_2 = 2; public static void main(string[] args) { string inputstr; // general purpose string for input boolean continuing = false; int player1choice = 0, player2choice = 0; int winner = 0; scanner input = new scanner(system. in); // main loop - ask the user if they want to play // and continue if confirmed do { system. out. print("rock, paper scissors: play? (y or n): "); inputstr = input. nextline(); if (! inputstr. equals("y")) continuing = false; else { continuing = true; system. out. println("ok, let's play! "); // player 1 by default is the computer, we may want // want to enhance in the future to allow two humans // (or two computers! ) player1choice = getcomputerchoice(); //system. out. println("player 1 chooses " + player1choice); // debug player2choice = getchoice(input); //system. out. println("player 2 chooses " + player2choice); // debug // echo choice and determine winner system. out. println("computer chose " + choicenumtostring(player1choice) + ", you chose " + choicenumtostring(player2choice)); winner = determinewinner(player1choice, player2choice); // show winner if (winner == player_1) system. out. println("computer wins! "); else if (winner == player_2) system. out. println("you win! "); else system. out. println("tie game! "); } } while(continuing == true); system. out. println("bye! "); } // get, validate, and return the human user's choice public static int getchoice(scanner input) { int choice = num_error; boolean valid = false; while (! valid) { system. out. print(" enter your choice: \n" + "\t1 for rock\n" + "\t2 for paper\n" + "\t3 for scissors" + ": "); choice = input. nextint(); input. nextline(); // clear the buffer valid = isvalid(choice); if (! valid) system. out. println("invalid choice; enter either 1, 2, or 3"); } return choice; }}my rockpaperscissors. java file contains an incomplete implementation of a program which plays the game rock, paper, scissors against the computer. winners are mapped using the following combinations: rock smashes scissorspaper smothers rockscissors cut papera tie occurs if the same object is chosen by both players. the file contains a fully functional main method, various useful constant definitions, and an implementation of the getchoice() method which obtains the game choice from the human user. for this assignment you need to complete the implementation by coding the following methods, for which i have provided the headers, so that the program runs successfully. use the commented method descriptions to guide your implementation. // convert a numeric choice to the corresponding string, // e. g. num_rock returns str_rock public static string choicenumtostring(int choicenum) // get a random value between 1 and 3 (limit) for the computer's choice public static int getcomputerchoice() // return true if ch is valid choice of num_rock, num_paper, or num_scissors public static boolean isvalid(int ch) // get, validate, and return the human user's choice public static int getchoice(scanner input) // determine the winner - returns player_1, player_2, or player_tie public static int determinewinner(int choice1, int choice2)do not modify any constant definition, function header, or the main method (but read the hint provided below). note that i have declared the program constants outside of the main method (above it, actually). doing it this way means that they are available for use outside of the main method, including in the methods you write (this are similar to "global constants").hint: you may want to start out by commenting code in the main method and bring the commented lines back in piece-by-piece as you implement the functions. remember to restore the entire main method back to it's original state when complete so as not to break the above rule about modifying the main method (what i don't see won't hurt output of the game is shown below: rock, paper scissors: play? (y or n): yok, let's play! enter your choice: 1 for rock 2 for paper 3 for scissors: 1computer chose rock, you chose rocktie game! rock, paper scissors: play? (y or n): yok, let's play! enter your choice: 1 for rock 2 for paper 3 for scissors: 1computer chose paper, you chose rockcomputer wins! rock, paper scissors: play? (y or n): yok, let's play! enter your choice: 1 for rock 2 for paper 3 for scissors: 1computer chose scissors, you chose rockyou win! rock, paper scissors: play? (y or n): nbye!

ansver
Answers: 1

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 09:00, 19elbatawisaly
What is one way in which tablets differ from laptops and notebooks? tablets are designed for touch-based interaction. tablets are designed to be used as desktops. tablets are designed for input via a keyboard and mouse. tablets are designed to be larger than laptops.
Answers: 1
image
Computers and Technology, 22.06.2019 22:50, youngboymark123
Assume the existence of a bankaccount class. define a derived class, savingsaccount that contains two instance variables: the first a double, named interestrate, and the second an integer named interesttype. the value of the interesttype variable can be 1 for simple interest and 2 for compound interest. there is also a constructor that accepts two parameters: a double that is used to initialize the interestrate variable, and a string that you may assume will contain either "simple", or "compound", and which should be used to initialize the interesttype variable appropriately. there should also be a pair of functions getinterestrate and getinteresttype that return the values of the corresponding data members (as double and int respectively).
Answers: 2
image
Computers and Technology, 23.06.2019 01:30, solikhalifeoy3j1r
In deadlock avoidance using banker’s algorithm, what would be the consequence(s) of: (i) a process declaring its maximum need as maximum possible for each resource. in other words, if a resource a has 5 instances, then each process declares its maximum need as 5. (ii) a process declaring its minimum needs as maximum needs. for example, a process may need 2-5 instances of resource a. but it declares its maximum need as 2.
Answers: 3
image
Computers and Technology, 23.06.2019 13:30, mads000
Drag the tiles to the correct boxes to complete the pairs. match the errors with their definitions. #name #value #ref when a formula produces output that is too lengthy to fit in the spreadsheet cell arrowright when you enter an invalid cell reference in a formula arrowright when you type text in cells that accept numeric data arrowright when you type in a cell reference that doesn’t exist arrowright reset next
Answers: 1
You know the right answer?
// rockpaperscissors. java// plays rock paper scissors with the computer import java. util. scanner;...

Questions in other subjects:

Konu
Spanish, 30.07.2019 18:50
Konu
Mathematics, 30.07.2019 18:50