Question

In: Computer Science

How do I write this method correctly? What Ihave is a Java project assignment where I'm...

How do I write this method correctly? What Ihave is a Java project assignment where I'm supposed to create a Roshambo game. One of the classes that the project is supposed to have is a class called "RoshamboApp that let the user play 1 of 2 AI opponents, a "Bart" class, and a "Lisa" class. The instructions say "Create a class names RoshamboApp that allows player1 to play Bart or Lisa as shown in the console output. Rock should beat scissors, paper should beat rock, and scissors should beat paper." The code for a switch statement that I wrote is below, but the IDE says the way I wrote it is wrong. How can I write it correctly?

The assignment:

Project 10-3: Roshambo

Create an application that uses an enumeration and an abstract class.

Console

Welcome to the game of Roshambo

Enter your name: Joel

Would you like to play Bart or Lisa? (B/L): b

Rock, paper, or scissors? (R/P/S): r

Joel: rock

Bart: rock

Draw!

Play again? (y/n): y

Rock, paper, or scissors? (R/P/S): p

Joel: paper

Bart: rock

Joel wins!

Play again? (y/n): y

Rock, paper, or scissors? (R/P/S): s

Joel: scissors

Bart: rock

Bart wins!

Play again? (y/n): n

Specifications

  • Create an enumeration named Roshambo that stores three values: rock, paper, and scissors. This enumeration should include a toString() method that can convert the selected value to a string.
  • Create an abstract class named Player that stores a name and a Roshambo value. This class should include an abstract method named generateRoshambo() that allows an inheriting class to generate and return a Roshambo value. It should also include get and set methods for the name and Roshambo value.
  • Create classes named Bart and Lisa that inherit the Player class and implement the generateRoshambo() method. The Bart class should always select rock. The Lisa class should randomly select rock, paper, or scissors (a 1 in 3 chance of each).
  • Create a class named Player1 that represents the player who will play Bart or Lisa. This class should inherit the Player class and implement the generateRoshambo() method. This method can return any value you choose. (You must implement this method even though it isn’t used for this player).
  • Create a class named RoshamboApp that allows player 1 to play Bart or Lisa as shown in the console output. Rock should beat scissors, paper should beat rock, and scissors should beat paper.
  • Use an enhanced version of the Console class to get and validate the user’s entries.

My code so far for the class in question:

// R = rock
// P = paper
// S = scissors

import java.util.Scanner;

public class RoshamboApp {

String R;
String P;
String S;   
String UserChoice;  
String AIChoice;

switch(UserChoice)
{
    case R:
        if (AIChoice == P) System.out.println("you lose.");
        if (AIChoice == S) System.out.println("you win!");
    case P:
        if (AIChoice == R) System.out.println("you win!");
        if (AIChoice == S) System.out.println("you lose.");
    case S:
        if (AIChoice == R) System.out.println("you lose.");
        if (AIChoice == P) System.out.println("you win!");      
}
if (UserChoice == AIChoice) System.out.println("it's a draw.");

}

Solutions

Expert Solution

/***************************Roshambo.java*******************/

public enum Roshambo

{
   ROCK, PAPER, SCISSORS;

   @Override
   public String toString() {
       if (this.ordinal() == 0)
           return "Rock";
       if (this.ordinal() == 1)
           return "Paper";
       if (this.ordinal() == 2)
           return "Scissors";
       return "";
   }
}

/*****************************Bart.java**********************/

public class Bart extends Player {
   public Bart() {
       name = "Bart";
   }

   // implements the generateRoshambo method from the Player class
   @Override
   public Roshambo generateRoshambo() {
       return Roshambo.ROCK;
   }
}

/***********************Lisa.java*****************/

import java.util.Random;

public class Lisa extends Player {
   public Lisa() {
       name = "Lisa";
   }

   /**
   *
   * @return  
   */
   @Override
   public Roshambo generateRoshambo() {
       // randomly selects rock,paper, or scissors
       // (a one in three chance)

       Random r = new Random();
       int ch = r.nextInt(3);
       if (ch == 0)
           return Roshambo.ROCK;
       else if (ch == 1)
           return Roshambo.PAPER;
       else
           return Roshambo.SCISSORS;
   }
}

/****************************Player1.java*****************/

public class Player1 extends Player {
   public Player1() {
       super();
   }

   @Override
   public Roshambo generateRoshambo() {
       if (response.compareTo("R") == 0 || response.compareTo("r") == 0)
           return Roshambo.ROCK;
       if (response.compareTo("P") == 0 || response.compareTo("p") == 0)
           return Roshambo.PAPER;
       return Roshambo.SCISSORS;
   }
}

