Question

In: Computer Science

Objectives • Use the static methods and static fields. •Use the method-call/return mechanism. • Use the...

Objectives

• Use the static methods and static fields.

•Use the method-call/return mechanism.

• Use the random-number generation.

• Overload methods. Problem Specification

Write a program that lets a user play "Rock, Paper, Scissors" against the computer or computer against computer. In user-computer game, the program should ask the user to choose one of the three choices, and then the computer randomly picks one (without knowing what the user has chosen). For this problem, the user should be asked to enter an integer: 1 for rock, 2 for paper, 3 for scissors. Rock beats scissors, scissors beats paper, paper beats rock. The program should say who wins, and then keep playing until someone (the user or the computer) has won 5 rounds. The computer needs to keep track of the current score and should also display it before each round. In computer-computer game, play for 100 rounds and display which object won majority of the times.

Design Specification

• Create Java class RPS.

• Create static variables for storing Score1 and Score2.

• Create a static method getRandom to get random integer between 1 and 3.

• Overload play method like in the below screenshot.

• Play() is for computer vs computer, play(userchoice) is for user vs computer.

in Java, please

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
=================================

// RPS.java

import java.util.Scanner;

public class RPS {
static int score1=0;
static int score2=0;
static int getRandom()
{
   return (int)(Math.random()*(3));
}
void play()
{

     
   int comp1,comp2,res;
       for(int i=1;i<=100;i++)
       {
           comp1=getRandom();
           comp2=getRandom();
             
           res=whoWon(comp1, comp2);
           if(res==1)
           {
               score1++;
           }
           else if(res==2)
           {
               score2++;
           }
       }
       System.out.println("Computer1 Score "+score1+" Computer2 score "+score2);
       if(score1>score2)
       {
           System.out.println("Computer1 won");
       }
       else if(score1<score2)
       {
           System.out.println("Computer2 won");
       }
       else
       {
           System.out.println("It's a Tie.");
       }
  
  
}
void play(int userChoice)
{
   int compChoice=getRandom();          
  
   int res=whoWon(userChoice,compChoice);
   if(res==1)
   {
       score1++;
   }
   else if(res==2)
   {
       score2++;
   }
   else
   {
       System.out.println("ITS A TIE!");
   }

}
int whoWon(int choice1,int choice2)
{
   int res=0;
   if(choice1==1 && choice2==1)
   {
       res=0;
   }
   else if(choice1==1 && choice2==2)
   {
       res=2;
   }
   else if(choice1==1 && choice2==3)
   {
       res=1;
   }
   else if(choice1==2 && choice2==1)
   {
       res=1;
   }
   else if(choice1==2 && choice2==2)
   {
       res=0;
   }
   else if(choice1==2 && choice2==3)
   {
       res=2;
   }
   else if(choice1==3 && choice2==1)
   {
       res=2;
   }
   else if(choice1==3 && choice2==2)
   {
       res=1;
   }
   else if(choice1==3 && choice2==3)
   {
       res=0;
   }
  
  
   return res;
}
   public static void main(String[] args) {
      int choice;
   /*
        * Creating an Scanner class object which is used to get the inputs
        * entered by the user
        */
        Scanner sc = new Scanner(System.in);
   System.out.println("Select mode : Enter 1 for Computer vs Computer , Enter 2 User vs Computer ");
   choice=sc.nextInt();
   RPS r=new RPS();
   if(choice==1)
   {
       r.play();
   }
   else
   {

   for(int i=1;i<=5;i++)
   {
       System.out.println("Select 1: Rock 2:Paper 3:Scissors");
       choice=sc.nextInt();
       r.play(choice);
       System.out.println("You score :"+score1+" Computer Score :"+score2);
   }   
   System.out.println("Total no of games 5");
   if(score1>score2)
   {
       System.out.println("You won");
   }
   else if(score1<score2)
   {
       System.out.println("You lost");
   }
   else
   {
       System.out.println("ITS TIE");
   }
     
            
   }
     

   }

}

==================================

Output#1:

Select mode : Enter 1 for Computer vs Computer , Enter 2 User vs Computer
1
Computer1 Score 15 Computer2 score 11
Computer1 won

==================================

Output#2:

