Académique Documents
Professionnel Documents
Culture Documents
CHAPTER OBJECTIVE
To review:
Basics of Java programming
Object-oriented programming
JAVA PROGRAMMING
HELLO WORLD!
GETTING STARTED
In Java, executable statements are placed in methods, that belong
to class definitions
Main method the first method to be executed when running a
Java program.
Program block any set of statements between the braces {
and }
Identifier name of a class, method, or variable in Java.Any string
of characters that begin with a letter, and can consists of letters,
numbers, and underscore characters.
// This is an inline comment.
/*
* This is a block comment.
*/
VARIABLE DECLARATIONS
Each variable must be declared before use in program.
Variable name can consist of letters, digits, underscores,
and dollar signs.
Java is case sensitive.
variable n variable N
Local variables
Instance variables
Class/static variables
VARIABLE DECLARATIONS
Examples:
OPERATORS
Assignment operator
int gear = 1;
x = y = z = 1; byte B = 22;
Arithmetic operators
+
*
/
%
OPERATORS
Unary operators
class Operator {
public static void main(String[] args){
int var=5;
System.out.println(var++);
System.out.println("\n"+ ++var);
}
}
9
OPERATORS
==
!=
>
>=
<
<=
equal to
not equal to
greater than
greater than or equal to
less than
less than or equal to
Conditional Operators
10
&& Conditional-AND
|| Conditional-OR
OPERATOR PRECEDENCE
11
CASTING
Examples:
12
double d2 = 3.9999;
int i2 = (int) d2;
double d3 = (double) i2;
String s1 = "2014";
int i1 = Integer.parseInt(s1);
int i2 = 35;
String s2 = Integer.toString(i2);
// i2 gets value 3
// d3 gets value 3.0
DECISION STATEMENTS
If statement
If else statement
If...else if...else statement
Nested if...else statement
public class Test {
13
DECISION STATEMENTS
Switch statement
switch(grade) {
case 'A' :
System.out.println("Excellent!");
break;
case 'B' :
case 'C' :
System.out.println("Well done");
break;
case 'D' :
System.out.println("You passed");
case 'F' :
System.out.println("Better try again");
break;
default :
System.out.println("Invalid grade");
}
14
LOOPS
while Loop
do...while Loop
for Loop
enhanced for Loop
while( x < 20 ) {
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}
do{
System.out.print("value of x : " + x );
x++;
System.out.print("\n");
}while( x < 20 );
15
SIMPLE OUTPUT
Example:
System.out.print("Java values: ");
System.out.print(3.1416);
System.out.print(',');
System.out.print(15);
System.out.println(" (double,char,int).");
16
SIMPLE INPUT
EXCEPTION HANDLING
18
EXERCISE
1.
2.
3.
19
OBJECT-ORIENTED PROGRAMMING
20
OBJECT TECHNOLOGY
21
OBJECT TECHNOLOGY
22
OBJECT TECHNOLOGY
OBJECT MODEL
Color of a Form
Move a Form
24
Class = automobile
Attributes = make, model, color, year
Object = each individual car
25
OBJECT-ORIENTED CONCEPTS
Java provides explicit support for many of the fundamental ObjectOriented Concepts. Some of these are:
Classification: Grouping related things together. This is supported
through classes, inheritance & packages.
Encapsulation: Representing data and the set of operations on the
data as a single entity - exactly what classes do.
Information Hiding: An object should be in full control of its data,
granting specific access only to whom it wishes.
Inheritance: Java allows related classes to be organized in a hierarchical
manner using the extends keyword.
Polymorphism: Same code behaves differently at different times
during execution.This is due to dynamic binding.
26
27
REFERENCE VARIABLE
poland
"Poland"
40000000
120359
.5
"Germany"
brazil
"Brazil"
180000000
3287951
1
"USA"
ACCESS MODIFIER
Mode of access
same class
same package subclass
same package non-subclass
different package subclass
different package non-subclass
private
protected
no modifier
( C++)
public
yes
no
no
no
no
yes
yes
yes ( C++)
yes
no
yes
yes
yes
no
no
yes
yes
yes
yes
yes
Attributes
/instance
variables
methods
objects
ADVANTAGES OF OBJECT-ORIENTATION.
31
INHERITANCE
INHERITANCE
33
Methods and instance variables of the super class are inherited by subclasses,
thus allowing for code reuse.
A subclass can define additional instance variables (e.g. hours) and additional
methods (e.g. addHours).
A subclass can override some of the methods of the super class to make them
behave differently (e.g. the pay & print)
Constructors are not inherited, but can be called using the super keyword. such
a call must be the first statement.
If the constructor of the super class is not called, then the complier
inserts a call to the default constructor -watch out!
34
ABSTRACT CLASS
Java uses Abstract classes & Interfaces to further strengthen the idea of
inheritance.
To see the role of abstract of classes, suppose that the pay method is not
implemented in the HourlyEmployee subclass.
Obviously, the pay method in the Employee class will be assumed, which
will lead to wrong result.
One solution is to remove the pay method out and put it in another
extension of the Employee class, MonthlyEmployee.
The problem with this solution is that it does not force subclasses of
Employee class to implement the pay method.
35
ABSTRACT CLASS
ABSTRACT CLASS
37
ABSTRACT CLASS
class Executive extends MonthlyEmployee {
private double bonus;
public Executive(String exName, double exRate) {
super(exName, exRate);
bonus = 0;
}
public void awardBonus(double amount) {
bonus = amount;
}
public double pay() {
double paycheck = super.pay() + bonus;
bonus = 0;
return paycheck;
}
public void print() {
super.print();
System.out.println("Current bonus: " + bonus);
}
}
HourlyEmployee
Employee
MonthlyEmployee
Executive
38
ABSTRACT CLASS
39
INTERFACE
If both parents of a child implement a method, which one does the child
inherits? - Multiple inheritance confusion.
40
INTERFACE
Recall also that java has the java.util.Arrays class, which has a sort method
that can sort any array whose contents are either primitive values or
Comparable objects.
Thus, to sort our list of Employee objects, all we need is to modify the
Employee class to implement the Comparable interface.
Notice that this will work even if the Employee class is extending another
class or implementing another interface.
This modification is shown in the next page.
41
INTERFACE
abstract class Employee implements Comparable {
protected String name;
protected double payRate;
public Employee(String empName, double empRate)
name = empName;
payRate = empRate;
}
public String getName() {return name;}
public void setPayRate(double newRate) {
payRate = newRate;
}
abstract public double pay();
public int compareTo(Object o) {
Employee e = (Employee) o;
return name.compareTo( e.getName());
}
}
HourlyEmployee
Comparable
42
Employee
MonthlyEmployee
Executive
EXERCISE
1.
43
EXERCISE
44