Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

SimpleGameInAlice1 Barb Ericson Georgia Institute of Technology June 2008 Creating a Simple Game in Alice.
Standard Algorithms Find the highest number. ! Your name and today’s date ! Find the maximum Dim numbers(20) As Integer.
Building Java Programs
Georgia Institute of Technology Speed part 5 Barb Ericson Georgia Institute of Technology May 2006.
Session 3 Algorithm. Algorithm Algorithm is a set of steps that are performed to solve a problem. The example below describes an algorithm Example Check.
While Loops. Challenge: ● Ask the user a simple math questions ● Continue asking the question until the user gets it right.
Building Java Programs Chapter 5
12 Pontoon1May Pontoon program CE : Fundamental Programming Techniques.
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.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
10 ThinkOfANumber program1July ThinkOfANumber program CE : Fundamental Programming Techniques.
JAVA Control Structures: Repetition. Objectives Be able to use a loop to implement a repetitive algorithm Practice, Practice, Practice... Reinforce the.
Intro-Sound-part21 Introduction to Processing Digital Sounds part 2 Barb Ericson Georgia Institute of Technology Oct 2009.
Lecture 15 Practice & exploration Subfunctions: Functions within functions “Guessing Game”: A hands-on exercise in evolutionary design © 2007 Daniel Valentine.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
CPS 2231 Computer Organization and Programming Instructor: Tian (Tina) Tian.
A revision guide for programming with python. 1.A program is a set of instructions that tells a computer what to do. 2.The role of a programmer is to.
Georgia Institute of Technology Speed part 3 Barb Ericson Georgia Institute of Technology May 2006.
Applications in Java Towson University *Ref:
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.
CreatingClasses-part11 Creating Classes part 1 Barb Ericson Georgia Institute of Technology Dec 2009.
CSC 204 Programming I Loop I The while statement.
Passing Other Objects Strings are called immutable which means that once a String object stores a value, it never changes –recall when we passed a message.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 1 Chapter 8 Testing and Debugging.
Georgia Institute of Technology Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
DiceRoller DiceRoller (object class) and DiceRollerViewer client class: Write a DiceRoller class (similar to Yahtzee) to: Allow the person to initially.
CS1101X: Programming Methodology Recitation 3 Control Structures.
Georgia Institute of Technology Movies part 2 Barb Ericson Georgia Institute of Technology April 2006.
CSE 201 – Elementary Computer Programming 1 Extra Exercises Source: Suggested but not selected midterm questions.
Implementation of the Hangman Game in C++
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.
Georgia Institute of Technology Pictures and Alice Barb Ericson Georgia Institute of Technology September 2006.
Georgia Institute of Technology Manipulating Pictures, Arrays, and Loops Barb Ericson Georgia Institute of Technology August 2005.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
Discussion 4. Labs public MyPoint(double xInit, double yInit ) { MyPoint p = new MyPoint(0, 0); } ClassProblem.java recursive java.lang.StackOverflowError.
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Georgia Institute of Technology Creating Classes part 4 Barb Ericson Georgia Institute of Technology May 2006.
CreatingClasses-SlideShow-part41 Creating Classes part 4 Barb Ericson Georgia Institute of Technology Dec 2009.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Georgia Institute of Technology Creating Classes part 2 Barb Ericson Georgia Institute of Technology June 2006.
CSE 110 Review Session Hans Hovanitz, Kate Kincade, and Ian Nall.
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.
Georgia Institute of Technology More on Creating Classes part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 3 Friday 03 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
Intro-Sound-Mod10-part31 Introduction to Processing Digital Sounds part 3 while loop, tracing, for loop, parameters Barb Ericson Georgia Institute of Technology.
AP Java Java’s version of Repeat until.
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
TOPIC 8 MORE ON WHILE LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by M. Guzdial and B. Ericson,
 2001 Prentice Hall, Inc. All rights reserved. 1 Chapter 16 - WMLScript Functions Outline 16.1 Introduction 16.2 Program Modules in WMLScript 16.3 Programmer-Defined.
