Você está na página 1de 34

Correlating Java Variables, Data Types, and Expressions with Alice 3 Tools

Copyright 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

What Will I Learn?


Objectives In this lesson, you will learn how to: Describe variables Describe Java simple types Define arithmetic operators Describe relational and logical operators Describe assignment operators

Copyright 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

Why Learn It?


Purpose Using Alice 3, you program animations using simple drag and drop techniques. You gain an understanding of how an animation is programmed to execute procedures sequentially, randomly, and conditionally. You use variables and programming constructs in an environment where you can immediately see the results of your actions. You are learning to program without learning the actual syntax of the Java programming language! Lets more fully examine the Java components youve learned by creating animations in Alice.

Copyright 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

Alice versus Java


Alice 3 3D programming environment that uses visual representations for the Java Programming language. Java Programming language; syntax can be edited using integrated development environment (IDE).

Used to create animations or Used to create applications that run interactive games while working with on any platform, including the web, programming constructs. using Java syntax. Drag and drop interface designed to IDE helps you objects model real reduce syntax errors and make world objects, allow for re-use and learning programming easier. easier maintenance.
public class HelloWorld { public static void main() { System.out.println( "Hello World!" ); } }

Copyright 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

Variables in Alice
A typical application uses various values and these values continuously change while the program is running. For example, if you program a car to roll over a certain number of times in Alice 3, the value entered by one user may be different from the value entered by another user.

Roll over X times

Copyright 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

Variables in Alice (cont.)


Alice 3 manages the values entered by different users with variables.
A variable is a place in memory where data of a specific type can be stored for later retrieval and use. Each variable is given a unique name to make it easy to find. We can then use it to store and retrieve data.

Copyright 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

Variables in Alice (cont.)


in Alice 3, properties of objects are variables that can be referenced in your animation. Alice has many built-in properties (such as car color).

The properties tab contains more properties.

Object Properties such as width, height, and depth are variables.

Copyright 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

Variables in Alice (cont.)


You can also create your own variables. To create a variable in a procedure, such as one to track the number of times a car spins, you can drag the local tile into the procedure. This will create a variable that will only run in the current procedure.

Copyright 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

Variables in Java
In Java, you declare variables that can then be referenced in other parts of the program.
public class Print{ public static void main(String[] args){ int i=1; int j; i and j are variables that are while(i<=7) declared here. { for(j=1;j<=i;j++) system.out.print("*"); i=i+2; i and j are called in system.out.println(); the program body } } }

Copyright 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

Data Types in Alice


Note how when you set up a variable in Alice, you need to define the type of the variable.

Copyright 2012, Oracle. All rights reserved.

10

Correlating Java Variables, Data Types, and Expressions

Data Types in Alice (cont.)


A variable's data type defines the kind of values that a variable can store.

Alice supports several data types:


Data Type Number Boolean Description You can perform arithmetic using data of type number any number like 0, 3, -1, and 5.79. Data can have one of two values, true or false; usually data of this type is the result of tests that compare one thing to another. Objects on which you call methods and functions; any object in Alice like a cat, waterfall, etc. Things like Strings (words), sounds, colors and other special values.

Object Other

Copyright 2012, Oracle. All rights reserved.

11

Correlating Java Variables, Data Types, and Expressions

Data Types in Java


When you declare a variable in a Java program, it also has a type associated with it. On the next slide, you see variables defined using four different Java types.

Copyright 2012, Oracle. All rights reserved.

12

Correlating Java Variables, Data Types, and Expressions

Data Types in Java (cont.)


