ch03 - Alice in Action with Java Chapter 3 Variables and...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Alice in Action with Java Chapter 3 Variables and Functions
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Alice in Action with Java 2 Objectives Use variables to store values for use later in a method Use a variable to store the value of an arithmetic expression Use a variable to store the value produced by a function Use parameters to write methods that are more broadly useful
Background image of page 2
Alice in Action with Java 3 Objectives (continued) Define and access property variables Use the vehicle property to synchronize the movements of two objects Create functions — messages that return a value to their sender
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Alice in Action with Java 4 Variables and Functions Variable: named container for storing/retrieving data Three types of variables Method (local): defined for use within a method Parameter: variable passed to a method Object (property): used to store an object property Tools for creating method variables and parameters Buttons in the upper-right corner of the method box Tool for creating object variables Button under the property pane of the details area
Background image of page 4
Alice in Action with Java 5 Variables and Functions (continued)
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Alice in Action with Java 6 Method Variables Defined using create new variable button Information needed to define a variable Name: refers to a location in memory Type: refers to kind of data stored; e.g., a number Initial value: starting value of specified type; e.g., 1 Method variables are local (valid only in the method) Common uses of method variables Computing and storing values for later retrieval Storing values entered by a user
Background image of page 6
Alice in Action with Java 7 Example 1: Storing a Computed Value User story objects: nativeGirl and horse Objective: move nativeGirl closer to horse Strategy for moving the girl the right distance Define variable storing distance from the girl to horse Ask the girl how far she is from the horse Store the girl’s reply in the variable Use variable’s current value in a move() message Preparing to program with user-defined variables Position nativeGirl and horse in a new world Review types: Number , Boolean , Object , Other
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Alice in Action with Java 8 Example 1: Storing a Computed Value (continued)
Background image of page 8
Alice in Action with Java 9 Example 1: Storing a Computed Value (continued) Creating the first local variable Click the create new variable button Define Number type named distanceToHorse Drag distanceToHorse to the editing area Set the variable value using a placeholder value of 1 Click on functions pane for nativeGirl object Drag distanceInFrontOf() onto placeholder Specify the entire horse as the object argument
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Alice in Action with Java 10 Example 1: Storing a Computed Value (continued)
Background image of page 10
Alice in Action with Java 11 Example 1: Storing a Computed Value (continued)
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Alice in Action with Java 12 Example 1: Storing a Computed Value
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 50

ch03 - Alice in Action with Java Chapter 3 Variables and...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online