Escolar Documentos
Profissional Documentos
Cultura Documentos
ECEg-3142 JiT
Chapter 1 - 1
Course Overview
Java as a second language course
Pre-requisites
ECEg-3142 JiT
Chapter 1 - 2
Class Structure
Lectures
(GIV) MON (10:00 12:00 a.m.) & wed (3:15-5:00 pm)
(CR2202)
(CR2202)
(GIII) wed (8:00-10:00 am) & Thur (3:15-5:00 pm)
(CR7304)
(CR2202)
Lab hours
For the labs, bring a laptop to class if you have one.
Office hours
Theoretical principles
Programming issues
ECEg-3142 JiT
Chapter 1 - 3
Text Book
Introduction to Object-Oriented Programming using java by
C. Thomas wu
ECEg-3142 JiT
Chapter 1 - 4
Outline
Introduce JAVA
Introduction to OOP Concepts
Classes and Objects
Messages and Methods
Getting started with java
The first java program and its components
Software development lifecycle
ECEg-3142 JiT
Chapter 1 - 5
Chapter 1
Introduction to
Object-Oriented
Programming and
Software
Development
ECEg-3142 JiT
Chapter 1 - 6
Objectives
After you have read and studied this chapter, you
should be able to
Name the basic components of object-oriented programming
Differentiate classes and objects.
Differentiate class and instance methods.
Differentiate class and instance data values.
Draw program diagrams using icons for classes and objects
Describe significance of inheritance in object-oriented programs
Name and explain the stages of the software lifecycle
ECEg-3142 JiT
Chapter 1 - 7
ECEg-3142 JiT
Chapter 1 - 8
procedural
ECEg-3142 JiT
object-oriented
ECEg-3142 JiT
Chapter 1 - 10
ECEg-3142 JiT
Chapter 1 - 11
Introducing Java
Released mid 1995 by Sun Microsystems
Designed to be:
A powerful, full-featured, pure OO development
language
Easy to learn - syntax is similar to C++
Platform independent
Support development of applications for networked
environment
Ideal for Web-based applications
ECEg-3142 JiT
Chapter 1 - 12
Introducing Java
Powerful
Class library
Hundreds of prewritten classes
Provide methods to accomplish various tasks
OO
Implements OO concepts (what is that???)
Encourages good software design
Reduces debugging and maintenance
ECEg-3142 JiT
Chapter 1 - 13
Introducing Java
Simplicity
Keywords
Java has 48 keywords
vs. Cobol (>300) or VB which have hundreds
ECEg-3142 JiT
Chapter 1 - 14
Reserved Words
The Java reserved words:
abstract
assert
boolean
break
byte
case
catch
char
class
const
continue
default
do
double
else
enum
extends
false
final
finally
float
for
goto
if
implements
import
instanceof
int
interface
long
native
new
null
package
private
protected
public
return
short
static
strictfp
super
switch
synchronized
this
throw
throws
transient
true
try
void
volatile
while
ECEg-3142 JiT
Chapter 1 - 15
Program Development
The mechanics of developing a program include
several activities
writing the program in a specific programming language
(such as Java)
translating the program into a form that the computer can
execute
investigating and fixing various types of errors that can occur
Chapter 1 - 16
ECEg-3142 JiT
Chapter 1 - 17
Errors
A program can have three types of errors
The compiler will find syntax errors and other basic problems
(compile-time errors)
If compile-time errors exist, an executable version of the program is
not created
ECEg-3142 JiT
Chapter 1 - 18
Programming Languages
Each type of CPU executes only a particular
machine language
A program must be translated into machine
language before it can be executed
A compiler is a software tool which translates
source code into a specific target language
Often, that target language is the machine
language for a particular CPU type
ECEg-3142 JiT
Java Translation
The Java compiler translates Java source code into a
special representation called bytecode
Java bytecode is not the machine language for any
traditional CPU
Another software tool, called an interpreter, translates
bytecode into machine language and executes it
Therefore the Java compiler is not tied to any
particular machine
Java is considered to be architecture-neutral
ECEg-3142 JiT
Chapter 1 - 20
Introducing Java
Portability
Programs can be written and compiled once, then run
on different platforms
Important for internet applications (applets)
Achieved by using:
Bytecode
Produced when a Java program is compiled
Chapter 1 - 21
ECEg-3142 JiT
Chapter 1 - 22
Introducing Java
Development environments
Java Development Kit
Available free from Sun/oracle Web site
Includes: compiler JVM and prewritten classes
Chapter 1 - 23
Development Environments
There are many programs that support the
development of Java software, including:
Chapter 1 - 24
File
HelloWorldWideWeb.java
ECEg-3142 JiT
Chapter 1 - 25
class
Indicates line of code is a class header
ECEg-3142 JiT
Chapter 1 - 26
ECEg-3142 JiT
Chapter 1 - 27
Identifiers
Identifiers are the words a programmer uses in a program
An identifier can be made up of letters, digits, the
underscore character ( _ ), and the dollar sign
Identifiers cannot begin with a digit
Java is case sensitive - Total, total, and TOTAL
are different identifiers
By convention, programmers use different case styles for
different types of identifiers, such as
title case for class names - Loan
upper case for constants - MAXIMUM
ECEg-3142 JiT
Chapter 1 - 28
Chapter 1 - 29
Jimmauniversity.java
public class Jimmauniversity
{
//----------------------------------------------------------------// Prints motto of jimma university .
//----------------------------------------------------------------public static void main (String[] args)
{
System.out.println ("A motto for jimma university");
System.out.println ("We are in the community.");
}
}
ECEg-3142 JiT
Chapter 1 - 30
class body
ECEg-3142 JiT
Chapter 1 - 31
method header
}
}
ECEg-3142 JiT
Chapter 1 - 32
Comments GIII
*/
*/
ECEg-3142 JiT
Chapter 1 - 33
e.g.
int aNumber = 1;
boolean isFound = true;
double anotherNumber = 1.50;
String s = example of a string;
ECEg-3142 JiT
Chapter 1 - 34
Variable Definitions
Variable definitions
Variable: name of place in memory that can contain data
All variables have:
Data type kind of data variable can contain
Name identifier that refers to the variable
Value the default or specified value
also called the literal of the statement
Semicolon
Remember: All java statements end with a semicolon!
e.g.
String s = MC697;
int count = 5;
ECEg-3142 JiT
Chapter 1 - 35
Variable Definitions
Initializing Variables
Assignment operator (=)
Used to assign value to a variable
char c = a; - note the single quotes
boolean b = true;
double d = 1.25;
ECEg-3142 JiT
Chapter 1 - 36
Data Types
Declaring Variables
Variable data type must be declared prior to initialization
There are two basic data types:
Primitive data types
Eight available primitive data types
Primitive data types are not capitalized in variable declarations
int aNumber = 5;
ECEg-3142 JiT
Chapter 1 - 37
ECEg-3142 JiT
Chapter 1 - 38
Data Types
Using Reference Variables
Uses class name as a data type
Points to an instance of that class
Example:
String s = Hello World;
Employee emp1;
ECEg-3142 JiT
Chapter 1 - 39
Variable Constants
Using Constants
Variable with a value that doesnt change
Keyword
final
Denotes value cannot change
Example:
final double SALES_TAX_RATE = 4.5;
note the naming convention
other examples?
ECEg-3142 JiT
Chapter 1 - 40
Method Defnitions
Methods contain:
access modifier
defines who can call this method
return type
defines what this method will return
static
optional - we will discuss this later
method name
should begin with a lower case
argument definition section
defines what input is expected in this method
block of code with statements
ECEg-3142 JiT
Chapter 1 - 41
ECEg-3142 JiT
Chapter 1 - 42
ECEg-3142 JiT
Chapter 1 - 43
ECEg-3142 JiT
Chapter 1 - 44
Math Class
Methods for exponentiation, rounding, etc.
ECEg-3142 JiT
Chapter 1 - 45
ECEg-3142 JiT
Chapter 1 - 46
ECEg-3142 JiT
Chapter 1 - 47
OOP Concepts
ECEg-3142 JiT
Chapter 1 - 48
ECEg-3142 JiT
Chapter 1 - 49
ECEg-3142 JiT
Chapter 1 - 50
What is an Object?
An Object is a software entity that models something in the real world. It has two
main properties:
State: the object encapsulates information about itself - fields.
Behaviour: the object can do some things on behalf of other objects methods
ECEg-3142 JiT
Chapter 1 - 51
What is a class?
A class is a general, abstract representation of an object, that specifies the fields and methods that
such an object has.
When we write OO programs we don't define individual objects, we define classes, and then use
them as templates for constructing objects. Each individual object is called an instance of its class.
For example, you might have a Tree class that describes the features of all trees (each tree has
branches and roots, grows, etc.).
The Tree class serves as an abstract model for the concept of a tree. To reach out and grab, or cut
down a tree, you must have a concrete instance of that tree a tree object.
Of course, once you have a Tree class, you can create lots of different instances of that tree, and
each different tree instance can have different features (it can be short, tall, bushy, have fruits, etc)
ECEg-3142 JiT
Chapter 1 - 52
methods: Example
ChangeNumber
Objects:
Desk
123445
ChairA
32143
ChairB
45687
ECEg-3142 JiT
Chapter 1 - 53
Once a class has been defined, it can be used to create any number of
objects of that class.
To create an object, we use new operator, the class name, and supply
construction parameters (if any) in parenthesis.
For example, the following statement creates and prints an object of
the Rectangle class
ECEg-3142 JiT
Chapter 1 - 54
Once the variable is declared, it can be used to create an object and store its reference in
the variable as follows:
myRectangle = new Rectangle(5, 10, 20, 30);
In fact, the process of declaring an object reference variable and creating an object can
be combined in one statement as follows.
Rectangle myRectangle = new Rectangle(5, 10, 20, 30);
The relationship between the reference variable, myRectangle and the Rectangle object
created is shown in the following figure :
ECEg-3142 JiT
Chapter 1 - 55
Exercise
public class Person {
// fields
String name;
int age;
// one method
void birthday () {
age = age + 1;
System.out.println(HBD! You are + age +years old now);
}
}
ECEg-3142 JiT
Chapter 1 - 56
Chapter 1 - 57
ECEg-3142 JiT
Chapter 1 - 58
Customer
Order
Product
ECEg-3142 JiT
Chapter 1 - 59
Objects
Are the instances of the class
ECEg-3142 JiT
Chapter 1 - 60
ECEg-3142 JiT
Chapter 1 - 61
Example of a class
public class Employee {
// Fields
public String name;
private double salary;
// Constructor (special type of method)
public Employee(){}
public Employee(String n, double s) {
name = n;
salary = s;
}
// Methods
void pay () {
//is it public or Private???
System.out.println("Pay to the order of " +
name + " $" + salary);
}
public String getName() { return name; }
}
ECEg-3142 JiT
Chapter 1 - 62
Chapter 1 - 63
secretary.pay ();
ECEg-3142 JiT
Chapter 1 - 64
<Class Name>
Example:
Account
We
Weuse
useaarectangle
rectangleto
to
represent
a
class
with
represent a class with
its
itsname
nameappearing
appearing
inside
the
inside therectangle.
rectangle.
Motorcycle
Chapter 1 - 65
<Object Name>
We
Weuse
useaarectangle
rectangleto
to
represent
an
object
and
represent an object and
place
placethe
theunderlined
underlined
name
of
the
name of theobject
objectinside
inside
the
rectangle.
the rectangle.
Example:
SV198
This
Thisisisan
anobject
objectnamed
named
SV198.
SV198.
ECEg-3142 JiT
Chapter 1 - 66
This
Thisnotation
notationindicates
indicates
the
class
which
the class whichthe
the
object
is
an
instance.
object is an instance.
Example:
SV198 : BankAccount
This
Thistells
tellsan
anobject
object
SV198
is
an
instance
SV198 is an instanceof
of
the
BankAccount
class.
the BankAccount class.
ECEg-3142 JiT
Chapter 1 - 67
ECEg-3142 JiT
68
ECEg-3142 JiT
Chapter 1 - 69
Fields
Declaration syntax:
private type name;
Example:
Chapter 1 - 70
//methods
public void setPoint(int xpos, int ypos)
{
x= xpos;
y= ypos;
}
ECEg-3142 JiT
ECEg-3142 JiT
Chapter 1 - 72
ECEg-3142 JiT
Chapter 1 - 73
ECEg-3142 JiT
Chapter 1 - 74
ECEg-3142 JiT
Chapter 1 - 75
Chapter 1 - 76
Instance methods
Example:
public void tranlate(int dx, int dy) {
x += dx;
y += dy;
}
ECEg-3142 JiT
Chapter 1 - 77
Categories of methods
accessor:
object state.
ECEg-3142 JiT
Chapter 1 - 78
Message / Receiver
Objects are manipulated by sending them
messages
The object itself is therefore a receiver of messages
The message is usually a method invocation
Example:
String name = jhon java;
name.reverse();
// name is receiver, reverse is the message
ECEg-3142 JiT
Chapter 1 - 79
Sending a Message
Message
Messagedeposit
depositwith
with
the
argument
250.00
the argument 250.00isis
sent
sentto
toaaBankAccount
BankAccount
object
SV198.
object SV198.
deposit 250.00
SV198 : BankAccount
ECEg-3142 JiT
Chapter 1 - 80
Ask
Askfor
forthe
thecurrent
current
balance
of
this
balance of this
particular
particularaccount.
account.
getCurrentBalance()
SV198 : BankAccount
current balance
The
Thecurrent
currentbalance
balanceof
of
SV198
is
returned.
SV198 is returned.
ECEg-3142 JiT
Chapter 1 - 81
MobileRobot
getMaximumSpeed()
maximum speed
ECEg-3142 JiT
Chapter 1 - 82
ECEg-3142 JiT
Chapter 1 - 83
SV129 : BankAccount
SV098 : BankAccount
SV211 : BankAccount
current balance
908.55
current balance
1304.98
current balance
All
Allthree
threeBankAccount
BankAccount
objects
possess
objects possessthe
the
same
instance
data
same instance data
value
valuecurrent
currentbalance.
balance.
354.00
The
Theactual
actualdollar
dollar
amounts
are,
amounts are,of
ofcourse,
course,
different.
different.
ECEg-3142 JiT
Chapter 1 - 84
There
Thereisisone
onecopy
copyofof
minimum
minimumbalance
balancefor
for
the
whole
class
and
the whole class and
shared
sharedby
byall
allinstances.
instances.
This
Thisline
lineisisan
an
instance-of
instance-of
relationship.
relationship.
SV129 : BankAccount
SV098 : BankAccount
SV211 : BankAccount
current balance
908.55
current balance
1304.98
current balance
354.00
ECEg-3142 JiT
Chapter 1 - 85
SV129 : BankAccount
minimum balance
100.00
current balance
908.55
When
Whenthe
theclass
classicon
iconisis
not
notshown,
shown,we
weinclude
include
the
class
data
value
the class data valueinin
the
theobject
objecticon
iconitself.
itself.
ECEg-3142 JiT
Chapter 1 - 86
Information Hiding
Hiding the internal structure of objects, protecting them from
corruption
Identity
Unique reference for each object
Persistent objects
Defined as available for use over time
ECEg-3142 JiT
Chapter 1 - 87
Inheritance
Inheritance is a mechanism in OOP to design two
or more entities that are different but share many
common features.
Features common to all classes are defined in the
superclass.
The classes that inherit common features from the
superclass are called subclasses.
We also call the superclass an ancestor and the subclass a
descendant.
ECEg-3142 JiT
Chapter 1 - 88
Inheritance
inheritance: Forming new classes based on existing
ones.
a way to share/reuse code between two or more classes
superclass: Parent class being extended.
subclass: Child class that inherits behavior from superclass.
gets a copy of every field and method from superclass
ECEg-3142 JiT
Chapter 1 - 89
Inheritance syntax
public class name extends superclass {
Example:
public class Lawyer extends Employee {
...
}
Chapter 1 90
A Sample Inheritance
Here are the superclass Account and its
subclasses Savings and Checking.
Account
Checking
Savings
ECEg-3142 JiT
Chapter 1 - 91
Inheritance Hierarchy
An example of inheritance hierarchy among
different types of students.
Student
Graduate
Masters
Doctoral
Undergrad
Law
Commuting
Resident
ECEg-3142 JiT
Chapter 1 - 92
ECEg-3142 JiT
Chapter 1 - 93
Container
Panel
ScrollPane
Window
Dialog
Frame
FileDialog
A FileDialog is a Dialog is a Window is a Container
ECEg-3142 JiT
Chapter 1 - 94
Chapter 1 - 95
Example of inheritance
class Person {
String name;
int age;
void birthday () {
age = age + 1;
}
}
class Employee
extends Person {
double salary;
void pay () { ...}
}
Chapter 1 - 96
ECEg-3142 JiT
Chapter 1 - 97
// ok
// ok
//ok
// illegal
//runtime check
ECEg-3142 JiT
Chapter 1 - 98
Chapter 1 - 99
ECEg-3142 JiT
Software Engineering
Much like building a skyscraper, we need a
disciplined approach in developing complex
software applications.
Software engineering is the application of a
systematic and disciplined approach to the
development, testing, and maintenance of a
program.
In this class, we will learn how to apply sound
software engineering principles when we develop
sample programs.
ECEg-3142 JiT
Chapter 1 - 101
Analysis
Design
Coding
Testing
Operation and Maintenance
ECEg-3142 JiT
Chapter 1 - 102