33
1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

Embed Size (px)

Citation preview

Page 1: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

1

Chapter 2: Java Fundamentals cont’d

Spring 2006-2007Lory Al Moakar

Page 2: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

2

Outline 2.1 The Parts of a Java Program 2.2 The print and println Methods, and the Java Standard Class

Library 2.3 Variables and Literals 2.4 Primitive Data Types 2.5 Arithmetic Operators 2.6 Combined Assignment Operators 2.7 Conversion Between Primitive Types 2.8 Creating Named Constants with final 2.9 The String Class 2.10 Scope 2.11 Comments 2.12 Programming Style 2.13 Reading Keyboard Input 2.14 Dialog Boxes 2.15 Common Errors to Avoid

Page 3: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

3

Review

Parts of the program Comments: start with //

Are ignored by the compiler Class header : public class Name Method header:

public static void main( String args[] ) Statements

Page 4: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

4

Review

System.out.print: displays on the screen what we put in “”

System.out.println: same as print except that it moves the cursor to a new line after displaying the text

Page 5: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

5

ReviewMoves the cursor to

\n a new line

\t the next tab stop

\b one character back

\r To the beginning of the line

\\ Prints a backslash

\’ Prints a single quote

\” Prints a double quotation mark

Page 6: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

6

Review

Variables: Declare: type and identifier Initialize: give it value ~ literal

Literals: Identifiers:

Start with letter, _, or $ Have only numbers, letters, _ and $ Cannot be a keyword

Page 7: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

7

Printing a variable

2 ways: On its own:

System.out.println( identifier); Combined with text:System.out.println( “The result is: “+

identifier );

Page 8: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

8

Example// This program has a variable.

public class Variable{ public static void main(String[] args) { int value;

value = 5; System.out.print("The value is "); System.out.println(value); }}

Page 9: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

9

2.4 Primitive Data Types

Each variable has a data type which is the type of data that can be stored in the memory location allocated

Here is a list of primitive(numeric) data types:

Page 10: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

10

2.4 Primitive Data Types cont’dData type

Size Range

byte 1 byte Integer between -128 to 127

short 2 bytes Integer between -215 to 215 -1

int 4 bytes Integer between -231 to 231 -1

long 8 bytes Integer between -263 to 263 -1

float 4 bytes Floating point numbers with 7 digits of accuracy

double 8 bytes Floating point numbers with 15 digits of accuracy

No digits after decimal point

15 digits after decimal point

7 digits after decimal point

Page 11: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

11

Declaration revisited

Datatype Variablename; Examples:

int hours; byte minutes; short month; float average; double distance

Page 12: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

12

The Integer Data Types// This program has variables of several of the integer types.

public class IntegerVariables{ public static void main(String[] args) { int checking; // Declare an int variable named checking. byte miles; // Declare a byte variable named miles. short minutes; // Declare a short variable named minutes. long days; // Declare a long variable named days.

checking = -20; miles = 105; minutes = 120; days = 185000; System.out.println("We have made a journey of " + miles + " miles."); System.out.println("It took us " + minutes + " minutes."); System.out.println("Our account balance is $" + checking); System.out.println("About " + days + " days ago Columbus " + "stood on this spot."); }}

We have made a journey of 105 miles.It took us 120 minutes.Our account balance is $-20.About 185000 days ago Columbus stood on this spot.No Commas

Page 13: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

13

The Floating Data Types// This program demonstrates the double data type.

public class Sale{ public static void main(String[] args) { double price, tax, total;

price = 29.75; tax = 1.76; total = 31.51; System.out.println("The price of the item " + "is " + price); System.out.println("The tax is " + tax); System.out.println("The total is " + total); }}

The price of the item is 29.75The tax is 1.76The total is 31.15

Page 14: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

14

The Floating Data Types

float number = 23.5; float number = 23.5f; float number = 23.5F;

ERROR

CORRECT

CORRECT

Page 15: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

15

The boolean Data Type a boolean variable can have two values:

true false

Example:boolean bool;bool = true;System.out.println(bool);bool = false;System.out.println(bool);

