1 / 18

Java Working with Numbers (Java: An Eventful Approach, Ch 3),

CS 120 Lecture 13. Java Working with Numbers (Java: An Eventful Approach, Ch 3),. 23 October 2012. Slides Credit: Bruce, Danyluk and Murtagh. Recall: Accessing Location Info. private Location firstPoint; public void onMousePress( Location pressPt ) {

gema
Download Presentation

Java Working with Numbers (Java: An Eventful Approach, Ch 3),

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 120 Lecture 13 JavaWorking with Numbers(Java: An Eventful Approach, Ch 3), 23 October 2012 Slides Credit: Bruce, Danyluk and Murtagh

  2. Recall: Accessing Location Info private Location firstPoint; public void onMousePress( Location pressPt ) { new Text("Pressed", pressPt, canvas ); firstPoint = press; } public void onMouseRelease ( Location releasePt) { new Text("Released", releasePt, canvas ); new Line( firstPoint, releasePt, canvas ); }

  3. Being Unpredictable • Formal parameters: • allow us to refer to information that is unknown when the program is written • Generalize our methods • Are there other ways we access and use “unknown” information?

  4. The Highs and Lows • What if we want to move an object only vertically by dragging the mouse? Pong: public void onMouseDrag( Location mousePos ) { paddle.moveTo( 10, mousePos.getY() ); }

  5. Accessor Methods • Methods that return information about an object • Examples: mousePos.getY(); canvas.getWidth();

  6. Expressions vs Statements • Statements: • “Top of the morning to you.” • paddle.moveTo(10,mousePos.getY() ); Perform a useful action • Expressions: • “the morning” • mousePos.getY() Used in statements

  7. Fun with Numbers • initialPosition.getX() and -5 are both numbers • Can add, subtract, divide, multiply numbers • initialPosition.getX() + 5 • 5 - initialPosition.getX() • initialPosition.getX() / 5 • initialPosition.getY() * 3 • What happens with 9/5?

  8. Order of Arithmetic Operations 2 + (initialPosition.getX() – initialPosition.getY()) * 5 • Precedence Rules: • Perform operations within parentheses first • Divisions and multiplications before additions and subtractions • Operations of equal precedence performed left to right • PE(MD)(AS)

  9. Instance Variables • We can give names to numeric values as we can name objects private int brightness; // shades of gray brightness = 50;

  10. Using Variables • First declare: private int brightness; private Color purple; • Then initialize brightness = 50; purple = new Color( 255, 0, 255 ); • We can also reassign values to variables brightness = 34; Unless…

  11. Using Constants private static final int BRIGHTNESS = 255; • The keyword final indicates we cannot reassign a new value to brightness • The Java convention for naming constants is all caps

  12. Displaying Numeric Information • We can combine numbers and text for display Text countDisplay; int programNum = 3; countDisplay = new Text("This is program #" + programNum, …); With text information the + operation does string concatenation!

  13. System.out.println • Two ways to display textual information • Text class • System.out.println(…); • System.out.println("Number " + 5 ); • prints Number 5 to the Java Console

  14. Playing Dice with the Universe • How can we simulate a roll of dice? • Through random numbers!

  15. Random Numbers RandomIntGenerator die; die = new RandomIntGenerator( 1, 6 ); //display a random integer between 1 and 6 System.out.println( die.nextValue() );

  16. public class RollAnotherOne extends WindowController { private static final int NUM_SIDES = 6; private RandomIntGenerator die = new RandomIntGenerator ( 1, NUM_SIDES ); public void begin() { new Text("Click to make me roll the dice ", TEXT_X, PROMPT_Y, canvas ); } public void onMouseClick (Location point ){ roll1 = die.nextValue(); roll2 = die.nextValue(); System.out.println("You rolled a " + roll1 + "and a " + roll2 + " for a total of " + ( roll1 + roll2 ) ); } }

  17. Review • Numbers • Expressions and statements • Variables and Constants • System.out.println(…) • Generating random numbers

  18. Student To Do’s • Read Java: An Eventful Approach • Ch. 3 (Today) • Ch. 4-5 • Practice, practice…. practice! • Work through examples by coding them! • Would you compete in a sport without practicing? • Would you perform on stage without practicing?

More Related