class PrintText{ W public static void main(String[] args) { //declare some variables Variables are declared and byte aByte = -10; initialized int aNumber = 10; char aChar = 'b'; boolean isBoolean = true; Variables are printed //print variables alone System.out.println(aByte); System.out.println(aNumber); //print variables with text System.out.println("aChar = " + aChar); System.out.println("The boolean var is:" + isBoolean); } }

Copyright 2012, Oracle. All rights reserved.

13

Correlating Java Variables, Data Types, and Expressions

Java Simple Types


There are eight basic data types in Java. These are called simple types (primitives).
Data Type byte char short Size Example Data true, false 12, 128 'A', '5', '#' 6, -14, 2345 Data Description Store true/false flags Store integers from -127 to 128 Store a single Unicode character Store integers from -32,768 to 32,767

boolean 1 bit 1 byte (8 bits) 2 bytes 2 bytes

Copyright 2012, Oracle. All rights reserved.

14

Correlating Java Variables, Data Types, and Expressions

Java Simple Types (cont.)


There are eight basic data types in Java. These are called simple types.
Data Type int long Size 4 bytes 8 bytes Example Data 6, -14, 2345 3459111, 2 3.145, .077 .0000456, 3.7 Data Description Store integers from -2,147,483,648 to 2,147,483,647 Store integers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 Store a positive or negative decimal number from 1.4023x10-45 to 3.4028x10+38 Store a positive or negative decimal number from 4.9406x10-324 to 1.7977x10308

float

4 bytes

double

8 bytes

Copyright 2012, Oracle. All rights reserved.

15

Correlating Java Variables, Data Types, and Expressions

Arithmetic Operators in Alice


The arithmetic operators in Alice 3 include add (+), subtract (-), multiply (*), and divide (/). You can use them to create expressions. Arithmetic operators are available in: Amount and Duration arguments Get Distance functions You can also store the value of an arithmetic expression in a variable.
Arithmetic operators perform basic mathmatical operations. They take two operands and return the result of the mathematical calculation.
Copyright 2012, Oracle. All rights reserved. 16

Amount and duration arguments

Correlating Java Variables, Data Types, and Expressions

Arithmetic Operators in Java


Arithmetic operators work the same in Alice and Java, they are just accessed differently. In Java, you include arithmetic operators directly in your code.
class basicOperators { //using arithmetic operators public static void main(String[] args) { int a = 1 + 1; int b = 3 * 3; int c = 1 + 8 / 4; int d = -2; System.out.println( a = + a); System.out.println(b = + b); System.out.println(c = + c); System.out.println(d = + d); } }
Copyright 2012, Oracle. All rights reserved. 17

Correlating Java Variables, Data Types, and Expressions

Arithmetic Operators in Java (cont.)


What are the results of basicOperators2? Note how the variables are referenced in subsequent lines of code.
class basicOperators2 { //using arithmetic operators and variables public static void main(String[] int a = 1+ 3; int b = a * 3; int c = b / 4; int d = c a; int e = -d; System.out.println(a = System.out.println(b = System.out.println(c = System.out.println(d = System.out.println(e = } } args) {

+ + + + +

a); b); c); d); e);

Copyright 2012, Oracle. All rights reserved.

18

Correlating Java Variables, Data Types, and Expressions

Relational Operators
A relational operator is a lexical unit used to express a relation, such as equality or greater than, between two expressions. Two suitable expressions combined with a relational operator often form a relational expression or condition in a programming language.

Relational operators include: = > <

Expressions with relational operators produce true or false values.

Copyright 2012, Oracle. All rights reserved.

19

Correlating Java Variables, Data Types, and Expressions

Relational Operators in Alice


The following example shows the relational operators available when testing the distance between the cat and the food and the mouse and the food.

Copyright 2012, Oracle. All rights reserved.

20

Correlating Java Variables, Data Types, and Expressions

Relational Operators in Java


In Java, the relational operators are typed using symbols. Assume variable A holds 10 and variable B holds 20.
Symbol == != > >= < <= Name of the Operator Equal to Not equal to Greater than Greater than or equal to Less than Less than or equal to Example (A == B) is false (A != B) is true (A > B) is false (A >= B) is false (A < B) is true (A <= B) is true

Copyright 2012, Oracle. All rights reserved.

21

Correlating Java Variables, Data Types, and Expressions

Relational Operators in Java (cont.)


Relational operators in Java and Alice work the same way. Below is sample java code containing relational operators.
class Test { public static void main(String[] args) { int a = 10; int b = 20; System.out.println("a == b = " + (a == b) ); System.out.println("a != b = " + (a != b) ); System.out.println("a > b = " + (a > b) ); System.out.println("a < b = " + (a < b) ); System.out.println("b >= a = " + (b >= a) ); System.out.println("b <= a = " + (b <= a) ); } }

The results are:


a == b = false a != b = true a > b = false a < b = true b >= a = true b <= a = false

Copyright 2012, Oracle. All rights reserved.

22

Correlating Java Variables, Data Types, and Expressions

Logical Operators
Logical operators are the boolean operators (AND, OR and NOT). Expressions written with logical operators result in TRUE or FALSE.

The following example shows variations of logical operators in Alice 3.

Copyright 2012, Oracle. All rights reserved.

23

Correlating Java Variables, Data Types, and Expressions

Logical Operators in Alice


In the following example, we have used the both a and b logical operators to combine two conditions into a single condition. Both of these conditions must be True for the overall condition to be True. Otherwise, it will be False.

Copyright 2012, Oracle. All rights reserved.

24

Correlating Java Variables, Data Types, and Expressions

Logical Operators in Java


Logical operators work the same in Alice 3 and Java, they are just accessed differently. In Java, the logical operators are typed using symbols:
Symbol &&
||

Name of the Operator Conditional-AND Conditional-OR NOT

The && and || logical operators are sometimes called short-circuted operators.

Copyright 2012, Oracle. All rights reserved.

25

Correlating Java Variables, Data Types, and Expressions

Logical Operators in Java (cont.)


Below is sample java code illustrating the NOT operator.
class BoolNotDemo { public static void main(String[] args){ int x = 2; int y = 1; boolean bl; bl = !(x > y); // bl is false System.out.println("x is not greater than y:"+bl); bl = !(y > x); // bl is true System.out.println("y is not greater than x:"+bl); } }

The results are:


x is not greater than y : false y is not greater than x : true
Copyright 2012, Oracle. All rights reserved. 26

Correlating Java Variables, Data Types, and Expressions

Assignment Operators in Alice


An assignment operator actually changes the value of a variable. In Alice 3, you assign values via the interface to properties and variables. Below, the color, opacity, position, vehicle and size are assigned values in the Object Properties window.

Copyright 2012, Oracle. All rights reserved.

27

Correlating Java Variables, Data Types, and Expressions

Assignment Operators in Alice (cont.)


An assignment operator actually changes the value of a variable. In Alice 3, you assign values via the interface to properties and variable. In this example: 1. The local variable numSpins is assigned an initial value of 3. 2. The number of times the car rolls left is assigned to numSpins. 2
1

Copyright 2012, Oracle. All rights reserved.

28

Correlating Java Variables, Data Types, and Expressions

Assignment Operators in Java


In your Java code, you use the equal sign (=) to assign one value to another. For example:
class AssignmentDemo{ public static void main(String[] args) { int x=5; int y=10; int z=20; y = x; z = y + z; System.out.println("The value of y is:"+ y); System.out.println("The value of z is:"+ z); } }

The output is: The value of y is: 5 The value of z is: 25

Copyright 2012, Oracle. All rights reserved.

29

Correlating Java Variables, Data Types, and Expressions

Assignment Operators in Java (cont.)


Java provides a special syntax for handling standard operations such as z = y + z; The syntax is z += y; This code saves a few keystrokes and but still assigns z the value of y plus z. The following chart summarizes the other assignment syntax for other operations.
Symbol += -= *= /= x += y x -= y x *= y x /= y Example Equivalent to x = x + y; x = x - y; x = x * y; x = x / y;

Copyright 2012, Oracle. All rights reserved.

30

Correlating Java Variables, Data Types, and Expressions

Assignment Operators in Java (cont.)


class AssignmentDemo2{ public static void main(String[] args) { int x=5; int y=10; x += y; System.out.println("The += result is:"+ x); x -= y; System.out.println("The -= result is:"+ x); x *= y; System.out.println("The *= result is:"+ x); x /= y; System.out.println("The /= result is"+ x); } }

The output is:


The += result is: 15 The -= result is: -5 The *= result is: 50 The /= result is: .5
Copyright 2012, Oracle. All rights reserved. 31

Correlating Java Variables, Data Types, and Expressions

Terminology
Key terms used in this lesson included: Arithmetic operators Assignment operators Data type Logical operators Relational operators Variable

Copyright 2012, Oracle. All rights reserved.

32

Correlating Java Variables, Data Types, and Expressions

Summary
In this lesson, you learned how to: Describe variables Describe Java simple types Define arithmetic operators Describe relational and logical operators Describe assignment operators

Copyright 2012, Oracle. All rights reserved.

33

Correlating Java Variables, Data Types, and Expressions

Practice
The exercises for this lesson cover the following topics: Create an animation to demonstrate the use of variables and simple types Create an animation to demonstrate the use of arithmetic, relational, and logical operators Create a Java syntax cheat sheet

Copyright 2012, Oracle. All rights reserved.

34

Você também pode gostar