truefalse

Page 16: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

16

The char Data Type used to store characters character literals are enclosed in single

quotes Example:

char letter;letter = 'A';System.out.println(letter);letter = 'B';System.out.println(letter);

AB

Page 17: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

17

The char Data Type

Characters are internally represented by numbers.

Java uses Unicode which is a set of numbers that are used as codes for representing characters

The codes are found in appendix A on the CD

Page 18: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

18

Example

char letter; letter = 65; System.out.println(letter) letter = 66; System.out.println(letter);

A B

Page 19: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

19

Variable Assignment and Initialization

assignment statement is used to put a value into a variable. Ex: x = 15;

always put the identifier on the left of the equal sign and the literal to the right of the equal sign. Ex: 15 = x; is incorrect

you can assign the value of one identifier to another. Ex: x = y; now x has the value stored in y

Page 20: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

20

Valid Variable Declaration

int month =2, days = 28; float distance = 35.2f; int c = 8, y =10, x, v=2;

Page 21: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

21

2.5 Arithmetic Operators

3 types of operator: unary: require a single operand.

There is one unary operand: -9 (the negation operator)

--5 is 5 binary: require two operands ternary: require three operands

Page 22: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

22

Arithmetic operatorsOperator Meaning Type Example

+ Addition Binary total = cost+tax;

- Subtraction Binary cost = total-tax;

* Multiplication Binary Tax = cost*rate;

/ Division Binary saleprice=original/2;

% Modulus Binary remainder=value%3;

Page 23: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

23

Examples of Statements with Arithmetic Operators

amount = x + y; amount = 562+543; Minutes = 800 – call; Tip = bill * 0.18; Slice = cake /8;

Notice that the identifier is to the

left of the =

Notice that the arithmetic operator

is to the right of the =

Page 24: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

24

Example

int counter = 0; //counter =0counter = counter + 1; //counter =1counter = counter + 39; //counter=40counter = counter – 8; //counter=32counter = counter * 5; //counter=160counter = counter / 2; //counter= 80

Page 25: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

25

The % operator

Returns the remainder of the division Examples;

4%5 is 4 30%6 is 0 22%7 is 1 3205%100 is 5 3205%10 is 5

Page 26: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

26

Exercise

Write the following in a Java file:double amount = 137/5;

System.out.println(“Amount is : “ + amount );

amount = 137.0/5; System.out.println(“Amount is : “ +

amount );

Page 27: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

27

Integer Division

Dividing an integer by an integer gives an integer the remainder is ignored

Examples: 5/4 is 1 17/3 is 5

Page 28: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

28

Operator Precedence

What is the result of: Polynomial = 1+2*3+ 6/2 -2; Is it ?

(1+2)*3 + 6/(2-2) 1+(2*3) +(6/2)-2 (1+2)*3 + (6/2)-2

Page 29: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

29

Precedence Rules

Always evaluate * , / and % before + and –

Always negate before any calculations *, / and % have same precedence + and – have same precedence If equal precedence then evaluate

from left to right except for negations where we evaluate from right to left

Page 30: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

30

Precedence examples

Polynomial = 1+2*3+ 6/2 – 2; Polynomial has the value of 1+6+3-2=8

Polynomial = –1 + 5 – 2; // 2 Polynomial = –(–3) + –(–5); //8

Page 31: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

31

Grouping with parentheses

You can use parentheses to force the evaluation of a formula

Examples: x * ( y + z*z ) instead of x*y + z*z x * ( y * ( z + 165 ) + 85 ) – 65 Average = (a +b +c ) /3;

Page 32: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

32

The Math class

value = Math.pow( x,y); // now value holds x to the power of y

value = Math.sqrt( x); //now value holds the square root of x

Page 33: 1 Chapter 2: Java Fundamentals cont’d Spring 2006-2007 Lory Al Moakar

33

Combined Assignment Operators

+= x += 1; x = x + 1;

–= x –= 1; x = x – 1;

*= x *= 1; x = x * 1;

/= x /= 1; x = x / 1;

%= x %= 1; x = x % 1;