A healthy diet for your Java application Devoxx France.pdf
COMSATS OOPS Course Overview
1. 10/30/2015 COMSATS Institute of Information Technology, Abbottabad OOPS 1
CSC 241: Object Oriented Programming
Instructor Mr.Sharifullah Durrani
(durranisharifullah@gmail.com)
Text books and notes1. Java How to Program, by Deitel & Deitel, 9th
edition.
2.Class Slides
Additional books
Absolute Java fifth Edition by Walter Savitch
Beginning Java 2 by Ivor Horton
Thinking in Java, by Bruce Eckel
Java The complete reference, by Herbert Schildt, 8th edition
Object Oriented Programming, by Ebalagurusammy
3. 10/30/2015 COMSATS Institute of Information Technology, Abbottabad OOPS 3
Course objectives
The ultimate objective of this course is to make students
familiar with the concepts of object-oriented
programming, analysis, and software development. These
concepts will be reinforced by their implementation in
Java programming language.
4. 10/30/2015 COMSATS Institute of Information Technology, Abbottabad OOPS 4
Assignments/Lab Tasks
Please check the CU Online subject page every day, for
notification of assignments, projects and updated information.
Assignments may be written assignments OR programming
assignments.
There will be a total of around 6 assignments.
The deadline for the submission of assignment will be given with
the assignment.
Assignments submitted after the deadline will not be accepted and
will carry ZERO MARKS.
Cheated assignments will get ZERO MARKS.
5. 10/30/2015 COMSATS Institute of Information Technology, Abbottabad Digital Image Processing CSC650 5
Class Project
Projects will have ~? % weight in the total marks ( later we can
discuss).
Projects may be conducted in groups of two-three students.
Project topics should be selected and approved within the first few
weeks of the course.
Project presentation date will be announced and projects will not
be accepted after the presentation date.
Projects consisting of Downloaded codes or presentations will not
be accepted and will carry ZERO MARKS.
7. Java is the world’s most widely used computer
programming language.
You’ll learn to write instructions commanding
computers to perform tasks.
Software (i.e., the instructions you write) controls
hardware (i.e., computers).
You’ll learn object-oriented programming—today’s
key programming methodology.
8. Java Editions: SE, EE and ME
◦ Covers Java Standard Edition 8
◦ Used for developing cross-platform, general-purpose
applications.
◦ Java is used in such a broad spectrum of applications that it has
two other editions.
◦ The Java Enterprise Edition (Java EE)
◦ Geared toward developing large-scale, distributed networking
applications and web-based applications.
9. ◦ Java Micro Edition (Java ME)
◦ geared toward developing applications for small, memory-
constrained devices, such as BlackBerry smartphones.
◦ Google’s Android operating system
11. Hardware
◦ the physical, tangible parts of a computer
◦ keyboard, monitor, disks, wires, chips, etc.
Software
◦ programs and data
◦ a program is a series of instructions
A computer requires both hardware and
software
Each is essentially useless without the other
1-
11
15. Operating System
◦ controls all machine activities
◦ provides the user interface to the computer
◦ manages resources such as the CPU and memory
◦ Windows XP, Unix, Linux, Mac OS
Application program
◦ generic term for any other kind of software
◦ word processors, missile control systems, games
Most operating systems and application
programs have a graphical user interface (GUI)
1-
15
16. Programmers write instructions in various
programming languages,
◦ directly understandable
◦ some requiring intermediate translation steps.
These may be divided into three general types:
◦ Machine languages
◦ Assembly languages
◦ High-level languages
17. Any computer can directly understand only its own machine
language, defined by its hardware design.
◦ Generally consist of strings of numbers (ultimately reduced to 1s and 0s)
that instruct computers to perform their most elementary operations one
at a time.
◦ Machine dependent—a particular machine language can be used on
only one type of computer.
English-like abbreviations that represent elementary operations
formed the basis of assembly languages.
Translator programs called assemblers convert early assembly-
language programs to machine language.
18. High-level languages
Allow you to write instructions that look almost like everyday English and
contain commonly used mathematical notations.
◦ Compilers convert high-level language programs into machine language.
◦ grossPay = basePay + overTimePay
Compiling a high-level language program into machine language
can take a considerable amount of computer time.
Interpreter programs execute high-level language programs directly,
although slower than compiled programs run.
19. Object-oriented programming (OOP) is a way to
organize and conceptualize a program as a set of
interacting objects.
The programmer defines the types of objects that will
exist.
The programmer creates object instances as they are
needed.
The programmer specifies how these various object
will communicate and interact with each other.
20. Real-world objects have attributes and behaviors.
Examples:
Dog
◦ Attributes: breed, color, hungry, tired, etc.
◦ Behaviors: eating, sleeping, etc.
Bank Account
◦ Attributes: account number, owner, balance
◦ Behaviors: withdraw, deposit
21. Writing software often involves creating a
computational model of real-world objects and
processes.
Object-oriented programming is a methodology that
gives programmers tools to make this modeling process
easier.
Software objects, like real-world objects, have attributes
and behaviors.
22. In object-oriented languages,
they are defined together.
◦ An object is a
collection of attributes
and the behaviors that
operate on them.
Variables in an object are
called attributes.
Procedures associated with
an object are called methods.
In traditional programming languages (Fortran, Cobol, C, etc)
data structures and procedures are defined separately.
Account
Account
Account
balance:
number:
Bank
deposit()
withdraw()
23. The definitions of the attributes and methods of an object are
organized into a class. Thus, a class is the generic definition
for a set of similar objects (i.e. Person as a generic definition
for Ali, Ahmed and Sulman)
A class can be thought of as a template used to create a set of
objects.
A class is a static definition; a piece of code written in a
programming language.
One or more objects described by the class are instantiated at
runtime.
The objects are called instances of the class.
24. Each instance will have its own distinct set of
attributes.
Every instance of the same class will have the same set
of attributes;
◦ every object has the same attributes but,
◦ each instance will have its own distinct values for those
attributes.
25. The "account" class
describes the attributes
and behaviors of bank
accounts.
The “account” class
defines two state variables
(account number and
balance) and two methods
(deposit and withdraw).
class: Account
deposit()
withdraw()
balance:
number:
26. When the program runs
there will be many
instances of the account
class.
Each instance will have its
own account number and
balance (object state)
Methods can only be
invoked .
balance: $240
number: 712
balance: $941
number: 036
balance: $19
number: 054
Instance #1
Instance #2
Instance #3
27. Messages and Methods Calls
◦ When you drive a car, pressing its gas pedal sends a message
to the car to perform a task—that is, to go faster.
◦ Similarly, you send messages to an object.
◦ Each message is implemented as a method call that tells a
method of the object to perform its task.
Reuse
◦ Reuse of existing classes when building new classes and
programs saves time and effort.
◦ Reuse also helps you build more reliable and effective systems,
because existing classes and components often have gone
through extensive testing, debugging and performance tuning.
28. ◦ How will you create the code (i.e., the program instructions)
for your programs?
◦ Follow a detailed analysis process for determining your
project’s requirements (i.e., defining what the system is
supposed to do)
◦ Develop a design that satisfies them (i.e., deciding how the
system should do it).
◦ Carefully review the design (and have your design reviewed by
other software professionals) before writing any code.
29. ◦ Analyzing and designing your system from an object-oriented
point of view is called an object-oriented analysis and design
(OOAD) process.
◦ Languages like Java are object oriented.
◦ Object-oriented programming (OOP) allows you to implement
an object-oriented design as a working system.
The UML (Unified Modeling Language)
◦ The Unified Modeling Language (UML) is the most widely
used graphical scheme for modeling object-oriented systems.
30. 1991
◦ Sun Microsystems funded an internal corporate research
project led by James Gosling, which resulted in a C++-based
object-oriented programming language Sun called Java.
◦ Key goal of Java is to be able to write programs that will run
on a great variety of computer systems and computer-control
devices.
◦ This is sometimes called “write once, run anywhere.”
31. Sun Microsystems was acquired by Oracle in 2009.
As of 2010 97% of enterprise desktops, three billion
handsets, and 80 million television devices run Java.
Java is the most widely used software development
language in the world.
Java Class Libraries
◦ Rich collections of existing classes and methods
◦ Also known as the Java APIs (Application Programming
Interfaces).
32. Java programs normally go through five
phases
◦ edit
◦ compile
◦ load
◦ verify
◦ Execute.
33. Phase 1 consists of editing a file
◦ Type a Java program (source code) using the editor.
◦ Make any necessary corrections.
◦ Save the program.
◦ A file name ending with the .java extension indicates that the
file contains Java source code.
34. Integrated development environments (IDEs)
Provide tools that support the software development process,
including editors for writing and editing programs and
debuggers for locating logic errors—errors that cause
programs to execute incorrectly.
Popular IDEs
Eclipse (www.eclipse.org)
NetBeans (www.netbeans.org).
jGRASP™ IDE (www.jgrasp.org)
DrJava IDE (www.drjava.org/download.shtml)
BlueJ IDE (www.bluej.org/)
TextPad® Text Editor for Windows®
(www.textpad.com/)
35. Phase 2: Compiling a Java Program into Bytecodes
◦ Use the command javac (the Java compiler) to compile a
program.
◦ For example, to compile a program called Welcome.java
javac Welcome.java
◦ If the program compiles, the compiler produces a .class file
called Welcome.class that contains the compiled version
of the program.
36. Java compiler translates Java source code into bytecodes
that represent the tasks to execute.
Bytecodes are executed by the Java Virtual Machine (JVM)
◦ a part of the JDK and the foundation of the Java platform.
Virtual machine (VM)—a software application that
simulates a computer
◦ Hides the underlying operating system and hardware from the
programs that interact with it.
37. Bytecodes are platform independent
◦ They do not depend on a particular hardware platform.
Bytecodes are portable
◦ The same bytecodes can execute on any platform containing a JVM
that understands the version of Java in which the bytecodes were
compiled.
The JVM is invoked by the java command. For example, to
execute a Java application called Welcome, you’d type the
command
java Welcome
38. Phase 3: Loading a Program into Memory
◦ The JVM places the program in memory to execute it—this is
known as loading.
◦ Class loader takes the .class files containing the program’s
bytecodes and transfers them to primary memory.
◦ Also loads any of the .class files provided by Java that your
program uses.
39.
40. Phase 4: Bytecode Verification
◦ As the classes are loaded, the bytecode verifier examines their
bytecodes
◦ Ensures that they’re valid and do not violate Java’s security
restrictions.
Java enforces strong security to make sure that Java
programs arriving over the network do not damage
your files or your system (as computer viruses and
worms might).
41.
42. Phase 5: Execution
◦ The JVM executes the program’s bytecodes.
◦ JVMs typically execute bytecodes using a combination of
interpretation and so-called just-in-time (JIT) compilation.
◦ A just-in-time (JIT) compiler—known as the Java HotSpot
compiler—translates the bytecodes into the underlying computer’s
machine language.
43. ◦ When the JVM encounters these compiled parts again, the
faster machine-language code executes.
Java programs go through two compilation phases
1- Source code is translated into bytecodes
2- during execution, the bytecodes are translated into machine
language for the actual computer on which the program
executes.
44. In the Java programming language:
◦ A program is made up of one or more classes
◦ A class contains one or more methods
◦ A method contains program statements
A Java application always contains a method called
main
1-
44
46. 1-
46
public class MyProgram
{
}
// comments about the class
public static void main (String[] args)
{
}
// comments about the method
method header
method body
47. Comments in a program are called inline
documentation
They should be included to explain the purpose of
the program and describe processing steps
They do not affect how a program works
Java comments can take three forms:
1-
47
// this comment runs to the end of the line
/* this comment runs to the terminating
symbol, even across line breaks */
/** this is a javadoc comment */
48. 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 - MyClass
◦ upper case for constants - MAXIMUM
1-
48
49. Sometimes we choose identifiers ourselves when
writing a program (such as MyClass)
Sometimes we are using another programmer's
code, so we use the identifiers that he or she
chose (such as println)
Often we use special identifiers called reserved
words that already have a predefined meaning in
the language
A reserved word cannot be used in any other way
1-
49
50. The Java reserved words:
1-
50
abstract
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
51. Spaces, blank lines, and tabs are called
white space
White space is used to separate words and
symbols in a program
Extra white space is ignored
A valid Java program can be formatted many
ways
Programs should be formatted to enhance
readability, using consistent indentation
1-
51
52. The syntax rules of a language define how we
can put together symbols, reserved words, and
identifiers to make a valid program
The semantics of a program statement define
what that statement means (its purpose or role in
a program)
A program that is syntactically correct is not
necessarily logically (semantically) correct
A program will always do what we tell it to do,
not what we meant to tell it to do
1-
52
53. 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
A problem can occur during program execution, such
as trying to divide by zero, which causes a program
to terminate abnormally (run-time errors)
A program may run, but produce incorrect results,
perhaps using an incorrect formula (logical errors)
1-
53
54. import java.util.Scanner;
/**
*
* @author jadoon
*/
public class HelloWorldApp {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in); //is used to take input
// TODO code application logic here
System.out.println("Welcome to my first java program");
System.out.println("Please Enter In Your First Name: ");
String firstName = reader.nextLine();
1-
54
55. System.out.println("Please Enter In The Year You Were Born: ");
String bornYear = reader.nextLine();
System.out.println("Please Enter In The Current Year: ");
String thisYear = reader.nextLine();
int bYear = Integer.parseInt(bornYear);//convert data type
int tYear = Integer.parseInt(thisYear);
int yold=tYear-bYear;
System.out.println("Hello "+ firstName +" your age is " + yold);
}//main method
}//class
1-
55
56. public class HelloWorld {
public static void main(String[] args) {
// Display the string.
System.out.println("Hello World!");
System.out.println("My first java program");
}
}
Save HelloWorld.java
Open cmd
SET PATH=%PATH%;C:JDK1.2BIN
Javac HelloWorld.java
Java HelloWorld