/********************************Player.java*****************/

public abstract class Player {

   public String name;
   public String response;

   public Player() {
   }

   // set method for name
   public void setName(String name) {
       this.name = name;
   }

   // get method for name
   public String getName() {
       return name;
   }

   // set method for response
   public void setResponse(String response) {
       this.response = response;
   }

   // get method for response
   public String getResponse() {
       return response;
   }

   // abstract method that allows an inheriting class
   // to generate and return a Roshambo value
   public abstract Roshambo generateRoshambo();
}

/*******************************RoshamboApp.java*******************************/

import java.util.Scanner;

/**
*
* @author Christina
*/
public class RoshamboApp

{

   public static void main(String args[])

   {
       Scanner s = new Scanner(System.in);
       char choice = ' ';
       char player;
       Player p = null;
       String name;

       // create 2-objects of Bart and lisa
       Bart b = new Bart();
       Lisa l = new Lisa();
       System.out.println("Welcome to the game of Roshambo\n");

       // input name
       System.out.print("Enter your name : ");
       name = s.nextLine();
       System.out.print("Would you like to play Bart or Lisa?(B/L): ");
       String playerChoice = s.nextLine();
       // playing neighbour choice
       player = playerChoice.charAt(0);

       // either bart
       if (player == 'b' || player == 'B')
           p = b;
       // or lisa
       else if (player == 'l' || player == 'L')
           p = l;

       // set exact name Bart for b|B, Lisa for l|L
       if (playerChoice.charAt(0) == 'b' || playerChoice.charAt(0) == 'B')
           playerChoice = "Bart";
       else if (playerChoice.charAt(0) == 'l' || playerChoice.charAt(0) == 'L')
           playerChoice = "Lisa";
       p.setName(playerChoice);

       // repeating loop with character variable choice

       do {

           System.out.print("Rock, paper or scissors?(R/P/S) : ");
           String rps = s.nextLine();
           Player1 p1 = new Player1();
           p1.setName(name);
           p1.setResponse(rps);

           // set Rock for r|R ,Paper for p|P, Scissors for s|S
           if (rps.charAt(0) == 'r' || rps.charAt(0) == 'R')
               rps = "Rock";
           else if (rps.charAt(0) == 'p' || rps.charAt(0) == 'P')
               rps = "Paper";
           else if (rps.charAt(0) == 's' || rps.charAt(0) == 'S')
               rps = "Scissors";
           System.out.println(name + " : " + rps);

           // main logic generate random number only ones.Each time it will give different
           // number
           String roshambo = p.generateRoshambo().toString(); // generate opponent's roshambo
           System.out.println(p.getName() + " : " + roshambo);

           // match generated random number to rashambo enterd by user for all cases
           if (Character.toUpperCase(rps.charAt(0)) == roshambo.charAt(0))
               System.out.println("Draw!");

           else if (rps.equals("Paper") && roshambo.equals("Rock"))
               System.out.println(p1.getName() + " Wins");

           else if (roshambo.equals("Paper") && rps.equals("Rock"))
               System.out.println(p.getName() + " Wins");

           else if (rps.equals("Paper") && roshambo.equals("Scissors"))
               System.out.println(p.getName() + " Wins!");

           else if (roshambo.equals("Paper") && rps.equals("Scissors"))
               System.out.println(p1.getName() + " Wins!");

           else if (rps.equals("Scissors") && roshambo.equals("Rock"))
               System.out.println(p.getName() + " Wins");

           else if (roshambo.equals("Scissors") && rps.equals("Rock"))
               System.out.println(p1.getName() + " Wins");

           // see if the user wants to continue

           System.out.print("Play again?(y/n): ");
           choice = s.nextLine().toLowerCase().charAt(0);
           System.out.println();

       } while (choice != 'n');

   }

}

/******************output*******************/

Welcome to the game of Roshambo

Enter your name : Joel
Would you like to play Bart or Lisa?(B/L): b
Rock, paper or scissors?(R/P/S) : r
Joel : Rock
Bart : Rock
Draw!
Play again?(y/n): y

Rock, paper or scissors?(R/P/S) : P
Joel : Paper
Bart : Rock
Joel Wins
Play again?(y/n): n

Please let me know if you have any doubt or modify the answer, Thanks :)


Related Solutions

I'm working in Java and am working on a project where I need to find an...
I'm working in Java and am working on a project where I need to find an average. The catch is that for some of the values there is no data because they will be entered at a later date. I have variables assigned so that for each entry if there is an input I'll have it say _____available = 1, otherwise the variable will equal 0. I'll use an example to make this more clear. Let's say I am trying...
I have a lab assignment that I'm not sure how to do. The experiment is a...
I have a lab assignment that I'm not sure how to do. The experiment is a cart moving 60cm distance and there is a fan on top of it making it have a mass of .56kg. Every trial there is 100g added to the cart. For this part, the time is kept the same. 1. If the force provided by the fan was the same for each run and we have chosen the same time interval, how does the impulse...
Hey, I'm stuck on this assignment for AP Comp Sci Java. I don't know how to...
Hey, I'm stuck on this assignment for AP Comp Sci Java. I don't know how to start. An array of String objects, words, has been properly declared and initialized. Each element of words contains a String consisting of lowercase letters (a–z). Write a code segment that uses an enhanced for loop to print all elements of words that end with "ing". As an example, if words contains {"ten", "fading", "post", "card", "thunder", "hinge", "trailing", "batting"}, then the following output should...
(JAVA) I was wondering if anyone can check if I am implementing this method correctly based...
(JAVA) I was wondering if anyone can check if I am implementing this method correctly based on the instructions? Here are the instructions: buildShape(String input) In the buildShape() method, you will parse the provided input to determine whether to create a CircleShape or SquareShape. Before you create the shape, you need to determine it's size, location and color. Use TestableRandom to randomly generate an int size ranging from 100-200. Randomly generate an x and y index for its location. X...
I'm having trouble with my ZeroDenominatorException. How do I implement it to where if the denominator...
I'm having trouble with my ZeroDenominatorException. How do I implement it to where if the denominator is 0 it throws the ZeroDenominatorException and the catch catches to guarantee that the denominator is never 0. /** * The main class is the driver for my Rational project. */ public class Main { /** * Main method is the entry point to my code. * @param args The command line arguments. */ public static void main(String[] args) { int numerator, denominator =...
Hi! I have a homework question I'm not sure if I'm doing it correctly. Below is...
Hi! I have a homework question I'm not sure if I'm doing it correctly. Below is the information from the question and my answer in bold. Glass Company makes glass orders based on the customer specifications, so the company uses job costing to track costs. The company uses direct labor hours as the cost driver for manufacturing overhead application. Manufacturing overhead costs for the year:        $787,500 Usage of direct labor hours for the year:               225,000 Beginning Work-in-process, March 1 (Job 57)      $80,000 Beginning...
I'm working on a project where I need to design a transmission line tap for a...
I'm working on a project where I need to design a transmission line tap for a station generator. The parameters include sizing three switches for the tap where at least one is a loop break switch. From where to start? What kind of calculations are needed to size the switches? What are those switches?
This is for an accounting assignment and I'm not sure where I'm going wrong. I'll copy...
This is for an accounting assignment and I'm not sure where I'm going wrong. I'll copy and paste what I have and the directions as best as possible. PLEEEASE HELP: June 22: Received a bill for $1,190 from Computer Parts and Repair Co. for repairs to the computer equipment. It's telling me my rep and maintenance expense is wrong. I entered: Repairs & Maint. Expense 1190 Accounts payable 1190 It's for Byte of Accouting. What else would this transaction be...
JAVA- How do I edit the following code as minimally as possible to add this method...
JAVA- How do I edit the following code as minimally as possible to add this method for calculating BMI? BMI Method: public static double calculateBMI(int height, int weight) { double BMI = (((double) weight) * 0.453592d) / ((((double) height) * 0.0254) * (((double) height) * 0.0254)); Format f = new DecimalFormat("##.######"); return (f.format(BMI)); } Code: import java.text.DecimalFormat; import java.util.Scanner; public class test2 { public static void main(String[] args) { DecimalFormat f = new DecimalFormat("##.0"); Scanner reader = new Scanner(System.in); System.out.printf("%10s...
The source code I have is what i'm trying to fix for the assignment at the...
The source code I have is what i'm trying to fix for the assignment at the bottom. Source Code: #include <iostream> #include <cstdlib> #include <ctime> #include <iomanip> using namespace std; const int NUM_ROWS = 10; const int NUM_COLS = 10; // Setting values in a 10 by 10 array of random integers (1 - 100) // Pre: twoDArray has been declared with row and column size of NUM_COLS // Must have constant integer NUM_COLS declared // rowSize must be less...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT