RANDOM NUMBER GAME PROJECT

Similar documents
AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: if Single-Selection Statement CSC 209 JAVA I. week 3- Control Statements: Part I

Introduction to Computer Science Unit 3. Programs

Supplementary Test 1

Lesson 10: Quiz #1 and Getting User Input (W03D2)

Object-Based Programming. Programming with Objects

static String usersname; public static int numberofplayers; private static double velocity, time;

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

Assignment Checklist. Prelab Activities. Lab Exercises. Labs Provided by Instructor. Postlab Activities. Section:

AP Computer Science Homework Set 5 2D Arrays

Assignment 2.4: Loops

Honors Computer Science Python Mr. Clausen Programs 4A, 4B, 4C, 4D, 4E, 4F

AP Computer Science Java Mr. Clausen Program 6A, 6B

There are several files including the start of a unit test and the method stubs in MindNumber.java. Here is a preview of what you will do:

THE IF STATEMENT. The if statement is used to check a condition: if the condition is true, we run a block

Programming Assignment #1 (Java)

Object Oriented Programming. Java-Lecture 6 - Arrays

Guessing Game with Objects

7. Arrays, More Java Looping

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Testing and Debugging

Java Outline (Upto Exam 2)

Object Oriented Programming with JAVA

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

Java Assignment 3: Loop Practice Ver 3.0 Last Updated: 12/1/2015 8:57 AM

IT115: Introduction to Java Programming 1. IT115: Introduction to Java Programming. Tic Tac Toe Application. Trina VanderLouw

4. Java language basics: Function. Minhaeng Lee

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

Loops. Eng. Mohammed Abdualal. Islamic University of Gaza. Faculty of Engineering. Computer Engineering Department

int x = 42, y = 33; //1 int z = x + y; //2 System.out.println(x + "+" + y + "=" + z); //3

private static final char[] Alphabet = "abcdefghijklmnopqrstuvwxyz".tochararray();

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

Basic Problem solving Techniques Top Down stepwise refinement If & if else.. While.. Counter controlled and sentinel controlled repetition Usage of

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

COMP 110 Programming Exercise: Simulation of the Game of Craps

Introduction to Computer Programming

CS201 - Assignment 3, Part 2 Due: Wednesday March 5, at the beginning of class

Fundamentals of Programming Data Types & Methods

Building Java Programs

Repetition, Looping. While Loop

Warm up Exercise. What are the types and values of the following expressions: * (3 + 1) 3 / / 2.0 (int)1.0 / 2

Welcome1.java // Fig. 2.1: Welcome1.java // Text-printing program.

Tic Tac Toe Game! Day 8

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

download instant at

CS141 Programming Assignment #6

Menu Driven Systems. While loops, menus and the switch statement. Mairead Meagher Dr. Siobhán Drohan. Produced by:

Object Oriented Programming. Java-Lecture 1

Midterm Examination (MTA)

Using Classes and Objects

CSCI Problem Solving, Programming and Computers Spring, 2016 Assignment 2 {Inheritance, Interfaces, Arrays}

Question 1 [20 points]

Last Class. While loops Infinite loops Loop counters Iterations

Chapter 4 The If Then Statement

Building Java Programs

AP Computer Science Unit 1. Programs

Control Statements: Part 1

Computer Programming, I. Laboratory Manual. Experiment #6. Loops

Media Computation. Lecture 16.1, December 8, 2008 Steve Harrison

2.8. Decision Making: Equality and Relational Operators

AP CS A Exam Review Answer Section

Lecture 7: Classes and Objects CS2301

Tutorial 3: Conditionals and Iteration COMP 202: Intro to Computing 1

CSC-140 Assignment 4

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

Advanced Computer Programming

4. Use a loop to print the first 25 Fibonacci numbers. Do you need to store these values in a data structure such as an array or list?

Decision Making and Loops

In this lab, you will learn more about selection statements. You will get familiar to

LAB 13: ARRAYS (ONE DIMINSION)

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

IST 297D Introduction to Application Programming Chapter 4 Problem Set. Name:

Session 8.2. Finding Winners Using Arrays

Conditional Programming

Lab 1: Silver Dollar Game 1 CSCI 2101B Fall 2018

Lab Activity Plan. John Dalbey CPE /30/2013

EECE.2160: ECE Application Programming Fall 2017

CONTENTS: Arrays Strings. COMP-202 Unit 5: Loops in Practice

Welcome! COMP s1. Programming Fundamentals

Chapter 6. Arrays. Java Actually: A Comprehensive Primer in Programming

CSC 1051 Data Structures and Algorithms I

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

To keep track of this new wrinkle we need some new variables at the Class level:

2/6/2018. ECE 220: Computer Systems & Programming. Function Signature Needed to Call Function. Signature Include Name and Types for Inputs and Outputs

To become familiar with array manipulation, searching, and sorting.

BLOCK STRUCTURE. class block main method block do-while statement block if statement block. if statement block. Block Structure Page 1

Object-Oriented Programming

CSIS-120 Final Exam Fall 2015 Name:

Introduction to Java Applications

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Arrays

Top-Down Program Development

Programming with Java

Claremont McKenna College Computer Science

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

CMPSCI 187 / Spring 2015 Hangman

public class Foo { private int var; public int Method1() { // var accessible anywhere here } public int MethodN() {

Evil Hangman Project

Lab 3 The High-Low Game

Transcription:

Random Number Game

RANDOM NUMBER GAME - Now it is time to put all your new knowledge to the test. You are going to build a random number game. - The game needs to generate a random number between 1 and 100. - The game needs to ask you to pick a number. - The game will let you know if the number is higher or lower than the number you picked. - If you get the number right, the game will tell you that you have won.

1 2 3 RANDOM NUMBER GAME PROJECT - Op en Eclipse - Create a new project called "NumberGame". - Create a new Java class called NumberGame. Make sure to check the box to add the "public static void main" method. - Add the code to import java.util.scanner for collecting user input. 4 - Add the code to import java.util.random for generating random numbers. 5

RANDOM NUMBER GAME PROJECT (CONTINUED) - Work on trying to get started on the solution before the code is revealed even if you do not know the full answer. This trial and error process will help you become a better programmer. - Add a comment on the first line of the.java file. The comment will be your first and last name. 6 7 - Press Enter

RANDOM NUMBER GAME PROJECT (CONTINUED) ALL coding will be done in the main method - Next we create an instance of the Random class and an instance of the Scanner class. 8

- To generate a random number we will use the generator.nextint class and passing it as a number. If we pass in the number 10 as an example, it will give us a random number between 0 and 9. Because of this we need to add one after we create the random number. - Since we are generating a number we will use the integer variable data type with the name randomnumber. 9 RANDOM NUMBER GAME PROJECT (CONTINUED) - Add the coding to randomly generate a number between 1 and 100.

RANDOM NUMBER GAME PROJECT (CONTINUED) - Add a variable to keep the while loop running. Lets give this variable a name of "running" and make it an integer. We will set the running variable to a default value of 1. 10

- Now we create the while loop. We want to keep this running as long as the "running" variable remains unchanged. 11 RANDOM NUMBER GAME PROJECT (CONTINUED) - Directly below the running variable, start the while loop.

- Inside the while loop, add a print statement that will print out the following text: Guess a number between 1 and 100: 12 RANDOM NUMBER GAME PROJECT (CONTINUED) 13 - Use input.nextint(); to retrieve the user's input and store it in and integer variable called guess. Add this code below the print statement. int guess = input.nextint();

- Now we will create a nested if statement similiar to the one we used in the Gradebook project. - The first part of the if statement refers to the users guess that is below the random number generated and will say: "If guess is less than randomnumber Then print a line that says "Too Low". 14 RANDOM NUMBER GAME PROJECT (CONTINUED) - Add the first part of the if statement to the while loop. (Refer back to the Gradebook porject if you do not remember how to structure an if statement)

- The second part of the if statement refers to the users guess that is above the random number generated and will say: "If guess is greater than randomnumber Then print a line that says "Too High". - This should be below the first if statement as an "else if" statement. 15 RANDOM NUMBER GAME PROJECT (CONTINUED) - Add the second part of the if statement to the while loop.

- The third part of the if statement refers to the users guess that is the random number generated and will say: "If guess is equal to randomnumber Then print a line that says "Correct! You win!. - This should be below the first two if statements as an "else if" statement also. The correct way to compare "is equal to" is to use a double equal sign in the if statement (ex: "=="). 16 17 RANDOM NUMBER GAME PROJECT (CONTINUED) - Add the second part of the if statement to the while loop. - We will also have to set the running variable to 0 so the program ends. This is put directly below the Correct! You win! print statement. running = 0;

RANDOM NUMBER GAME PROJECT (CONTINUED) - Here is the completed project. - Run the program to check that it works correctly.

RANDOM NUMBER GAME PROJECT (CONTINUED) - When you run the program it will look like this as you try to guess the randomly generated number.

- Now that you have the number game working, lets make one change and add one more item. 18 19 20 RANDOM NUMBER GAME PROJECT (CONTINUED) - First, change the random number to be from 0 to 200. - Add a counter variable that keeps track of how many guesses it takes for you to get the correct number (hint: you used a counter variable in Gradebook.java). - When you get the correct number, have the program print out how many guesses it took. Create a print statement that says It took you # guesses. (the # symbol refers to the number of guesses)

RANDOM NUMBER GAME PROJECT (CONTINUED) - After you have made the changes on slide 15, the program will look like this.

PRINTING 21 - Print the NumberGame.java file and turn in