Question

JAVA We will write a psychic game program in which a player guesses a number randomly...

JAVA

We will write a psychic game program in which a player guesses a number randomly generated by a computer. For this project, we need 3 classes, Player, PsychicGame, and Driver. Description for each class is as following:

Project Folder Name: LB05

1. Player class

a.Input

- nickName : nick name of the player

- guessedNumber : a number picked for the player will be assigned randomly by a computer later.

b. Process

- constructor: asks a user player’s nickName using standard input, and initialize guessedNumber by 0.

- getNickName() : returns nickname.

- getGuessedNumber() : returns guessedNumber.

- play : get a computer-generated random number among 1 through 6, then display the message, “nickname has picked the number guessedNumber” using standard output.

c. Output

2. PsychicGame class

a. Input

- p1 : an object from Player class.

- originalNumber : a number generated randomly by a computer will be compared to a guessedNumber generated by a player.

b. Process

- constructor: initialize an object of Player class, then initialize the originalNumber as 0.

- play : assign a computer-generated random number from 1 to 6 to the originalNumber, display a message, “ The computer has chosen the number originalNumber”, lets the Player class object pick its number, then compare these two numbers. If the numbers are matched, then display a message, “player’s nickname has won.”, or “No one has won this time.”

c.Output

3. Driver class

This is the driver class which runs this application by the main method. However, at the very beginning, please display a header mentioning whose psychic game is by standard output.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

JAVA PROGRAM

Driver class

package LB05;

//This is Driver class to test the PsychicGame
public class Driver {
   public static void main(String[] args){
       System.out.println("XYZ's Psychic game"); //XYZ is a random name taken for program purpose
      
       PsychicGame pg = new PsychicGame(); //PsychicGame instance is created
      
       pg.play();//play() method is called
      
   }

}

PsychicGame class

package LB05;

//Psychic game class
public class PsychicGame {
   private Player p1;
   private int originalNumber;
     
   //constructor
   public PsychicGame(){
       this.p1 = new Player();//Player is initialized
       this.originalNumber = 0;//originalNumber is initialized to 1
   }
     
   public void play(){
       //Random number between 1 and 6 will be generated by below statement.
       // Math.random() will generate anything >= 0 but <1
       //Math.random() will generate anything >= 0 but < 5
       //With the use of Math.round and addition of 1 any integer between 1 and 6 can be generated
       this.originalNumber = 1+ Math.round((float)Math.random()*5);
         
       System.out.println("The computer has chosen the number "+this.originalNumber);
       //this will call player's play() method
       this.p1.play();
         
       //below logic will check whether computer generated number and player's guessed number matched or not
       if(this.originalNumber == this.p1.getGuessedNumber()){
           System.out.println("Player's "+this.p1.getNickName()+ " has won.");
       }else{
           System.out.println("No one has won this time.");
       }
   }

}

Player class

package LB05;

import java.util.Scanner;

//Player class
public class Player {
   private String nickName;
   private int guessedNumber;
  
   //Constructor
  
   public Player(){
       Scanner in = new Scanner(System.in);
       System.out.println("Please enter nickname:");
       this.nickName = in.next(); //User nickname will be taken through scanner
       in.close();//scanner needs to be closed after it's done
       this.guessedNumber = 0;//guessedNumber is initialized to 0.
   }
  
   //getter for nickName
   public String getNickName(){
       return this.nickName;
   }
  
   //getter for guessedNumber
   public int getGuessedNumber(){
       return this.guessedNumber;
   }
  
   /**
   * This method will let the player pick any number between 1 to 6 and display it.
   */
   public void play(){
       //Random number between 1 and 6 will be generated by below statement.
       // Math.random() will generate anything >= 0 but <1
       //Math.random() will generate anything >= 0 but < 5
       //With the use of Math.round and addition of 1 any integer between 1 and 6 can be generated
       this.guessedNumber = 1+ Math.round((float)Math.random()*5);
      
       System.out.println(this.nickName+" has picked the number "+this.guessedNumber);
   }
}

Output:

Run1:

XYZ's Psychic game
Please enter nickname:
hero
The computer has chosen the number 5
hero has picked the number 1
No one has won this time.

Run2:

XYZ's Psychic game
Please enter nickname:
zero
The computer has chosen the number 3
zero has picked the number 5
No one has won this time.

Run3:

XYZ's Psychic game
Please enter nickname:
bob
The computer has chosen the number 2
bob has picked the number 2
Player's Bob has won.

Explanation:

Code comments are given with the code.
Generation of random number between 1 and 6 has been explained in the section where the random number is generated and assigned.

