Unformatted text preview:

Alice in Action with JavaObjectivesObjectives (continued)Variables and FunctionsPowerPoint PresentationMethod VariablesExample 1: Storing a Computed ValueSlide 8Example 1: Storing a Computed Value (continued)Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Example 2: Storing a User-Entered ValueSlide 17Slide 18Example 2: Storing a User-Entered Value (continued)Slide 20Slide 21ParametersExample 1: Old MacDonald Had a FarmSlide 24Slide 25Slide 26Example 2: Jumping Fish!Slide 28Slide 29Slide 30Property VariablesSlide 32Property Variables (continued)Slide 34Slide 35Alice Tip: Using the Vehicle PropertySlide 37Slide 38FunctionsExample: Retrieving an Attribute from an ObjectSlide 41Example: Retrieving an Attribute from an Object (continued)Slide 43Slide 44Slide 45Functions with ParametersSlide 47Slide 48SummarySummary (continued)Alice in Action with JavaChapter 3Variables and FunctionsAlice in Action with Java 2Objectives•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 usefulAlice in Action with Java 3Objectives (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 senderAlice in Action with Java 4Variables 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 areaAlice in Action with Java 5Variables and Functions (continued)Alice in Action with Java 6Method 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 userAlice in Action with Java 7Example 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, OtherAlice in Action with Java 8Example 1: Storing a Computed Value (continued)Alice in Action with Java 9Example 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 argumentAlice in Action with Java 10Example 1: Storing a Computed Value (continued)Alice in Action with Java 11Example 1: Storing a Computed Value (continued)Alice in Action with Java 12Example 1: Storing a Computed Value (continued) •Using distanceToHorse in the move() method–Drag the move()message into the editing area–Select Forward->expressions->distanceToHorse•A test of method shows the girl too close to horse•Adjusting the distance that the girl moves–Click the list arrow next to distanceToHorse–Select math->distanceToHorse-->.5 •Use functions and variables over trial-and-error–Behavior will adapt to repositioning of objectsAlice in Action with Java 13Example 1: Storing a Computed Value (continued)Alice in Action with Java 14Example 1: Storing a Computed Value (continued)Alice in Action with Java 15Example 1: Storing a Computed Value (continued)Alice in Action with Java 16Example 2: Storing a User-Entered Value•User story built around the Pythagorean Theorem–skaterGirl requests the lengths of two sides–User passes edge1 and edge2 values to skaterGirl–skaterGirl computes hypotenuse and displays value•Overview of implementation–Create skaterGirl method computeHypotenuse() –Declare three Number variables for triangle sides–Add two say()statements for interactive dialog–Add set() method for edge1–Incorporate NumberDialog into set()parameter listAlice in Action with Java 17Example 2: Storing a User-Entered Value (continued)Alice in Action with Java 18Example 2: Storing a User-Entered Value (continued)Alice in Action with Java 19Example 2: Storing a User-Entered Value (continued)•Overview of implementation (continued)–Add set()method for edge2–Incorporate NumberDialog into set()parameter list–Add set()method for hypotenuse–Drag and drop Math.sqrt()over placeholder value–Pass a mathematical expression to Math.sqrt()–Define String variable named hypotString –Convert hypotenuse to string and store in hypotString–Add say()method to display results–Display concatenates dialog and hypotString valueAlice in Action with Java 20Example 2: Storing a User-Entered Value (continued)Alice in Action with Java 21Example 2: Storing a User-Entered Value (continued)Alice in Action with Java 22Parameters•Argument: value passed to an object via a message•Parameter: variable that stores an argument•Example using the say() message–The parameter is a single variable of String type–One possible argument is “Oh, hello there!” •You may pass multiple arguments to messages–Example: roll() message requires two argumentsAlice in Action with Java 23Example 1: Old MacDonald Had a Farm•Objective: display song lyrics for four animals•Use of parameters helps avoid repetitious coding•Two parameters needed: animal and noise •Overview of implementation –Create new world with scarecrow before fence–Create singVerse()method for scarecrow–Add two parameters, animal and noise –Add String types, firstLine and doubleNoise–Incorporate parameters and


View Full Document

BOISE STATE CS 120 - Chapter 3

Documents in this Course
Load more
Download Chapter 3
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 3 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 3 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?