Discussion 4. Labs public MyPoint(double xInit, double yInit ) { MyPoint p = new MyPoint(0, 0); } ClassProblem.java recursive java.lang.StackOverflowError.

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6 Repetition Statements.
Advertisements

Writing Pseudocode And Making a Flow Chart A Number Guessing Game
Building Java Programs
Logic & program control part 3: Compound selection structures.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1.
3-2 What are relational operators and logical values? How to use the input and disp functions. Learn to use if, if-else and else-if conditional statements.
Overview Reference parameters Documenting functions A game of craps. Design, code, test and document.
12 Pontoon1May Pontoon program CE : Fundamental Programming Techniques.
Discussion 3. Questions? Term test? Lectures? Labs?
BACS 287 Programming Logic 3. BACS 287 Iteration Constructs Iteration constructs are used when you want to execute a segment of code several times. In.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 6 Repetition Statements Animated Version.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Sample Development: HiLo Game *Chapter 6: Console.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
بسم الله الرحمن الرحيم CPCS203: Programming II. Objectives After you have read and studied this chapter, you should be able to Implement repetition control.
Computer Science 1620 Programming & Problem Solving.
Program design example Task: Develop an algorithm expressed in pseudocode for a specified problem specified problem.
Chapter Chapter 6 Repetition Statements. Objectives Understand repetition control (loop ) statements in Java: while statement. do-while statement.
Chapter Chapter 5 Selection Statements. Objectives Understand selection control statement –if statements –switch statements Write boolean expressions.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Selection Statements.
10 ThinkOfANumber program1July ThinkOfANumber program CE : Fundamental Programming Techniques.
Gentle Introduction to Programming in Java Dr. Jey Veerasamy 1.
Lecture 15 Practice & exploration Subfunctions: Functions within functions “Guessing Game”: A hands-on exercise in evolutionary design © 2007 Daniel Valentine.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Lecture 4 Loops.
Chapter 6 – Repetition Statements : Objectives After you have read and studied this chapter, you should be able to Implement repetition control in a program.
1 Copyright (C) 2008 by Dennis A. Fairclough all rights reserved.
MOM! Phineas and Ferb are … Aims:
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
While Loops Indefinite Iteration. Last lesson we looked at definite loops using the ‘For’ statement. The while loop keeps going while some condition is.
DiceRoller DiceRoller (object class) and DiceRollerViewer client class: Write a DiceRoller class (similar to Yahtzee) to: Allow the person to initially.
Chapter 4 Loops Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
CS1101X: Programming Methodology Recitation 3 Control Structures.
CS1101X: Programming Methodology Recitation 1 Java Basics Numerical Data.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 5: Software Design & Testing; Revision Session.
Discussion 5. Lab 3 Not really using rectangle class Return type of getPoint is String instead of Point You are not able to retrieve the point if you.
Conditions. Objectives  Understanding what altering the flow of control does on programs and being able to apply thee to design code  Look at why indentation.
Implementation of the Hangman Game in C++
Making Decisions uCode: October Review What are the differences between: o BlueJ o Java Computer objects represent some thing or idea in the real.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Structured Programming: Debugging and Practice by the end of this class you should be able to: debug a program using echo printing debug a program using.
CS161 Topic #16 1 Today in CS161 Lecture #16 Prepare for the Final Reviewing all Topics this term Variables If Statements Loops (do while, while, for)
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
1 ENGI 2420 Structured Programming (Lab Tutorial 7) Memorial University of Newfoundland.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Designing While Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Programming Fundamentals I Java Programming Spring 2009 Instructor: Xuan Tung Hoang TA: Tran Minh Trung Lab 03.
Iteration. Iteration: Review  If you wanted to display all the numbers from 1 to 1000, you wouldn’t want to do this, would you? Start display 1 display.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Error Handling Tonga Institute of Higher Education.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
1 ENGI 2420 Structured Programming (Lab Tutorial 7) Memorial University of Newfoundland.
Special Methods in Java. Mathematical methods The Math library is extensive, has many methods that you can call to aid you in your programming. Math.pow(double.
1 Project 7: Looping. Project 7 For this project you will produce two Java programs. The requirements for each program will be described separately on.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
1 Project 5: Leap Years. 222 Leap Years Write a program that reads an integer value from the user representing a year and determines if the year is a.
Feedback  Lab2, Hw1  Groups  Group Project Requirements.
General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change.
Loops, Part II IT108 George Mason University. Indefinite Loop Don’t always have access to the number of iterations ahead of time If a condition (user-response,
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Introduction to OOP with Java 4th Ed, C. Thomas Wu
Intro to OOP with Java, C. Thomas Wu
Building Java Programs
Writing Functions( ) (Part 4)
Functions continued.
CSE 1020:Software Development
CHAPTER 6 Testing and Debugging.
Presentation transcript:

Discussion 4

Labs public MyPoint(double xInit, double yInit ) { MyPoint p = new MyPoint(0, 0); } ClassProblem.java recursive java.lang.StackOverflowError

Labs Indentation?

Term Test

6.12 Sample Development

Problem Statement Write an application that will play Hi-Lo games with the user. The objective of the game is for the user to guess the computer-generated secret number in the least number of tries. The secret number is an integer between 1 and 100, inclusive. When the user makes a guess, the program replies with HI or LO depending on whether the guess is higher or lower than the secret number. The maximum number of tries allowed for each game is six. The user can play as many games as she wants.

Overall Plan Tasks: do { Task 1: generate a secret number; Task 2: play one game; } while ( the user wants to play );

Required Classes Ch6HiLo main class JOptionPane Math standard classes

Development Steps We will develop this program in four steps: 1. Start with a skeleton Ch6HiLo class. 2. Add code to the Ch6HiLo class to play a game using a dummy secret number. 3. Add code to the Ch6HiLo class to generate a random number. 4. Finalize the code by tying up loose ends.

Step 1 Design The topmost control logic of HiLo 1. describe the game rules; 2. prompt the user to play a game or not; while ( answer is yes ) { 3. generate the secret number; 4. play one game; 5. prompt the user to play another game or not; }

Step 1 Code Directory: Chapter6/Step1 Source Files: Ch6HiLo.java Directory: Chapter6/Step1 Source Files: Ch6HiLo.java Program source file is too big to list here. From now on, we ask you to view the source files using your Java IDE.

Step 1 Test In the testing phase, we run the program and verify confirm that the topmost control loop terminates correctly under different conditions. Play the game zero times one time one or more times

Step 2 Design Implement the playGame method that plays one game of HiLo. Use a dummy secret number By using a fix number such as 45 as a dummy secret number, we will be able to test the correctness of the playGame method

The Logic of playGame int guessCount = 0; do { get next guess; guessCount++; if (guess < secretNumber) { print the hint LO; } else if (guess > secretNumber) { print the hint HI; } } while (guessCount < number of guesses allowed && guess != secretNumber ); if (guess == secretNumber) { print the winning message; } else { print the losing message; }

Step 2 Code Directory: Chapter6/Step2 Source Files: Ch6HiLo.java Directory: Chapter6/Step2 Source Files: Ch6HiLo.java

Step 2 Test We compile and run the program numerous times To test getNextGuess, enter a number less than 1 a number greater than 100 a number between 2 and 99 the number 1 and the number 100 To test playGame, enter a guess less than 45 a guess greater than six wrong guesses

Step 3 Design We complete the generateSecretNumber method. We want to generate a number between 1 and 100 inclusively. private void generateSecretNumber( ) { double X = Math.random(); secretNumber = (int) Math.floor( X * 100 ) + 1; System.out.println("Secret Number: " + secretNumber); //TEMP return secretNumber; }

Step 3 Code Directory: Chapter6/Step3 Source Files: Ch6HiLo.java Directory: Chapter6/Step3 Source Files: Ch6HiLo.java

Step 3 Test We use a separate test driver to generate 1000 secret numbers. We run the program numerous times with different input values and check the results. Try both valid and invalid input values and confirm the response is appropriate

Step 4: Finalize Program Completion Finish the describeRules method Remove all temporary statements Possible Extensions Allow the user to set her desired min and max for secret numbers Allow the user to set the number of guesses allowed Keep the score — the number of guesses made — while playing games and display the average score when the user quits the program