Question

In: Computer Science

This assignment will acquaint you with the use of while loops and boolean expressions. You will...

This assignment will acquaint you with the use of while loops and boolean expressions. You will create a program that acts as a score keeper of a racquet ball game between two players. The program will continually ask the user who the winner of every point is as the game is played. It will maintain the scores and declare the match is over, using these rules: (1) A game is over when a. one of the players wins 7 points or, b. one of the players wins 3 points while the other player has 0 points. (2) The match is over when one of the players wins 2 games. The program will run a single match. See sample runs below. Write a complete class called RacquetBallMatch that has the following: • (8 points) playGame method: that plays one full game of 7 points between two players Allen and Bob. This method accepts a Scanner object and returns the name of the winner of this game. Specifically, this method repeatedly asks the user “who the winner of the next point is” until the game is over by following the rules mentioned above. The user is expected to enter the first letter of the player’s names, i.e. A for Allen and B for Bob. (Make the input case insensitive) See sample runs below. • (2 points) printGameScores method: accepts points of two players and prints the scores. See sample runs below. • (2 points) printMatchScores method: accepts game counts of two players and prints the scores. See sample runs below. • (5 points) main method: o Welcomes the user. o Declares and initializes a Scanner object to be used throughout to read user input. o Calls the playGame method repeatedly until the winner of a 3-game set can be declared. For example, if Bob wins the first 2 games, he would be declared the match winner and the program would stop. • (3 points) Include appropriate program documentation and formatting including: Your first and last name, the date of submission, code comments necessary to explain the operation of your program, and proper indentation of the code, etc. Notes: • For each of the methods, think about the following: What is the return type, what parameter(s) will it need to perform the task, and accordingly decide the method signature for each. • Don’t use static variables (variables that are declared outside of all the methods). It is ok to have class constants (variables declared with final keyword). • Use while-loops to handle the repetition. • Make sure there is no code-duplication. • Use a Boolean variable that captures the winning condition for a game or the match and use it in the while-loop conditions.

Solutions

Expert Solution

import java.util.Scanner;
import java.lang.*;
class Main
{
  static char playGame (Scanner sc)
  {
    int a = 0, b = 0;
    char win;
    while (true)
    {
            System.out.print("Enter round winner : ");
            win = sc.next ().charAt(0);
            System.out.println();
            win = Character.toLowerCase (win);
            if (win == 'a')
                a++;
            else
                b++;
            if (a == 3 && b == 0)
                return 'A';
            else if (b == 3 && a == 0)
                return 'B';
            else if (a == 7)
                return 'A';
            else if (b == 7)
                return 'B';
    }
  }

  static void printGameScores (int a, int b)
  {
    System.out.println ("Allen's score : " + a);
    System.out.println ("Bob's score : " + b);
  }

  static void printMatchScores (int a, int b)
  {
    System.out.println ("Games won by A : " + a);
    System.out.println ("Games won by B : " + b);
  }

  public static void main (String[]args)
  {
    System.out.println ("Welcome to Racquet Ball simulator\n");
    Scanner sc = new Scanner (System.in);
    char win;
    String winner = "";
    int a = 0;
    int b = 0;
    boolean matchOver = false;
    while (!matchOver)
      {
        win = playGame (sc);
        System.out.println("Game over.\n");
        if (win == 'A')
          a++;
        else
          b++;
        if (a == 2)
          {
            winner = "Allen";
            matchOver = true;
          }
        else if (b == 2)
          {
            winner = "Bob";
            matchOver = true;
          }
      }
    System.out.println ("The winner of the match is " + winner);
  }
}

I'm not sure where I was supposed to call the printing functions, so I've just declared them. Let me know if you need any changes!


Related Solutions