Georgia Institute of Technology More on Creating Classes Barb Ericson Georgia Institute of Technology June 2006.
Algorithm Definition An algorithm is a step-by-step solution to a problem.
Algorithm Definition An algorithm is a step-by-step solution to a problem.
CSC111 Quick Revision.
What’s cheating and what is not?
Truth tables: Ways to organize results of Boolean expressions.
Truth tables: Ways to organize results of Boolean expressions.
How can you make a guessing game?
More on Creating Classes
Creating and Modifying Text part 3
Barb Ericson Georgia Institute of Technology Oct 2005
Data Types Every variable has a given data type. The most common data types are: String - Text made up of numbers, letters and characters. Integer - Whole.
Selection Sort Insertion Sort if time
Presentation transcript:

Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005

Georgia Institute of Technology Learning Goals Computing concepts –Pulling out a method That is called by the constructors –Getting input Using SimpleInput class from Georgia Tech –Showing output Using SimpleOutput class from Georgia Tech –Generating random sentences Using arrays of nouns, verbs, and phrases

Georgia Institute of Technology Pull out a Method Both Constructors need to pick a random number using the minimum and maximum We can pull out this common code and make a method for it public void pickNumber() And then call the method in each constructor

Georgia Institute of Technology Pick a Number Method public NumberGuessGame(int min, int max) { this.minNumber = min; this.maxNumber = max; this.pickNumber(); } public void pickNumber() { int numValues = this.maxNumber - this.minNumber + 1; this.pickedNumber = this.randomNumGen.nextInt(numValues); this.pickedNumber = this.pickedNumber + this.minNumber; }

Georgia Institute of Technology Need a Method to Play the Game Set a variable to not done Loop while not done –Get the current guess –Increment the number of guesses –Check if the guess was right If so tell the user the guess was right and how many guesses it took Set a variable (done) to stop the loop –Check if the guess was low Tell the user –Else the guess was too high Tell the user

Georgia Institute of Technology Need a Way to Interact with User Use the SimpleInput class for input –Created by Georgia Tech –Has a method getIntNumber(String message) That will display the message in a pop-up window and return an integer number entered by the user Use the SimpleOutput class for output –Created by Georgia Tech –Has a method showInformation(String message) which will display the output in a pop-up window And wait for the user to push a button to show it has been read

Georgia Institute of Technology Going from Algorithm to Code Set a variable to not done Loop while not done –Get the current guess –Increment the number of guesses –Check if the guess was right If so tell the user the guess was right and how many guesses it took Set a variable (done) to stop the loop –Else check if the guess was low Tell the user –Else the guess was too high Tell the user Use boolean done variable and set it to false Use a while loop that loops as long as the done isn’t true while (!done) Using SimpleInput And a variable guess numGuesses++; if (guess == pickedNumber) Use SimpleOutput and numGuesses Change done to true else if (guess < pickedNumber) else

Georgia Institute of Technology Add a method to play the game public void playGame() { boolean done = false; // loop till guess is correct while (!done) { // need to get a guess from the user int guess = SimpleInput.getIntNumber("Pick a number "+ "between " + this.minNumber + " and " + this.maxNumber); // increment the number of guesses this.numGuesses++; // we need to check the guess (compare to pickedNum) if (guess == this.pickedNumber) {

Georgia Institute of Technology Play // tell the user she/he was right SimpleOutput.showInformation("That was right! " + "It took you " + this.numGuesses + " tries"); done = true; } else if (guess < this.pickedNumber) { // we need to tell the user too low SimpleOutput.showInformation("Too low"); } else { // tell the user the guess is too high SimpleOutput.showInformation("Too high"); }

Georgia Institute of Technology Add a main method public static void main(String[] args) { NumberGuessGame game = new NumberGuessGame(); game.playGame(); }

Georgia Institute of Technology Random Sentence Generator Exercise Write a class that can generate random sentences. –Create a class SentenceGenerator That has an array of nouns An array of verbs And an array of phrases And a method generate sentence which will return a String object that has a randomly selected noun, verb, and phrase appended together

Georgia Institute of Technology Summary If more than one constructor needs to do the same thing –Pull out the common thing and put it in a method –And call the method in the constructors You can get input from the user –Using SimpleInput You can display output to the user –Using SimpleOutput You can use java.util.Random –To create random sentences