34
Copyright © 2012, Oracle. All rights reserved. Correlating Java Variables, Data Types, and Expressions with Alice 3 Tools

Correlating Java Variables, Data Types, and …programarejava.wikispaces.com/file/view/JF_V01_S02_L09.pdfunderstanding of how an animation is programmed to execute procedures sequentially,

  • Upload
    hakhanh

  • View
    215

  • Download
    0

Embed Size (px)

Citation preview

Copyright © 2012, Oracle. All rights reserved.

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

2Copyright © 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

What Will I Learn?ObjectivesIn this lesson, you will learn how to: • Describe variables • Describe Java simple types• Define arithmetic operators• Describe relational and logical

operators• Describe assignment operators

3Copyright © 2012, Oracle. All rights reserved.

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!

Let’s more fully examine the Java components you’ve learned by creating animations in Alice.

Correlating Java Variables, Data Types, and Expressions

Why Learn It?

4Copyright © 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

Alice versus JavaAlice 3 Java

3D programming environment that uses visual representations for the Java Programming language.

Programming language; syntax can be edited using integrated development environment (IDE).

Used to create animations or interactive games while working with programming constructs.

Used to create applications that run on any platform, including the web, using Java syntax.

Drag and drop interface designed to reduce syntax errors and make learning programming easier.

IDE helps you objects model real world objects, allow for re-use and easier maintenance.

public class HelloWorld { public static void main() { System.out.println( "Hello World!" ); }

}

5Copyright © 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

Variables in AliceA 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 overX times

6Copyright © 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.

7Copyright © 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).

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

The properties tab containsmore properties.

8Copyright © 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.

9Copyright © 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; while(i<=7) { for(j=1;j<=i;j++) system.out.print("*"); i=i+2; system.out.println(); }}}

i and j are variables that are declared here.

i and j are called in the program body

10Copyright © 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

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

11Copyright © 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

Data Types in Alice (cont.)

Alice supports several data types:Data Type Description

Number You can perform arithmetic using data of type number any number like 0, 3, -1, and 5.79.

Boolean 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.

Object Objects on which you call methods and functions; any object in Alice like a cat, waterfall, etc.

Other Things like Strings (words), sounds, colors and other special values.

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

12Copyright © 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

Data Types in JavaWhen 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.

13Copyright © 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

Data Types in Java (cont.)Wclass PrintText{ public static void main(String[] args) { //declare some variables byte aByte = -10; int aNumber = 10; char aChar = 'b'; boolean isBoolean = true; //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); } }

Variables are declared and initialized

Variables are printed

14Copyright © 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

Java Simple TypesThere are eight basic data types in Java. These are called “simple types (primitives)”.

Data Type

Size Example Data

Data Description

boolean 1 bit true, false Store true/false flags byte 1 byte (8

bits)12, 128 Store integers from -127 to 128

char 2 bytes 'A', '5', '#' Store a single Unicode charactershort 2 bytes 6, -14,

2345Store integers from -32,768 to 32,767

15Copyright © 2012, Oracle. All rights reserved.

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

Size Example Data

Data Description

int 4 bytes 6, -14, 2345

Store integers from -2,147,483,648 to 2,147,483,647

long 8 bytes 3459111, 2

Store integers from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

float 4 bytes 3.145, .077

Store a positive or negative decimal number from 1.4023x10-45 to 3.4028x10+38

double 8 bytes .0000456, 3.7

Store a positive or negative decimal number from 4.9406x10-324 to 1.7977x10308

16Copyright © 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

Arithmetic Operators in AliceThe 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.

Amount and duration arguments

Arithmetic operators perform basic mathmatical operations. They take two operands and return the result of the mathematical calculation.

17Copyright © 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

Arithmetic Operators in JavaArithmetic 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);

}

}

18Copyright © 2012, Oracle. All rights reserved.

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[] args) {int a = 1+ 3;int b = a * 3;int c = b / 4;int d = c – a;int e = -d;System.out.println(“a = ” + a);System.out.println(“b = ” + b);System.out.println(“c = ” + c);System.out.println(“d = ” + d);System.out.println(“e = ” + e);

}}

19Copyright © 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

Relational Operators

Relational operators include: = ≠ > < ≥ ≤

Expressions with relational operators produce true or false values.

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.

20Copyright © 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

Relational Operators in AliceThe following example shows the relational operators available when testing the distance between the cat and the food and the mouse and the food.

21Copyright © 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

Relational Operators in JavaIn Java, the relational operators are typed using symbols. Assume variable A holds 10 and variable B holds 20.

Symbol Name of the Operator Example

== Equal to (A == B) is false

!= Not equal to (A != B) is true

> Greater than (A > B) is false

>= Greater than or equal to (A >= B) is false

< Less than (A < B) is true

<= Less than or equal to (A <= B) is true

22Copyright © 2012, Oracle. All rights reserved.

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.

The results are:a == b = falsea != b = truea > b = falsea < b = trueb >= a = trueb <= a = false

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) );}}

23Copyright © 2012, Oracle. All rights reserved.

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.

24Copyright © 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

Logical Operators in AliceIn 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.

25Copyright © 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

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

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

Symbol Name of the Operator

&& Conditional-AND

|| Conditional-OR

! NOT

26Copyright © 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

Logical Operators in Java (cont.)Below is sample java code illustrating the NOT operator.

The results are:x is not greater than y : falsey is not greater than x : true

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); }}

27Copyright © 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

Assignment Operators in AliceAn 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.

28Copyright © 2012, Oracle. All rights reserved.

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. 1

2

29Copyright © 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

Assignment Operators in JavaIn your Java code, you use the equal sign (“=”) to assign one value to another. For example:

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

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); }}

30Copyright © 2012, Oracle. All rights reserved.

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 Example Equivalent to

+= x += y x = x + y;

-= x -= y x = x - y;

*= x *= y x = x * y;

/= x /= y x = x / y;

31Copyright © 2012, Oracle. All rights reserved.

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: 15The -= result is: -5The *= result is: 50The /= result is: .5

32Copyright © 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

TerminologyKey terms used in this lesson included:Arithmetic operatorsAssignment operatorsData typeLogical operatorsRelational operatorsVariable

33Copyright © 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

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

34Copyright © 2012, Oracle. All rights reserved.

Correlating Java Variables, Data Types, and Expressions

PracticeThe 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