1. Use Boolean algebra to simplify the following Boolean expressions to expressions containing a minimum number...
1. Use Boolean algebra to simplify the following Boolean expressions to expressions containing a minimum number of literals: (a) A’C’ + A’BC + B’C (b) (A + B + C)’(ABC)’ (c) ABC’ + AC (d) A’B’D + A’C’D + BD (e) (A’ + B)’(A’ + C’)’(AB’C)’ (f) (AE + A’B’)(C’D’ + CD) + (AC)’ 2. Obtain the truth table of the function F = (AB + C)(B + AC), express the function F in sum-of-minterms and product-of-maxterms forms, and express...
Assignment Description This assignment focuses on programming basics; expressions, variables, constants, methods, selection and loops. Danny...
Assignment Description This assignment focuses on programming basics; expressions, variables, constants, methods, selection and loops. Danny runs an ice cream shop in the inner suburbs of Melbourne. Due to the growing number of customers, Danny has decided to take on extra casual employees. In order to manage payroll for his employees, Danny has decided to develop an employee payroll management system. Details of each employee to be maintained in the system will include; employee id, name, gender (M or F),...
Simplify the following Boolean expressions to the minimum number of terms using the properties of Boolean...
Simplify the following Boolean expressions to the minimum number of terms using the properties of Boolean algebra (show your work and write the property you are applying). State if they cannot be simplified A. X’Y + XY B. (X + Y)(X + Y’) C. (A’ + B’) (A + B)’ D. ABC + A’B + A’BC’ E. XY + X(WZ + WZ’)
Simplify the following Boolean expressions to the minimum number of terms using the properties of Boolean...
Simplify the following Boolean expressions to the minimum number of terms using the properties of Boolean algebra (show your work and write the property you are applying). State if they cannot be simplified. A. A’B + AB B. XY + X(WZ + WZ’) C. X’Y’(X’+Y)(Y’+Y) D. ABC + A’B + A’BC’ E. (A+B)(AC+AC’)+AB+B Draw the circuit logic diagrams for both the original and simplified expressions.
Modify the following code to use ONLY pointer arithmetic (no array expressions) and no for loops...
Modify the following code to use ONLY pointer arithmetic (no array expressions) and no for loops to do the same thing this code does. Be sure that you understand how the code works and how the pointer arithmetic relates to the array expression form. Provide liberal comments to explain what your pointer arithmetic is computing. #include <stdlib.h> #include <stdio.h> int main(int argc, char **argv) { int arg_count = 0; for (arg_count = 0; arg_count < argc; arg_count++) printf("%s\n", argv[arg_count]); }
Lab 7. Boolean Expressions a) Write a program that evaluates the following expressions. Assign reasonable values...
Lab 7. Boolean Expressions a) Write a program that evaluates the following expressions. Assign reasonable values to the variables. Print the results. a<b≥c , √a−7 b2 ≠c , d∨e∧f , a<b∨¬d ∧means and, ∨means inclusive or, ¬ means not. b) Write a program that asks a user whether he or she wants to become a Java programmer and determines if the user typed “yes” (Print true if yes and false otherwise.) Don't use the if statement here
Python code Assignment Overview In this assignment you will practice with conditionals and loops (for). This...
Python code Assignment Overview In this assignment you will practice with conditionals and loops (for). This assignment will give you experience on the use of the while loop and the for loop. You will use both selection (if) and repetition (while, for) in this assignment. Write a program that calculates the balance of a savings account at the end of a period of time. It should ask the user for the annual interest rate, the starting balance, and the number...
Objectives To reinforce the use of If-Else statements To learn how to use while loops Introduction:...
Objectives To reinforce the use of If-Else statements To learn how to use while loops Introduction: Mission to Mars Your friend has been playing a new Mars Colony simulator nonstop! They are always talking about how cool it would be if they could be a on the first real-life mission to Mars! To amuse your friend, you have decided to create a series of programs about the possible first colony on Mars. Problem: Where Can We Get the Best Deal?...
Determine the value, true or false, of each of the following Boolean expressions, assuming that the...
Determine the value, true or false, of each of the following Boolean expressions, assuming that the value of the variable count is 0 and the value of the variable limit is 10. Give your answer as one of the values true or false. a. (count == 0) && (limit < 20) b. count == 0 && limit < 20 c. (limit > 20) || (count < 5) d. !(count == 12) e. (count == 1) && (x < y) f....
In the given instruction, I am to use while loops only. The goal is to prompt...
In the given instruction, I am to use while loops only. The goal is to prompt the user to select an acceptable input. If the wrong input is given, the program forces the user to select an appropriate input. The program also keeps running until the user chooses to exist by selecting a very specific input which in my case is the upper or lower case "E". The problem is even after selecting upper or lower case "E", my program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT