TopIndentingNumeric types and Conditionals

Numeric types and Conditionals

Locations and Accessor Methods

Accessor methods return values representing information about the object. Examples are width, height, etc.

A mutator method tells the computer to do something. So, mutator methods can be used where the computer expects a command. We have used them in the lists of commands that form the bodies of methods.

Click on CrossHairs to show the example with uses of accessor and mutator methods.

Numbers and String Interpolation

You can declare and use instance variables and definitions that refer to numbers and you can perform operations involving numbers using operators like + and -. Click on ClickCounter to show the example. Notice the use of "string interpolation": If there is an expression in curly braces inside a string, then the expression is evaluated, converted to a string, and inserted into the surrounding string.

Click on MouseMeter to show another example.

Dragging

It turns out that a lot of games and other animations involve dragging objects around the screen: Click on SimpleDrag to show the example.

That example didn't give a good solution for dragging. Click on BetterDrag for a better solution. We'll show an even better one later once we introduce boolean variables.

Conditional statements

Conditional statements allow you to decide whether or not to execute a block of code depending on a statement that may be true or false. For example, if I wanted to emulate a (ridiculously) simple game of basketball I might want to score only when the user clicked inside the hoop.

Basketball: To see the first attempt click on BasketBall to show the example.

To make our basketball game a bit more exciting, let's add something that is clearly missing from our first version -- a ball. In particular, we would like to change the game so that to score you have to drag the ball into the basket.

Click on BetterBasketBall to show the example.


TopIndentingNumeric types and Conditionals