You are on page 1of 18

Program Name and Code:(22412) Academic Year : 2022-2023

Course Name and Code: Java Programming Semester : fourth

ASTUDY ON

DEVELOP A ‘JAVA PROGRAM ON NUMBER GUESSING GAME’

MICRO PROJECT
Submitted by 4 students

Sr. Roll No Enrollment Seat No


Full Name Of Students
No (Sem-III) No (Sem-III)

1 2220 JANHVI VIJAYKUMAR 2117340059 275150


KATAKTHOND

2 2221 KULSOOM FEROZ KAZI 2117340029 27520

3 2242 SADIYA KHURSHID SAYED 2117340034 275125

4 2262 SEJAL MANGESH 2117340232 275178


GHORPADE

Under the Guidance of

PROF.MS.TAMANNA SHAIKH
in

Three Years Diploma in Engineering & Technology of Maharashtra State Board of Technical
Education, Mumbai (Autonomous)

SO 9001:2008 (ISO/IEC-27001:2013)

At

1734 – TRINITY POLYTECHNIC PUNE


MAHARASHTRA STATE BOARD OF
TECHNICAL EDUCATION, MUMBAI

Certificate

This is to certify that Mr. /Ms.

Roll No: of Fourth Semester of Diploma

Programme in Engineering & Technology at 1734 – Trinity Polytechnic Pune, has

completed the Micro Project satisfactorily in Subject ______________in the academic

year 2022-23 as per the MSBTE prescribed curriculum of I Scheme.

Place: Enrollment No:

Date: / /2023 Exam seat No:

Project Guide Head of the Department Principal


Seal of Institute
INDEX

Sr No. Title

1. Introduction

2. Abstract

3. Information

4. Code

5. Conclusion

6. Reference

INTRODUCTION

What is the Guessing Game?

A Guessing Game is a game where the computer will choose any random
number between 1 and 100, and you have to guess that number. Every time you
choose a number, the computer will tell you whether your guess is right or
wrong than the number selected by the computer. Well, keep in mind that you
Trinity Polytechnic Pune

only get three tries to guess the number. At the end of the game, it will tell you
what number was chosen by the computer (if you are unable to guess the correct
number). And, if you choose the correct number, then it tells you how many
tries it took you to guess the number

Computer Department 4
Trinity Polytechnic Pune

ABSTRACT

About Project

Here, we are going to construct Project named Number Guessing Game in


which the user will be provided a range and the user have to guess a number in
a limited number of attempts.

What are we Building?

The aim is to create a Java Program in which a user is given K chances to


estimate a randomly generated number. The game's rules are as follows:

 If the guessed number is more than the real number, the program will
display the message "guessed number greater than the actual number."
 If the guessed number is less than the real number, the program will
display a message indicating that the guessed number is less than the
actual number.
 If the guessed number is the same as the real number, or if all K trials
have been completed, the program will terminate with an appropriate
message.

Computer Department 5
Trinity Polytechnic Pune

INFORMATION

CONSTRAINTS USED TO BUILD THE CODE:

Random Numbers
Random numbers are the numbers that use a large set of numbers and select a single number
from them using a mathematical algorithm. It satisfies the following two conditions :

 The generated values are uniformly distributed over a definite interval.


 It is impossible to guess the future value based on current and past values.

A few good applications of random numbers are :

 Dice Roller
 Scientific Hypothesis
 Statistics
 Random number series, and many more.

The Math Class and random() Method

The Java Math class contains numerous methods for performing various mathematical
computations. The random() method is one of them. It belongs to the Math class and is a
static method. As you may know, in Java, static methods are methods that can be called
without first generating a class instance.

So, the random function being a static method, can be called directly. Only double data type
random numbers will be generated, and the generated number can be larger than or equal to
0.0 and less than 1.0 are generated. We must first import the java.lang.Math class before we
can use the random() method, or else the compiler will throw an error.

Computer Department 6
Trinity Polytechnic Pune

Control statements

Control Statements are the base of any programming language. Using control
statements we implement real world scenarios in programs. There are three
types of Control Statements in Java:

 Decision-Making Statements
 Loop Statements
 Jump or Branch Statements

Decision making statements execute a piece of code based on some condition.


Looping Statements execute a piece of code repeatedly until a condition
becomes false. Jump or branch statements help in transferring the control of
the flow of execution to a specific point in the code.

 Decision-Making Statements

1. If Statement

These are the simplest and yet most widely used control statements in Java. The
if statement is used to decide whether a particular block of code will be
executed or not based on a certain condition.

If the condition is true, then the code is executed otherwise not.

Let's see the execution flow of the if statement in a flow diagram:

Computer Department 7
Trinity Polytechnic Pune

2. if-else Statement

The if statement is used to execute a block of code based on a condition. But if


the condition is false and we want to do some other task when the condition is
false, how should we do it?

That's where else statement is used. In this, if the condition is true then the code
inside the if block is executed otherwise the else block is executed.

