Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Draw a flowchart for the following code 2. - Use parts of the codes to guide your explanation in describing all the user options

1. Draw a flowchart for the following code

2.- Use parts of the codes to guide your explanation in describing all the user options in the program. Include all the requirements (a small part of the codes and explain) and how you code your program. check the code and explain whichever the following requirements presented in the code:

Add data

View all data

Search a particular data

Delete a particular data

Check valid and invalid input

Usage of selection and repetition (where appropriate) structures

Usage of methods

Usage of array

Usage of menu

Some forms of calculation

// RockPaperScissors.java

import java.util.Random; import java.util.Scanner;

public class RockPaperScissors {

/* * Creating an Scanner class object which is used to get the inputs * entered by the user */ static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int userWon=0,comWon=0,tied=0; int userChoice,compChoice; char ch; while(true) { System.out.println("Lets play ..."); compChoice=getComputerGuess(); userChoice=getUserChoice(); if(userChoice==1) { System.out.print("I picked ROCK, "); } else if(userChoice==2) { System.out.print("I picked PAPER, "); } else if(userChoice==3) { System.out.print("I picked SCISSORS, "); } int res=checkWinner(userChoice,compChoice); if(res==0) { tied++; } else if(res==1) { userWon++; } else if(res==2) { comWon++; } System.out.println("Play again (Y/N) ?"); ch = sc.next(".").charAt(0); if(ch=='Y'||ch=='y') continue; else { System.out.println(" Here are the Stats:"); System.out.println("\tYou won "+comWon+" times"); System.out.println("\tYou lost "+userWon+" times"); System.out.println("\tWe tied "+tied+" times"); System.out.println("That was fun!"); break; } }

} // This function will return the UserChoice private static int getUserChoice() {

//Creating a random Class object Random r = new Random();

return r.nextInt(3) + 1; }

// This function will return the computerChoice private static int getComputerGuess() { int cChoice; while (true) { System.out.print("Your turn (ROCK=1, PAPER=2, SCISSORS=3 ) :"); cChoice=sc.nextInt(); if (cChoice < 1 || cChoice > 3) { System.out.println("** Invalid Choice.Must be between 1-3 **"); continue; } else break; } return cChoice; } // This function compares the userchoice and computerChoice private static int checkWinner(int userChoice, int computerChoice) { int res=0; // based on user choice and computer choice display result if (userChoice == 1 && computerChoice == 1) { System.out.println("we tied"); res=0; } else if (userChoice == 1 && computerChoice == 2) { System.out.println("you win!"); res=2; } else if (userChoice == 1 && computerChoice == 3) { System.out.println("I win!"); res=1; } else if (userChoice == 2 && computerChoice == 1) { System.out.println("I win!"); res=1; } else if (userChoice == 2 && computerChoice == 2) { System.out.println("we tied"); res=0; } else if (userChoice == 2 && computerChoice == 3) { System.out.println("you win!"); res=2; } else if (userChoice == 3 && computerChoice == 1) { System.out.println("you win!"); res=2; } else if (userChoice == 3 && computerChoice == 2) { System.out.println("I win!"); res=1; } else if (userChoice == 3 && computerChoice == 3) { System.out.println("we tied"); res=0;

} return res; }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Sham Navathe

4th Edition

0321122267, 978-0321122261

More Books

Students also viewed these Databases questions

Question

10. Are you a. a leader? b. a follower? _______

Answered: 1 week ago