Computers and Technology

// 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!

answer
Answers: 1

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 01:30, gabriieldelacruz16
What “old fashioned” features of checking accounts is p2p replacing
Answers: 3
image
Computers and Technology, 22.06.2019 07:30, alexandramendez0616
Jasper and samantha are in a robotics competition. the guidelines state that the robots should be able to move a 10-gram weight at least 2 meters and turn in a circle. jasper and samantha have already built the robot. which step of the design process should they follow next to decide whether their robot meets the minimum criteria for the competition?
Answers: 1
image
Computers and Technology, 23.06.2019 05:20, jaylenmiller437
Which operating system is a version of linux?
Answers: 1
image
Computers and Technology, 23.06.2019 09:30, Cocco
You have been supporting csm tech publishing's windows server 2016 server network for over a year. the office has two windows server 2016 servers running active directory and a number of other roles. management has informed you that a small sales office is opening in the same building three floors up. the sales manager wants to install a sales application on a server located in the sales office. this server will have limited physical security because there's no special room dedicated for it, which means it will be accessible to non-it personnel and visitors. you're considering installing windows server 2016 server core on the new server because accessing its console regularly probably won't be necessary, and this server will be managed from one of the other csm tech publishing servers. what are the benefits and drawbacks of using server core for this branch office? what are some things you should do to set up this server management environment?
Answers: 1
Do you know the correct answer?
// rockpaperscissors. java// plays rock paper scissors with the computer import java. util. scanner;...

Questions in other subjects:

Konu
Mathematics, 21.05.2021 06:10
Konu
Mathematics, 21.05.2021 06:20