Let's see the execution flow of the if-else statement in a flow diagram:

Computer Department 8
Trinity Polytechnic Pune

 Loop Statements

1. while Loop

The while loop statement is the simplest kind of loop statement. It is used to
iterate over a single statement or a block of statements until the specified
boolean condition is false.

The while loop statement is also called the entry-control looping statement
because the condition is checked prior to the execution of the statement and as
soon as the boolean condition becomes false, the loop automatically stops.

You can use a while loop statement if the number of iterations is not fixed.

Normally the while loop statement contains an update section where the
variables, which are involved in while loop condition, are updated.

Let's see the execution flow of the while loop statement in a flow diagram:

Computer Department 9
Trinity Polytechnic Pune

2. for Loop

Unlike the while loop control statements in Java, a for loop statement consists of the
initialization of a variable, a condition, and an increment/decrement value, all in one line. It
executes the body of the loop until the condition is false.

The for loop statement is shorter and provides an easy way to debug structure in Java. You
can use the for loop statement if the number of iterations is known.

In a for loop statement, execution begins with the initialization of the looping variable, then it
executes the condition, and then it increments or decrements the looping variable.

If the condition results in true then the loop body is executed otherwise the for loop statement
is terminated.

Let's see the execution flow of the for loop statement in a flow diagram:

Computer Department 10
Trinity Polytechnic Pune

CODE

import java.util.Scanner;

import java.util.Random;

public class guessingGame2

static int numRandom()//custom function numRandom()

Random generator=new Random();

int numRandom=generator.nextInt(100)+1;

return numRandom;

public static void main(String[] str)

//Variable Declaration//

int numRandom;

int numGuess1;

int numGuess2;

int numGuess3;

String display1= ("!!!You guessed it right!!!");

String display2=("!!!Sorry, that is not correct!!!");

String display3=("The correct number is ");

String greeting1=("!!!Welcome to the Guessing Game!!!");

String greeting2=("!!!Where you get 3 chances to guess a number from 1 to 100


and see if you’re right!!!");

Computer Department 11
Trinity Polytechnic Pune

String prompt1=("Would you like to play?");

String choice1=("1. Yes");

String choice2= ("2. No");

String prompt2=("Please enter a number 1 to 100:");

String second=("Please enter your second guess: ");

String last=("Please enter your final guess:");

String prompt3=(" Would you like to play again?");

String goodbye1=(" !!!Goodbye!!!");

String goodbye2=(" !!!  Thanks for playing!!!");

int reply=0;

//Scanner object//

Scanner keyboard=new Scanner(System.in);

//Main Program//

System.out.println(greeting1);

System.out.println(" ");

System.out.println(greeting2);

System.out.println(" ");

System.out.println(prompt1);System.out.println(choice1);System.out.println(cho
ice2);

reply = keyboard.nextInt();

//Begin while loop//

while (reply==1)

numRandom=numRandom();//calling for a random number.//

Computer Department 12
Trinity Polytechnic Pune

System.out.print(prompt2);

numGuess1=keyboard.nextInt();

System.out.println(" ");System.out.print(second);

numGuess2=keyboard.nextInt();

System.out.println(" ");System.out.print(last);

numGuess3=keyboard.nextInt();

if (numGuess1==numRandom||numGuess2==numRandom||numGuess3==numRandom)        
//if loop1//

System.out.println(" ");

System.out.println(display1);

System.out.println(" ");

else

System.out.println(" ");

System.out.println(display2);

System.out.println(" ");

System.out.println(display3+numRandom+" ");

System.out.println(" ");

System.out.println(prompt3);

System.out.println(choice1);

System.out.println(choice2);

reply=keyboard.nextInt();

Computer Department 13
Trinity Polytechnic Pune

if (reply==2)

System.out.println(goodbye2);

while (reply==2)

for(;reply>=2;reply--)

System.out.println(goodbye1);

Computer Department 14
Trinity Polytechnic Pune

PROGRAM OUTPUT

Computer Department 15
Trinity Polytechnic Pune

Computer Department 16
Trinity Polytechnic Pune

CONCLUSION

 In this article we have created a guessing game in java.


 First we generated a number and then ask the user for answer k
times(trials)
 If the guessed number is more than the real number, the program will
display the message "guessed number greater than the actual number."
 If the guessed number is less than the real number, the program will
display a message indicating that the guessed number is less than the
actual number.
 If the guessed number is the same as the real number, or if all K trials
have been completed, the program will terminate with an appropriate
message.
 To Guess the number we can use a binary search algorithm For example
if the number generated is 25 then we can first check if the answer is
(0+100/2)=50 as it is higher than then answer then we will move our low
to 0 and high to 50 then mid will be 25 which is the answer.

Computer Department 17
Trinity Polytechnic Pune

REFERENCES

https://www.scaler.com/topics/
https://www.scaler.com/topics/math-random-in-java/
https://www.scaler.com/topics/control-statements-in-java/

Computer Department 18

You might also like