Java working with numbers java an eventful approach ch 3
This presentation is the property of its rightful owner.
Sponsored Links
1 / 18

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


  • 112 Views
  • Uploaded on
  • Presentation posted in: General

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 ) {

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Java working with numbers java an eventful approach ch 3

CS 120 Lecture 13

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

23 October 2012

Slides Credit: Bruce, Danyluk and Murtagh


Recall accessing location info

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 );

}


Being unpredictable

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?


The highs and lows

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() );

    }


Accessor methods

Accessor Methods

  • Methods that return information about an object

  • Examples:

    mousePos.getY();

    canvas.getWidth();


Expressions vs statements

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


Fun with numbers

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?


Order of arithmetic operations

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)


  • Instance variables

    Instance Variables

    • We can give names to numeric values as we can name objects

      private int brightness; // shades of gray

      brightness = 50;


    Using variables

    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…


    Using constants

    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


    Displaying numeric information

    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!


    System out println

    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


    Playing dice with the universe

    Playing Dice with the Universe

    • How can we simulate a roll of dice?

    • Through random numbers!


    Random numbers

    Random Numbers

    RandomIntGenerator die;

    die = new RandomIntGenerator( 1, 6 );

    //display a random integer between 1 and 6

    System.out.println( die.nextValue() );


    Java working with numbers java an eventful approach ch 3

    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 ) );

    }

    }


    Review

    Review

    • Numbers

    • Expressions and statements

    • Variables and Constants

    • System.out.println(…)

    • Generating random numbers


    Student to do s

    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?


  • Login