1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals.

Slides:



Advertisements
Similar presentations
1 CSC 221: Computer Programming I Fall 2006 interacting objects modular design: dot races constants, static fields cascading if-else, logical operators.
Advertisements

Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
Games and Simulations O-O Programming in Java The Walker School
Chapter 10 THINKING IN OBJECTS 1 Object Oriented programming Instructor: Dr. Essam H. Houssein.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Road Map Introduction to object oriented programming. Classes
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Understanding class definitions Looking inside classes.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
Games and Simulations O-O Programming in Java The Walker School
1 CSC 222: Object-Oriented Programming Spring 2012 Simulations & library classes  HW3: RouletteWheel, RouletteGame, RouletteTester  javadoc  java.lang.
Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9.
Chapter 7 Designing Classes. Class Design When we are developing a piece of software, we want to design the software We don’t want to just sit down and.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
METHODS Introduction to Systems Programming - COMP 1005, 1405 Instructor : Behnam Hajian
4.1 Instance Variables, Constructors, and Methods.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
1 CSC 222: Object-Oriented Programming Spring 2012 Object-oriented design  example: word frequencies w/ parallel lists  exception handling  System.out.format.
Sahar Mosleh California State University San MarcosPage 1 A for loop can contain multiple initialization actions separated with commas Caution must be.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Java is an object oriented programming language In this model of programming all entities are objects that have methods and fields Methods perform tasks.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
COP-3330: Object Oriented Programming Flow Control May 16, 2012 Eng. Hector M Lugo-Cordero, MS.
1 CSC 221: Computer Programming I Spring 2010 repetition & simulations  conditional repetition, while loops  examples: paper folding, sequence generation,
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Class Miscellanea Details About Classes. Review We’ve seen that a class has two sections: class Temperature { public: //... public members private: //...
1 CSC 221: Computer Programming I Fall 2009 interaction & repetition  modular design: dot races  constants, static fields  conditional repetition, while.
Chapter 3 Object Interaction.  To construct interesting applications it is not enough to build individual objects  Objects must be combined so they.
Java Programming Fifth Edition Chapter 5 Making Decisions.
Chapter 5: Making Decisions. Objectives Plan decision-making logic Make decisions with the if and if…else structures Use multiple statements in if and.
Arithmetic, Class Variables and Class Methods Week 11
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
Week 12 - Wednesday.  What did we talk about last time?  Hunters and prey.
Week 12 - Monday.  What did we talk about last time?  Defining classes  Class practice  Lab 11.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
Chapter 9: Continuing Classes By Matt Hirsch. Table Of Contents 1.Static Fields and Methods 2.Inheritance I. Recycle Code with Inheritance II. Overriding.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Understanding class definitions Exploring source code 6.0.
CONDITIONALS CITS1001. Scope of this lecture if statements switch statements Source ppts: Objects First with Java - A Practical Introduction using BlueJ,
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
ENCAPSULATION. WHY ENCAPSULATE? So far, the objects we have designed have all of their methods and variables visible to any part of the program that has.
1 CSC 221: Computer Programming I Fall 2005 graphics & design  Dot & DotRace revisited  Circle class implementation  adding Circle methods  static.
Java Programming Fifth Edition
Methods Chapter 6.
CSC 222: Object-Oriented Programming Spring 2013
CSC 222: Object-Oriented Programming Spring 2015
CSC 222: Object-Oriented Programming Fall 2017
Group Status Project Status.
CMSC 202 Java Primer 2.
CSC 222: Object-Oriented Programming Spring 2013
Classes, Objects and Methods
CSC 222: Object-Oriented Programming Spring 2012
Presentation transcript:

1 CSC 221: Computer Programming I Spring 2010 interaction & design  modular design: roulette game  constants, static fields  % operator, string equals  variable scope  logical operators

2 Roulette games consider the task of simulating a roulette game  need to be able to spin a wheel  make bets, either by number or even/odd  keep track of winnings/losses (most likely losses!) behaviors?  create a game  initialize the player's bankroll  for each spin of the wheel  get the player's bet  determine if winner and update bankroll accordingly  make sure the player has credits in order to play? we could try modeling a game by implementing a class directly  must model the wheel behavior, also manage the bankroll and bets lots of details to keep track of; not easy to generalize

3 A modular design instead, we can encapsulate all of the behavior of a roulette wheel in a class RouletteWheel class:  create a RouletteWheel  spin the wheel, return number  keep track of the number of spins so far once the RouletteWheel class is defined, a RouletteGame will be much simpler RouletteGame class:  create a RouletteGame (with its own RouletteWheel )  add credits to bankroll  check the bankroll balance  take a number bet & determine if a winner  take an even/odd bet & determine if a winner

4 RouletteWheel class public class RouletteWheel { private Die roller; private int numSpins; public RouletteWheel() { this.roller = new Die(38); this.numSpins = 0; } public int spin() { int number = this.roller.roll(); this.numSpins++; if (number > 36) { return 0; } else { return number; } public int getNumberOfSpins() { return this.numSpins; } more naturally:  fields store a Die (for generating random numbers) and a counter for the number of spins  constructor creates the Die object and initializes the count  methods access and update these fields to maintain the wheel's state CREATE AND PLAY

5 Magic numbers the RouletteWheel class works, but what if we wanted to change to a European-style wheel?  only 1-36 and 0 (no 00)  would have to change die = new Die(38); to die = new Die(37);  having "magic numbers" like 38 in code is bad practice unclear what 38 refers to when reading the code requires searching for the number when a change is desired public class RouletteWheel { private Die roller; private int numSpins; public RouletteWheel() { this.roller = new Die(38); this.numSpins = 0; } public int spin() { int number = this.roller.roll(); this.numSpins++; if (number > 36) { return 0; } else { return number; } public int getNumberOfSpins() { return this.numSpins; }

6 Constants better solution: define a constant  a constant is a variable whose value cannot change  use a constant any time a "magic number" appears in code a constant declaration looks like any other field except  the keyword final specifies that the variable, once assigned a value, is unchangeable  the keyword static specifies that the variable is shared by all objects of that class since a final value cannot be changed, it is wasteful to have every object store a copy of it instead, can have one static variable that is shared by all by convention, constants are written in all upper-case with underscores public class RouletteWheel { private final static int MAX_NUM = 36; private final static int NUM_ZEROS = 2; private Die roller; private int numSpins; public RouletteWheel() { this.roller = new Die(RouletteWheel.MAX_NUM + RouletteWheel.NUM_ZEROS); this.numSpins = 0; } public int spin() { int number = this.roller.roll(); this.numSpins++; if (number > RouletteWheel.MAX_NUM) { return 0; } else { return number; } public int getNumberOfSpins() { return this.numSpins; }

7 Static fields in fact, it is sometimes useful to have static fields that aren't constants  if all roulette wheels have the same range, there is no reason for every wheel to have its own Die  we could declare the Die field to be static, so that the one Die is shared by all wheels  the first RouletteWheel object creates/inits the field, subsequent objects simply access it note: methods can be declared static as well  e.g., Math.random() MORE LATER public class RouletteWheel { private final static int MAX_NUM = 36; private final static int NUM_ZEROS = 2; private static Die roller = new Die(RouletteWheel.MAX_NUM + RouletteWheel.NUM_ZEROS); private int numSpins; public RouletteWheel() { this.numSpins = 0; } public int spin() { int number = RouletteWheel.roller.roll(); this.numSpins++; if (number > RouletteWheel.MAX_NUM) { return 0; } else { return number; } public int getNumberOfSpins() { return this.numSpins; }

using the RouletteWheel class, a RouletteGame class is straightforward  fields store roulette wheel and bankroll  constructor creates the RouletteWheel and inits the bankroll  methods utilize the RouletteWheel methods to produce the game behaviors 8 RouletteGame class public class RouletteGame { private RouletteWheel wheel; private int credits; public RouletteGame() { this.wheel = new RouletteWheel(); this.credits = 0; } public void addCredits(int numCredits) { this.credits += numCredits; } public int checkCredits() { return this.credits; } public String makeBet(int betAmount, int number) { int result = this.wheel.spin(); if (result == number) { this.credits += 35*betAmount; return result + " - winner"; } else { this.credits -= betAmount; return result + " - loser"; } public String makeBet(int betAmount, String betType) { int result = this.wheel.spin(); String resultType = "odd"; if (result % 2 == 0) { resultType = "even"; } if (betType.equals(resultType)) { this.credits += betAmount; return result + " - winner"; } else { this.credits -= betAmount; return result + " - loser"; }

the % operator returns the remainder after dividing one number by another  useful for determining odd/even (result % 2) will evaluate to 0 if result is even, 1 if odd 9 RouletteGame details: % and equals public class RouletteGame { private RouletteWheel wheel; private int credits;. public String makeBet(int betAmount, String betType) { int result = this.wheel.spin(); String resultType = "odd"; if (result % 2 == 0) { resultType = "even"; } if (betType.equals(resultType)) { this.credits += betAmount; return result + " - winner"; } else { this.credits -= betAmount; return result + " - loser"; } when comparing two String values, == does not always work as expected (explanation later)  instead, use the equals method defined for Strings str1.equals(str2) will return true if str1 & str2 are identical, else false

consider the segment: String resultType = "odd"; if (result % 2 == 0) { resultType = "even"; } 10 RouletteGame details: scope why not? if (result % 2 == 0) { String resultType = "even"; } else { String resultType = "odd"; } key idea: on large software projects, need to be able to develop independent modules independently  in the real world, a software project might get divided into several parts  each part is designed & coded by a different team, then integrated together  internal naming conflicts should not be a problem e.g., when declaring a local variable in a method, the programmer should not have to worry about whether that name is used elsewhere every variable has a scope (i.e., where in the program it exists and can be accessed)  can reuse the same local variable/parameter name in different methods since they exist in different scopes

11 RouletteGame details: scope  the scope of a field is the entire class (i.e., all methods can access it)  the scope of a parameter is its entire method  the scope of a local variable is from its declaration to the end of its enclosing block {…} if (result % 2 == 0) { String resultType = "even"; } else { String resultType = "odd"; } since the variable is declared inside { }, it will not exist after the if-else String resultType; if (result % 2 == 0) { resultType = "even"; } else { resultType = "odd"; } this would work – since declared outside the if-else, the variable exists & can be accessed afterwards String resultType = "odd"; if (result % 2 == 0) { resultType = "even"; } if must declare outside the if-else, might as well give it a default value then change only if needed

does makeBet work correctly in all cases?  what if the spin is 0? 12 RouletteGame fix? public class RouletteGame { private RouletteWheel wheel; private int credits;. public String makeBet(int betAmount, String betType) { int result = this.wheel.spin(); String resultType = "odd"; if (result % 2 == 0) { resultType = "even"; } if (betType.equals(resultType)) { this.credits += betAmount; return result + " - winner"; } else { this.credits -= betAmount; return result + " - loser"; } 0 should automatically lose  one possible revision if (result == 0) { this.credits -= betAmount; return result + " – loser"; } else if (betType.equals(resultType)) { this.credits += betAmount; return result + " - winner"; } else { this.credits -= betAmount; return result + " - loser"; } these two cases have identical code – would like to combine and avoid duplication

13 RouletteGame fix: logical operators public String makeBet(int betAmount, String betType) { int result = this.wheel.spin(); String resultType = "odd"; if (result % 2 == 0) { resultType = "even"; } if (result > 0 && betType.equals(resultType)) { this.credits += betAmount; return result + " - winner"; } else { this.credits -= betAmount; return result + " - loser"; } Java provides logical operators for simplifying such cases (TEST1 && TEST2) evaluates to true if either TEST1 AND TEST2 is true (TEST1 || TEST2) evaluates to true if either TEST1 OR TEST2 is true (!TEST) evaluates to true if TEST is NOT true warning: the tests that appear on both sides of || and && must be complete Boolean expressions (x == 2 || x == 12) OK (x == 2 || 12) BAD!

RouletteGame: error checking public void addCredits(int numCredits) { if (numCredits > 0) { this.credits += numCredits; } public String makeBet(int betAmount, int number) { if (betAmount > 0 && this.credits >= betAmount && number >= 1 && number <= 36) { // SAME AS BEFORE else { return "ILLEGAL BET"; } public String makeBet(int betAmount, String betType) { if (betAmount > 0 && this.credits >= betAmount && (betType.equals("odd") || betType.equals("even"))) { // SAME AS BEFORE } else { return "ILLEGAL BET"; } 14

RouletteWheel: adding color in addition to odd/even and a specific number, can also bet on a color red: 1, 3, 5, 7, 9, black: 2, 4, 6, 8, 10, 12, 14, 16, 1811, 13, 15, 17, 19, 21, 23, 25, 27,20, 22, 24, 26, 28, 30, 32, 34, 3629, 31, 33, 35 green: 0, 00 how do we alter the RouletteWheel so that we can determine color? how do we alter RouletteGame to allow bets on "red" or "black"? 15