A variable is a name that represents a storage location ... · Alice Variables: Intro A variable is...
Transcript of A variable is a name that represents a storage location ... · Alice Variables: Intro A variable is...
Alice Variables: Intro
• A variable is a name that represents a storage location for a value
– Represented by an identifier (name) using the same naming conventions asfor methods
– Corresponds to a data type - the kind of value it can represent
– Has a location in computer memory allocated to it
– The data type and amount of memory limits the range of values a variablecan store
– Stores a value of the specified data type
• Data types:
– Double: Fractional values (include decimal point)
– Integer: Whole numbers (no decimal point)
– Boolean: True/false
– String: Literal values indicated by double quotes
– NOTE: If Java not selected as language, will see slightly different typewindow
1
Alice Variables: Intro (2)
• A variable declaration specifies a variable’s name and type
– Alice expects an initial value in addition
– Alice refers to the combination of declaration and initialization as variabledefinition
• A variable’s scope refers to where a variable’s value can be accessed
• There are three types of variables based on where they are declared:
1. Local variables
– These are declared within a method
– Their scope (where they can be used) is within the body (code) of thatmethod; hence the name local
2. Parameters
– These are declared in a method’s header - in the parentheses that followthe name of the method when created
– These are used to hold values that can be sent to the method when it iscalled
– Their scope is the same as for local variables
– The values that are sent to the method are called arguments
– They allow the method to behave differently on each call, providingdifferent arguments are passed to the method
3. Instance variables (properties)
– These are declared within a class
– They specify properties for objects of that class
– Their scope is the whole class (i.e., they can be used by methods of theclass)
• Variables are used to store values that are used in a program
– They make it easy to update values as a program executes
– They make it easy to modify a program’s code
2
Alice Variables: Local Variables
• Steps in declaring a local variable:
1. Drag variable into a method from the list of control structures in edit modeA pop-up window appears
2. The radio button variable next to the label Is variable should be selected(see constants below)
3. Select a data type
4. Enter an initial value; Alice insists on this
– You may want something entirely different for an initial value than theoptions provided
– Simply select one - you will be able to change this once the variable hasbeen declared
5. Name the variable
– The name should be a meaningful noun or noun phrase
3
Alice Variables: Local Variables (2)
• Expressions
– An expression is a piece of code that produces a value
– There are several forms this can take:
1. A literal (actual) value: 5, 17.2
2. A variable: x, length
3. A function call: this.alice.getWidth(), Math.sqrt(x)
4. A compound expression: expression1 operator expression2, where ex-pression1 and expression2 can be any of these types of expressions
• Variables are initialized with an expression
– Frequently want to initialize a variable with an expression other than asimple value
– Considerx : 5y : 10z : 0.5 ∗ x + 1.5 ∗ y
– When declaring z, Alice won’t allow the above, but you can change it afteryou’ve created the variable z
• To change the expressions for a variable:
1. Click the down arrow to the right of the expression - a set of choices appears
4
Alice Variables: Local Variables (3)
These include
– Default values
– Compound expressions (Math)
– Randomly generated values
– User-specified values
– Values of existing variables (listed at bottom)
• Math allows the creation of complicated expressions involving operators
– It also provides math functions like square root, trig functions, etc.
• Function calls can be dragged into the value slots of expressions
5
Alice Variables: Local Variables (4)
• Input can be interactively supplied by the user using the getTYPEFromUser()function (under Scene), where TYPE is replaced by one of Alice’s data types
– This function has a String parameter (message )that is printed before re-trieving a typed-in value from the user
– This string is called a prompt and should tell the user exactly what isexpected
– The program will wait until the user enters a value and hits ENTER beforecontinuing
6
Alice Variables: Local Variables (5)
• Assignment statements
– These are used to change the value of a variable as a program executes
– Assignment statements are dragged from the control list at the bottom ofthe edit window
– They require an expression in the same way as when a variable is initialedon declaration
– The syntax is var name = expression
– The semantics: The value of the expression is stored in the memory locationassociated with var name
– Generally, only local variables appear on the left of assignment statements
• Constants
– When creating a variable, there radio buttons labeled variable and constantnext to Is variable
– A programming constant is a value that does not change throughout aprogram’s execution
– By declaring a variable constant, you will be prevented from changing it’svalue
– Constant names use all caps, unlike variable names
7
Alice Variables: Parameters
• A parameter is a ”variable” that stores a value passed to a method
– The value is sent when the method is called, and the value is called anargument
• Creating a parameter for a method is similar to creating a local variable
– Once a method has been named and created, an add parameter buttonappears in its header
– Selecting this brings up a window just like for variables
8
Alice Variables: Parameters (2)
– When selecting the data type, you’ll see a new option: Gallery
∗ Under Gallery are listed all the classes available in your program
∗ You can pass actual objects as arguments into a method
∗ When selecting a type from the gallery, choose the most general typethat you want your method to accept
∗ Classes that start with S (S-types, which stand for story types) areclasses that cannot be modified by the programer
∗ Appendix C lists the complete Alice S-type hierarchy
9
Alice Variables: Instance Variables
• Instance variables define the properties of a class
• They are created the same as for methods except by choosing Add Propertymenu of the selected class
• When an instance variable is created, Alice automatically creates two methodsassociated with it:
1. Procedure set var name for assigning a value to var name
2. Function get var name for retrieving the value of var name
10
Alice Variables: User-defined Functions
• Just like with methods, you can create custom-designed functions to generatevalues
• They are created in the same way a user-defined procedures are, with thefollowing differences:
– Since a function returns a value, a function has an associated data type,called its return type
– When creating a new function, a pop-up window similar to that for newvariables
– In addition, the body of the function requires a return statement
∗ When executed, a return statement halts the execution of the function,and specifies the value to be returned
∗ Return statements are added by dragging from the control statementlist at the bottom of the edit window
∗ The value to be returned can be any acceptable expression, and theexpression is created in the same way as for variables
• Remember: a function call can appear anywhere a value is expected in a pro-gram
11
Alice Variables: Vehicles(Note: This topic has nothing to do with variables)
• Every class has a vehicle property
• Its default value is Scene
• When a vehicle moves, every object with that vehicle property will move withthe vehicle by the same amount
• To change an object’s vehicle, use the setVehicle() method (a procedure)
• The vehicle property makes it easy to have multiple objects move in tandem
– The alternative would be to program each of the objects to move by thesame amount, using a doTogether() control statement
– This requires a lot more code and effort
12