Add a comment
Know the answer?
Add Answer to:
JAVA We will write a psychic game program in which a player guesses a number randomly...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You will write a two-class Java program that implements the Game of 21. This is a...

    You will write a two-class Java program that implements the Game of 21. This is a fairly simple game where a player plays against a “dealer”. The player will receive two and optionally three numbers. Each number is randomly generated in the range 1 to 11 inclusive (in notation: [1,11]). The player’s score is the sum of these numbers. The dealer will receive two random numbers, also in [1,11]. The player wins if its score is greater than the dealer’s...

  • Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that...

    Java CSC252   Programming II    Static Methods, Enums, Constants, Random Rock Paper Scissors Write a program that allows the user to play "Rock, Paper, Scissors". Write the RPS class. All code should be in one file. main() will be part of the RPS class, fairly small, and contain only a loop that asks the user if they want to play "Rock, Paper, Scissors". If they say yes, it calls the static method play() of the RPS class. If not, the program...

  • Use C++ 11 to write the program War Game Requirement Setting This is a 2-player game....

    Use C++ 11 to write the program War Game Requirement Setting This is a 2-player game. It is played through dice. Rule for scoring The player who rolls higher number gets one point. If both players roll the same number, it is considered a draw and no one gets a point. Dice Specification There are two kinds of dice: normal die, represented by Die class. loaded die, represented by the loadedDie class. Classes Die class Die class has a member...

  • C# Code: Write the code that simulates the gambling game of craps. To play the game,...

    C# Code: Write the code that simulates the gambling game of craps. To play the game, a player rolls a pair of dice (2 die). After the dice come to rest, the sum of the faces of the 2 die is calculated. If the sum is 7 or 11 on the first throw, the player wins and the game is over. If the sum is 2, 3, or 12 on the first throw, the player loses and the game is...

  • Number Guessing Game Games Write program in C++. For this game, the computer will select a...

    Number Guessing Game Games Write program in C++. For this game, the computer will select a random number between 1 and 100 (inclusive). The computer will then ask the (human) player to guess the number the computer has selected. After the player’s guess is input to the computer, the computer will output one of three responses, depending on the relationship of the number the player guessed to the number the computer selected: “Your guess was too low.” “Your guess was...

  • Can anyone help me with this java program? We are still very early with lessons so...

    Can anyone help me with this java program? We are still very early with lessons so no advanced code please. And if you comment throughout the code that would be incredibly helpful. Thank you Create 2 Java files for this assignment: Die.java and TestDie.java Part 1 - The Die Class             The program Design a Die class that contains the following attributes: sides: represents how many sides a dice has. A dice usually has 6 sides but sometimes could have...

  • Java Constructor and method

    Player Class Specification The Player class represents a player that will play a simple game. In this game, a player can play the number 0, 1, or 2. The Player class contains the following four fields: private String name; private int play; private int [] tally; private StringBuffer history; The Player class contains the following 2 constructors and 5 methods that you must implement: 1. Constructor with String parameter – If the argument is null, throw the IllegalArgumentException with the...

  • (C++) Please create a tic tac toe game. Must write a Player class to store each...

    (C++) Please create a tic tac toe game. Must write a Player class to store each players’ information (name and score) and to update their information (increase their score if appropriate) and to retrieve their information (name and score).The players must be called by name during the game. The turns alternate starting with player 1 in the first move of round 1. Whoever plays last in a round will play second in the next round (assuming there is one).The rows...

  • Write a program to Simulate a game of tic tac toe in c#

    Write a program to Simulate a game of tic tac toe. A game of tic tac toe has two players. A Player class is required to store /represent information about each player. The UML diagram is given below.Player-name: string-symbol :charPlayer (name:string,symbol:char)getName():stringgetSymbol():chargetInfo():string The tic tac toe board will be represented by a two dimensional array of size 3 by 3 characters. At the start of the game each cell is empty (must be set to the underscore character ‘_’). Program flow:1)    ...

  • PLEASE INCLUDE SAW-PROMPTS FOR 2 PLAYERS NAMES(VALIDATE NAMES). SHOW MENU (PLAYER MUST SELECT FROM MENU B4...

    PLEASE INCLUDE SAW-PROMPTS FOR 2 PLAYERS NAMES(VALIDATE NAMES). SHOW MENU (PLAYER MUST SELECT FROM MENU B4 THE GAME STARTS 1=PLAY GAME, 2=SHOW GAME RULES, 3=SHOW PLAYER STATISTICS, AND 4=EXIT GAME WITH A GOODBYE MESSAGE.) PLAYERS NEED OPTION TO SHOW STATS(IN A DIFFERNT WINDOW-FOR OPTION 3)-GAME SHOULD BE rock, paper, scissor and SAW!! PLEASE USE A JAVA GRAPHICAL USER INTERFACE. MUST HAVE ROCK, PAPER, SCISSORS, AND SAW PLEASE This project requires students to create a design for a “Rock, Paper, Scissors,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT