Question


Lab 5 Rewrite the program in Lab 1 in object-oriented programming. You need to analyze the functionalities in Lab 1. You mayLab 1 Write a program for number-guessing game (you may refer to Guess the Number if you have never played this game). The pr

lab5 is to rewrite lab1 by oop

the code down below is the code of lab1

impor ava.u .Bcanner; import java.util.Random; public class homewOrkl public e acanner (Svatem.in) eanne int start-D, end-99

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

Hi i am giving you the same in multiple classes using oops concept hope this will be helpful to you kindly give a thumbs up if it helps you and do comment if you have any doubt regarding the same.

code:-

Main.java

import java.util.Scanner;
import java.util.Random;

public class Main
{
   public static void main(String[] args) {
   GuessGame game = new GuessGame();
game.init();
  
   }
}

GuessGame.java

import java.util.Scanner;

public class GuessGame {
private Scanner input;
private Player player;
private NumberGuess guessNumberGame;
private boolean running;
private String command;

GuessGame() {
this.player = new Player();
this.input = new Scanner(System.in);
this.guessNumberGame = new NumberGuess(100);
this.running = true;
}

public void init() {
while (running) {

this.player.play(this.input, this.guessNumberGame);
this.guessNumberGame.setSecretNumber(100);
}
}
}

NumberGuess.java

import java.util.Random;

public class NumberGuess {
private int secretNumber;
private Random random;

NumberGuess(int limit) {
this.random = new Random();
this.setSecretNumber(limit);
}

public void setSecretNumber(int limit) {
this.secretNumber = this.random.nextInt(limit);
}

public int getSecretNumber() {
return this.secretNumber;
}
}

Player.java

import java.util.Scanner;

public class Player {
public void play(Scanner input, NumberGuess game) {
int tries = 5;
int number = 0;
Scanner s = new Scanner(System.in);
int start = 0 , end = 99;
while (true) {
       if(end-start <= 1) {
       System.out.println("You lose .....");
       break;
       }
       int guess;
       System.out.print(start+" "+end+"\n");
       guess = s.nextInt();
       if(guess== game.getSecretNumber()) {
       System.out.println("You Win !");
       break;
       }
       if(guess<start || guess >end) {
       System.out.println("Out of Range , try again");
       continue;
       }
       if(guess<game.getSecretNumber()) {
       System.out.println("too small");
       start = guess+1;
       continue;
       }
       if(guess>game.getSecretNumber()) {
       System.out.println("too large");
       end = guess-1;
       }
       }
}
}

you just need to create the classes as mentioned above and run it i am not able to upload the output image getting 504 error so run it your self you will get the same output as you are getting in your code . happy to help kindly give thumbs up.

Add a comment
Know the answer?
Add Answer to:
lab5 is to rewrite lab1 by oop the code down below is the code of lab1...
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
  • Has to be written in C++. Visual studio. Write a C++ program to realize the game...

    Has to be written in C++. Visual studio. Write a C++ program to realize the game of guessing the number. Generally, the game will generate a random number and the player has to find out the number. In each guess, the program will give you a feedback as your guess is correct, too large, or too small. Then the player play repetitively until find out the number. Specifically, the game plays as the following. a). When the game is started,...

  • Part 1: Python code; rewrite this code in C#, run the program and submit - include...

    Part 1: Python code; rewrite this code in C#, run the program and submit - include comments number= 4 guesscount=0 guess=int(input("Guess a number between 1 and 10: ")) while guess!=number: guesscount=guesscount+1 if guess<number: print("Your guess is too low") elif guess>number: print("Your guess is too high") else: print("You got it!!") guess=int(input("Guess again: ")) print("You figured it out in ",guesscount," guesses") Part 2: C++ code; rewrite the following code in C#. Run the program and submit. - include comments #include <iostream> using...

  • Java Listed below is code to play a guessing game. In the game, two players attempt...

    Java Listed below is code to play a guessing game. In the game, two players attempt to guess a number. Your task is to extend the program with objects that represent either a human player or a computer player. boolean checkForWin (int guess, int answer) { System.out.println("You guessed" + guess +"."); if (answer == guess) { System.out.println( "You're right! You win!") ; return true; } else if (answer < guess) System.out.println ("Your guess is too high.") ; else System.out.println ("Your...

  • Write a guessing game where the user has to guess a secret number. After every guess...

    Write a guessing game where the user has to guess a secret number. After every guess the program tells the user whether their number was too large or too small. At the end the number of tries needed should be printed. It counts only as one try if they input the same number multiple times consecutively. Using PYTHON.

  • Can someone upload a picture of the code in matlab Write a "Guess My Number Game"...

    Can someone upload a picture of the code in matlab Write a "Guess My Number Game" program. The program generates a random integer in a specified range, and the user (the player) has to guess the number. The program allows the use to play as many times as he/she would like; at the conclusion of each game, the program asks whether the player wants to play again The basic algorithm is: 1. The program starts by printing instructions on the...

  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

  • Can you please enter this python program code into an IPO Chart? import random def menu():...

    Can you please enter this python program code into an IPO Chart? import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the non-numbers #if user enters letters, then except will show the message, Numbers only! try: c=int(input("Enter your choice: ")) if(c>=1 and c<=3): return c else: print("Enter number between 1 and 3 inclusive.") except: #print exception print("Numbers Only!")...

  • using python in eclipses Question I am attempting to solve My attempt... 1. Add a new...

    using python in eclipses Question I am attempting to solve My attempt... 1. Add a new module named twenty_questions_class to the package library. 2. Define a class named TwentyQuestions in the module with an instance constructor and a method. a. The instance constructor _init is used to create an instance of the TwentyQuestions class with a value for the RANGE. b. The method named play lets the user play the game. For example, I am thinking of a secret number...

  • In Java You’re going to make a Guess the Number game. The computer will "think" of...

    In Java You’re going to make a Guess the Number game. The computer will "think" of a secret number from 1 to 20 and ask the user to guess it. After each guess, the computer will tell the user whether the number is too high or too low. The user wins if they can guess the number within six tries. The program should look like this in the console, player input is in bold: Hello! What is your name? Abaddon...

  • Currently, the game allows players to play as many times as they wish. It does not...

    Currently, the game allows players to play as many times as they wish. It does not provide any feedback on how the players are doing, however. Modify the game so that it keeps track of the number of games played as well as the average number of guesses made per game. To implement this change, add the following list of global variables to the beginning of the script’s Main Script Logic section. Assign each variable an initial value of zero....

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
ADVERTISEMENT