Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In Java, create a JApplet in which the user can click one of three buttons label

ID: 3661618 • Letter: I

Question

In Java, create a JApplet in which the user can click one of three buttons labeled "Rock", "Paper", or "Scissors". The computer's choice is randomly generated. A typical execution after the user has played a few games is shown below. Save the applet as JRockPaperScissors.java.

Additionally, keep a tally that shows the number of games that the player has won, tied, or lost that updates and displays after every game. Use the drawString() method to display the course id (CSC 102) in the lower left-hand corner of the applet.

//Please pay special attention to the last line about using the drawString() method to add information to the program and keeping a tally. This is different from the standard book answer. Thank you.

Explanation / Answer

Answer:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class JRockPaperScissors extends JApplet implements ActionListener

{

private JButton JBRock;

private JButton JBPaper;

private JButton JBScissor;

private int totUserWins;

private int totCompWins;

private JLabel compChoiceLabel;

private JLabel winLabel;

private JLabel userWinLabel;

private JLabel compWinLabel;

public void init()

{

JBRock=new JButton("ROCK");

JBPaper=new JButton("PAPER");

JBScissor=new JButton("SCISSOR");

compChoiceLabel=new JLabel();

winLabel=new JLabel();

userWinLabel=new JLabel();

compWinLabel=new JLabel();

add(JBRock);

add(JBPaper);

add(JBScissor);

add(compChoiceLabel);

add(winLabel);

add(userWinLabel);

add(compWinLabel);

JBRock.addActionListener(this);

JBPaper.addActionListener(this);

JBScissor.addActionListener(this);

totUserWins=0;

totCompWins=0;

}

public void actionPerformed(ActionEvent atEvent)

{

int compCh=(int)Math.random()*3;

String[] gameString={"ROCK","PAPER","SCISSOR"};

String userCStr=(String)atEvent.getSource();

if(userCStr.equals("ROCK"))

{

compChoiceLabel.setText("Computer Chosen:"+gameString[compCh]);

if(gameString[compCh].equals("ROCK"))

{

winLabel.setText("DRAW");

}

else if(gameString[compCh].equals("PAPER"))

{

winLabel.setText("Computer wins");

totCompWins++;

}

else if(gameString[compCh].equals("SCISSOR"))

{

winLabel.setText("YOU wins");

totUserWins++;

}

}

else if(userCStr.equals("PAPER"))

{

compChoiceLabel.setText("Computer Chosen:"+gameString[compCh]);

if(gameString[compCh].equals("ROCK"))

{

winLabel.setText("YOU wins");

totUserWins++;

}

else if(gameString[compCh].equals("PAPER"))

{

winLabel.setText("DRAW");

}

else if(gameString[compCh].equals("SCISSOR"))

{

winLabel.setText("Computer wins");

totCompWins++;

}

}

else if(userCStr.equals("SCISSOR"))

{

compChoiceLabel.setText("Computer Chosen:"+gameString[compCh]);

if(gameString[compCh].equals("ROCK"))

{

winLabel.setText("Computer wins");

totCompWins++;

}

else if(gameString[compCh].equals("PAPER"))

{

winLabel.setText("YOU wins");

totUserWins++;

}

else if(gameString[compCh].equals("SCISSOR"))

{

winLabel.setText("DRAW");

}

}

userWinLabel.setText("YOU WINS "+totUserWins);

compWinLabel.setText("COMPUTER WINS "+totCompWins);

}

public void paint(Graphics g1)

{

     super.paint(g1);

g1.drawString("User Wins"+totUserWins, 200,200);

g1.drawString("Computer Wins"+totCompWins,250,200);

g1.drawString("COURSE ID: CSC 102",0,200);

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote