Transcript Lecture3

Chapter 2: Java
Fundamentals cont’d
Spring 2006-2007
Lory Al Moakar
1
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
2
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
3
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
4
Review
Moves 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
5
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
6
Printing a variable
 2 ways:
 On its own:
System.out.println( identifier);
 Combined with text:
System.out.println( “The result is: “+
identifier );
7
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);
8
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:
9
2.4 Primitive Data Types cont’d
Data
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
after
Integer between -263 7todigits
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
decimal point
15 digits after
decimal point
10
Declaration revisited
 Datatype Variablename;
 Examples:





int hours;
byte minutes;
short month;
float average;
double distance
11
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.
We have
made named
a journey
byte miles; // Declare
a byte variable
miles. of 105
It took
us 120
minutes.
short minutes; // Declare
a short
variable
named minutes.
long days;
// Declare
long variable
named days.
Our aaccount
balance
is $-20.
}
}
miles.
About 185000 days ago Columbus stood
checking = -20;
miles = 105;
No Commas
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.");
on this spot.
12
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.75
The tax is 1.76
The total is 31.15
13
The Floating Data Types
 float number = 23.5;
 float number = 23.5f;
 float number = 23.5F;
ERROR
CORRECT
CORRECT
14
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);
true
false
15
The char Data Type
 used to store characters
 character literals are enclosed in single
quotes
 Example:
char letter;
A
B
letter = 'A';
System.out.println(letter);
letter = 'B';
System.out.println(letter);
16
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
17
Example
char letter;
letter = 65;
System.out.println(letter)
letter = 66;
System.out.println(letter);
A
B
18
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
19
Valid Variable Declaration
 int month =2, days = 28;
 float distance = 35.2f;
 int c = 8, y =10, x, v=2;
20
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
21
Arithmetic operators
Operator
Meaning
Type
Example
+
Addition
Binary total = cost+tax;
-
Subtraction
Binary cost = total-tax;
*
Multiplication Binary Tax = cost*rate;
/
Division
%
Modulus
Binary saleprice=original/2
;
Binary remainder=value%
3;
22
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 =
23
Example
int counter = 0;
counter = counter
counter = counter
counter = counter
counter = counter
counter = counter
//counter =0
+ 1; //counter =1
+ 39; //counter=40
– 8; //counter=32
* 5; //counter=160
/ 2; //counter= 80
24
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
25
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 );
26
Integer Division
 Dividing an integer by an integer
gives an integer  the remainder is
ignored
 Examples:
 5/4 is 1
 17/3 is 5
27
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
28
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
29
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
30
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;
31
The Math class
 value
holds
 value
holds
= Math.pow( x,y); // now value
x to the power of y
= Math.sqrt( x); //now value
the square root of x
32
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;
33