# 10 ThinkOfANumber program1July 151 10 ThinkOfANumber program CE00858-1: Fundamental Programming Techniques.

## Presentation on theme: "10 ThinkOfANumber program1July 151 10 ThinkOfANumber program CE00858-1: Fundamental Programming Techniques."— Presentation transcript:

10 ThinkOfANumber program1July 151 10 ThinkOfANumber program CE00858-1: Fundamental Programming Techniques

10 ThinkOfANumber program2July 152 Objectives In this session, we will: analyse a problem that uses selection and iteration implement the solution using concepts introduced so far

10 ThinkOfANumber program3July 15 ThinkOfANumber specification generate random number and get user to guess what it is randomly generate a number in range 1 – 100 prompt user to enter a guess keep prompting for a number until the user guesses correctly output a hint based on the number guessed: higher lower at the end, output the number of guesses taken to guess correctly

10 ThinkOfANumber program4July 15 Analysis what data is used? what operations are needed? what operations are done once before the loop? how many times is loop repeated? what operations are repeated inside the loop? what operations are done once after the loop? some of these operations may need further analysis

10 ThinkOfANumber program5July 15 //think of a number and allow the user to guess it import java.util.*; public class ThinkNumber { public static void main (String args[]) { //generate number in range 1 - 100 //prompt user to enter guess //loop until correct number is guessed //check guess and output hint //guess is correct - output count }

10 ThinkOfANumber program6July 15 Code snippets generating value in range 1 – 100: int number = (int)(Math.random() * 100) + 1; looping until correct value input while (guess != number)

10 ThinkOfANumber program7July 15 Testing need to modify code to be able to test add an output statement so that we know what number has been randomly generated must guess correct number first so that loop is not entered must guess higher than number so that "lower" is output must guess lower than number so that "higher" is output

10 ThinkOfANumber program8July 158 Summary In this session we have: analysing a problem to determine what is required implementing a solution that: generates random numbers uses a while loop to handle a non-deterministic situation has a choice within the loop performs initial operations before the loop outputs results after the loop In the next session we will: introduce methods to perform blocks of code

Download ppt "10 ThinkOfANumber program1July 151 10 ThinkOfANumber program CE00858-1: Fundamental Programming Techniques."

Similar presentations