Select mode : Enter 1 for Computer vs Computer , Enter 2 User vs Computer
2
Select 1: Rock 2:Paper 3:Scissors
1
You score :0 Computer Score :1
Select 1: Rock 2:Paper 3:Scissors
2
You score :1 Computer Score :1
Select 1: Rock 2:Paper 3:Scissors
2
ITS A TIE!
You score :1 Computer Score :1
Select 1: Rock 2:Paper 3:Scissors
3
You score :1 Computer Score :2
Select 1: Rock 2:Paper 3:Scissors
1
You score :1 Computer Score :3
Total no of games 5
You lost

=====================Could you plz rate me well.Thank You


Related Solutions

JAVA Add static methods largest and smallest to the Measurable interface. The methods should return the...
JAVA Add static methods largest and smallest to the Measurable interface. The methods should return the object with the largest or smallest measure from an array of Measurable objects.
Create a static method with the appropriate inputs and outputs. Call each of them in the...
Create a static method with the appropriate inputs and outputs. Call each of them in the main method. Write a method called stringToListOfWords() which takes in a String converts it into a list of words. We assumes that each word in the input string is separated by whitespace.3 2Use the equals() method. 3The split() method of String can split an input up along a provided special string called a regular expression or regex. A regex is much like a code...
Create a static method with the appropriate inputs and outputs. Call each of them in the...
Create a static method with the appropriate inputs and outputs. Call each of them in the main method. Write a method called removeAllInstances() which takes in a List and item4 . The method then proceeds to remove each item in the list that matches the given item. For example, if the method is passed the List [1, 4, 5, 6, 5, 5, 2] and the Integer 5, the method removes all 5’s from the List. The List then becomes [1,...
Create a static method with the appropriate inputs and outputs. Call each of them in the...
Create a static method with the appropriate inputs and outputs. Call each of them in the main method. Write a method called isPermutaion() which takes in two List objects which contain the same types. It returns true if the Lists are permutations of each other and false if not. Two lists are permutations if they contain the same elements, including the same number of duplicates, but they don’t have to contain the elements in the same order. For example, [1,2,...
Create a static method with the appropriate inputs and outputs. Call each of them in the...
Create a static method with the appropriate inputs and outputs. Call each of them in the main method. Write a method named allMultiples() which takes in a List of integers and an int. The method returns a new List of integers which contains all of the numbers from the input list which are multiples of the given int. For example, if the List is [1, 25, 2, 5, 30, 19, 57, 2, 25] and 5 was provided, the new list...
Design an application with a single class and two static methods: method main and method isLeap....
Design an application with a single class and two static methods: method main and method isLeap. Static method isLeap accepts year as integer and returns boolean value true or false depending whether year is leap or not. public static boolean isLeap ( int year ) The year is leap year if it is divisible by 4, but not divisible by 100 except if it is divisible by 400. Examples 2007 is not a leap year 2008 is leap year 1700...
Design a Geometry class with the following methods: * A static method that accepts the radius...
Design a Geometry class with the following methods: * A static method that accepts the radius of a circle and returns the area of the circle. Use the following formula: Area=?r^2 * A static method that accepts the length and width of a rectangle and returns the area of the rectangle. Use the folliwng formula: Area = Length x Width * A static method that accepts the length of a triangle's base and the triangle's hight. The method should return...
Write a static method remove(int v, int[] in) that will return a new array of the...
Write a static method remove(int v, int[] in) that will return a new array of the integers in the given array, but with the value v removed. For example, if v is 3 and in contains 0, 1, 3, 2, 3, 0, 3, and 1, the method will return an array containing 0, 1, 2, 0, and 1. Hint: You can follow two steps to solve this problem: Create an array in the method, let say you called it result....
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int...
Given the following method declaration, write a valid method call. public static void calcArea(String roomName, int length, int width)
1) Question with methods use scanner: 1) Create one method that will add four numbers (return...
1) Question with methods use scanner: 1) Create one method that will add four numbers (return method or regular public static void ) 2) Create another method that will subtract four numbers (return method or regular public static void ) 3) Create another method that will multiplay four numbers (return method or regular public static void ) 4) Create another method that will divide four numbers (return method or regular public static void ) 5) Create another method that will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT