Presentation is loading. Please wait.

Presentation is loading. Please wait.

Agenda Types and identifiers Practice Assignment Keywords in Java

Similar presentations


Presentation on theme: "Agenda Types and identifiers Practice Assignment Keywords in Java"— Presentation transcript:

1 Agenda Types and identifiers Practice Assignment Keywords in Java
Type casting Homework

2 declaration The first time a variable is introduced in a program. Java is strongly typed. Initialization: a variable get assigned a value. A variable if often initialized in its declaration, but doesn’t have to be. Java demands all variables to be declared before they can be used. Two parts: <type> <name> Example: int intNum; intNum=7; Int intNum=7;

3 Assignment The value of a variable is changed through assignment:
int x; x=10; x=15; 10 15

4 keywords abstract double int strictfp boolean else interface super break extends long switch byte final native synchronized case finally new this catch float package throw char for private throws class goto protected transient const if public try continue implements return void Default import short volatile do instanceof static while

5

6 Types Every identifier has a type Primitive(built-in) types:
int – integer boolean – true or false double – floating-point numbers char – character

7 Type Casting Type casting converts a number of one type to a number of a different, but compatible type. int intNumber= 35; double doubleNumber = 3.9; double doubleValue=intNumber; //OK int intValue = doubleNumber; //Wrong int intValue = (int)doubleNumber; Test this on Eclipse

8 Example int intNum=5; double doubleNum=3.98; doubleNum = intNum; System.out.println("doubleNum: " + doubleNum); intNum =(int) doubleNum; System.out.println("intNum: " + intNum); double average; average=(double)(10/3); System.out.println("average: " + average);

9 AP type questions Which of the following pairs of declarations will cause an error message? I. double x = 14.7; int y = x; II double x = 14.7; int y = (int) x; III int x = 14; double y = x;

10 double answer = 13/5; System. out. println(“13/5 = “ + answer); //2
double answer = 13/5; System.out.println(“13/5 = “ + answer); //2.0 The programmer intends the output to be 13 / 5 = 2.6; HOW???

11 Concatenation A System.out.println() statement can be used to output the value of a variable. Variable identifiers are not enclosed by quotation marks. To append, or concatenate, the value of a variable to a string, the + operator is used. The + operator converts the value of the variable to a string and then concatenates the strings before output.

12 Homework 1 Create a Smile application that displays a smiling face made of keyboard characters. The application output should look similar to: ***** * * * _ _ * * * * : * * \ _ / * * * ******

13 Homework 2 Create a RectanglePerimeter application that calculates and displays the perimeter of a rectangle with width 4 and length 13. The perimeter of a rectangle is calculated as 2w + 2l. Use variables as appropriate and output the perimeter using println.


Download ppt "Agenda Types and identifiers Practice Assignment Keywords in Java"

Similar presentations


Ads by Google