0% found this document useful (0 votes)
113 views44 pages

Object Oriented Programming: Dr. Madiha Liaqat Assistant Professor SED, UET, Taxila

The document discusses an object-oriented programming course taught by Dr. Madiha Liaqat. It covers topics like Java history, features, and the process for creating and running Java programs. Students will learn object-oriented programming concepts using Java and complete assignments, a midterm exam, and final exam that make up the overall grading criteria.

Uploaded by

saba doll
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views44 pages

Object Oriented Programming: Dr. Madiha Liaqat Assistant Professor SED, UET, Taxila

The document discusses an object-oriented programming course taught by Dr. Madiha Liaqat. It covers topics like Java history, features, and the process for creating and running Java programs. Students will learn object-oriented programming concepts using Java and complete assignments, a midterm exam, and final exam that make up the overall grading criteria.

Uploaded by

saba doll
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 44

OBJECT ORIENTED

PROGRAMMING

Dr. Madiha Liaqat


Assistant Professor
SED, UET, Taxila
TEXT BOOKS
• The Complete Reference Java 2 by Herbert
Schildt latest edition
GRADING CRITERIA
 Quiz 15%
 Assignment 15%
 Mid Exam 30%
 Final Exam 40%
COURSE LEARNING OUTCOMES

PROGRAM
COURSE LEARNING OUTCOME (CLO) LEARNING
OUTCOME (PLO)
1.Describe the concepts of object-oriented paradigm. 1
2.Examine the programs to comprehend their structures. 2

3.Apply appropriate techniques to write programs 3


TOPICS TO BE COVERED TODAY
 What is a Program?
 Why Java

 Java History

 Versions of Java

 Features of Java

 Java Procedure

 Creating Simple Java Program


WHAT IS A PROGRAM?
 A computer program (also a software program, or just a
program) is a sequence of instructions written to perform a
specified task for a computer.
THE EVOLUTION OF PROGRAMMING
LANGUAGES
 Machine language: 1940’s
 Assembly language: early 1950’s

 Higher-level languages: late 1950’s


 Fortran:scientific computation
 Cobol: business data processing
 Lisp: symbolic computation

 Today: thousands of programming languages


WHAT IS PROGRAMMING?
 When we say “programming” we are actually
referring to the science of transforming our
intentions in a high-level programming language.
WHAT ARE WE DOING IN THIS COURSE?
 Learn programming in a high-level programming
language.
 You will study Object-Oriented Programming using
‘Java’, a popular high-level object-oriented
programming language.
 You’ll be exposed to fundamental programming
concepts, including object-oriented programming
(OOP) using Java. You’ll build different Java
projects—like a basic calculator—to help you
practice along the way.
WHY JAVA?
 It’s the current “hot” language
 Java is among the most popular programming languages out there, mainly
because of how versatile and compatible it is. Java can be used for a large
number of things, including software development, mobile applications, and
large systems development. As of 2019, 88% market share of all smartphones run
on Android, the mobile operating system written in Java. Knowing Java opens a
great deal of doors for you as a developer.
 Java mean Cup of coffee
 It’s almost entirely object-oriented
 It has a vast library of predefined objects and operations
 It’s more platform independent
 this makes it great for Web programming
 It’s more secure
 It isn’t C++
JAVA HISTORY
 Java
 was created in 1991
 by James Gosling et al. of Sun Microsystems.
 Initially called Oak, in honor of the tree outside
Gosling's window, its name was changed to Java
because there was already a language called
Oak.
JAVA HISTORY CONTD. . . . .
 The term Java actual refers to more than just a
particular language like C or Pascal. Java encompasses
several parts, including :
 A high level language - the Java language is a high level one
that at a glance looks very similar to C and C++ but offers many
unique features of its own.

 Java bytecode - a compiler, such as Sun's javac, transforms the


Java language source code to bytecode that runs in the JVM.

 Java Virtual Machine (JVM) a program, such as Sun's java,


that runs on a given platform and takes the bytecode programs as
input and interprets them just as if it were a physical processor
executing machine code.
JAVA HISTORY CONTD. . . . .
 Sun provides a set of programming tools such as javac,
java and others in a bundle that it calls a Java Software
Development Kit for each version of the language and
for different platforms such as Windows, Linux, etc..
Sun also provides a runtime bundle with just the JVM
when the programming tools are not needed.
VERSIONS OF JAVA
 Since its introduction, Sun has released a new version of
the Java language every two years or so.
 These new versions brought enhancements, new
capabilities and fixes to bugs.
 Until recently, the versions were numbered 1.x, where x
reached up till 4. (Intermediate revisions were labeled
with a third number - 1.x.y - as in 1.4.2.)
 The newest version, however, is called Java 5.0 rather
than Java 1.5.
VERSIONS OF JAVA
FEATURES OF JAVA
 Simple- Java is designed to be easy to learn. If you
understand the basic concept of OOP Java, it would
be easy to master.
 Object Oriented − In Java, everything is an Object.
Java can be easily extended since it is based on
the Object model.
 robust – restricts the programmer to find the
mistakes early, performs compile-time (strong
typing) and run-time (exception-handling) checks.
FEATURES OF JAVA
 multithreaded – With Java's multithreaded feature
it is possible to write programs that can perform
many tasks simultaneously. This design feature
allows the developers to construct interactive
applications that can run smoothly.
 architecture-neutral – Java Virtual Machine
provides a platform independent environment for
the execution of Java bytecode
 interpreted and high-performance – Java
programs are compiled into an intermediate
representation – bytecode:
 can be later interpreted by any JVM
 can be also translated into the native machine code for
efficiency.
FEATURES OF JAVA
 distributed – Java is designed for the distributed
environment of the internet. Distributed computing
allowed objects on two different computers to
execute procedures remotely.
 dynamic – The linking of data and methods to
where they are located, is done at run-time.
 New classes can be loaded while a program is running.
Linking is done on the fly.
 Even if libraries are recompiled, there is no need to
recompile code that uses classes in those libraries.
This differs from C++, which uses static binding.
JAVA PROCEDURE
 The essential steps to creating and running Java
programs go as follows:
 Create a Java source code file
 Compile the source code
 Run the compiled code in a Java Virtual Machine.
STEPS FOR CREATING AND RUNNING A
JAVA PROGRAM
JAVA PROCEDURE DETAIL
 You create the code with a text editor (for example
notepad) and save it to a file with the ".java" suffix.
 All Java source code files must end with this type
name.
 The first part of the name must match the class
name in the source code.
 In the figure the class name is Test so you must
therefore save it to the file name Test.java.
JAVA PROCEDURE DETAIL
 With the javac program, you compile this file as follows:
C:> javac Test.java
 This creates a bytecode file (or files if the code file included
more than one class) that ends with the ".class" type appended.
◦ Here the output is Test.class.
◦ The bytecode consists of the instructions for the Java Virtual
Machine (JVM or just VM).
JAVA PROCEDURE DETAIL
 The JVM is an interpreter program that emulates a
processor that executes the bytecode instructions just as
if it were a hardware processor executing native
machine code instructions.
◦ The Java bytecode can then run on any platform in which the
JVM is available and the program should perform the same.
◦ This Write Once, Run Anywhere approach is a key goal of the
Java language.
ANOTHER EXAMPLE WITH BYTECODE
REPRESENTATION
Test.java

Compile it with
> javac Test.java

Command used for


reading bytecode file

Bytecode
Representation
PROGRAMMING TOOLS
 You can choose from essentially two different programming
environments for Java:
 Manual – with text editor (notepad) create the Java source
code files (*.java) and then use the command line tools in the
Java Software Development Kit (SDK). The SDK is provided by
Sun for several platforms and includes a number of tools, the
most important of which are:
 javac - compiles Java source code files (e.g. Test.java) to

bytecode files (e.g. Test.class) *


 java - runs Java application programs (i.e. implements the

JVM for the Java programs.)



PROGRAMMING TOOLS
 Integrated Development Environment (IDE) - graphical user
interface programming environments (often called GUI Builders)
are elaborate, programs that allow you to interactively build
graphical interfaces, edit the code, execute and run the
applications all within the IDE system. Example Java IDEs include:
 NetBeans

 Borland JBuilder

 Eclipse

 Dr Java
PROGRAMMING TOOLS
Java Environment/ Life Cycle of Java
Code
Runtime
Compile-time Environment
Environment Class Loader
Java Class
Bytecode Libraries
Java Verifier
Source
(.java)

Just in
Java
Time
Java Interpreter Java
Compiler
Bytecodes Virtual
Java move locally machine
Compiler or through
network
Runtime System

Java
Bytecode Operating System
(.class )

Hardware
BYTE CODE
• Java programs are translated into an intermediate
language called bytecode.
• Bytecode is the same no matter which computer
platform it is run on.
• Bytecode is translated into native code that the
computer can execute on a program called the
Java Virtual Machine (JVM).
• The Bytecode can be executed on any computer
that has the JVM. Hence Java’s slogan, “Write
once, run anywhere”.
CLASS LOADER & BYTECODE VERIFIER
 The procedure followed to obtain and install a class goes as follows:
 Loading includes the following tasks:
◦ locate the bytecode file in a disk file
◦ read it into the JVM's method area on the heap.
◦ parse the class data into sections for constants, methods, fields, etc..
◦ create an instance of java.lang.Class to represent the class.

 Linking proceeds through these 3 steps


◦ verification - check that the basic structure and form of the class is proper. Run
bytecode verification.
◦ preparation - allocate memory for the class data such as static variables.
◦ resolution - convert the symbolic references in the class file.
 Initialization
The static variables and constants must be set to either default or user assigned
values.

 ByteCode Verifier is called when class is first loaded in runtime


environment. As part of the class loading process, a thorough verification
of the bytecodes takes place to insure that the file holds a valid Java class
and does not break any of the rules for class behavior.
JIT COMPILER
 In the Java programming language and environment, a just-in-time (JIT) compiler is a
program that turns Java bytecode (a program that contains instructions that must be
interpreted) into instructions that can be sent directly to the processor.
 After you've written a Java program, the source language statements are compiled by
the Java compiler into bytecode rather than into code that contains instructions that
match a particular hardware platform's processor. The bytecode is platform-
independent code that can be sent to any platform and run on that platform. In the
past, most programs written in any language have had to be recompiled, and
sometimes, rewritten for each computer platform. One of the biggest advantages of
Java is that you only have to write and compile a program once.
 The Java on any platform will interpret the compiled bytecode into instructions
understandable by the particular processor. However, the virtual machine handles one
bytecode instruction at a time. Using the Java just-in-time compiler (really a second
compiler) at the particular system platform compiles the bytecode into the particular
system code (as though the program had been compiled initially on that platform).
Once the code has been (re-)compiled by the JIT compiler, it will usually run more
quickly in the computer.
 The just-in-time compiler comes with the virtual machine and is used optionally. It
compiles the bytecode into platform-specific executable code that is immediately
executed.
ASSIGNMENT # 1
 List down Java Compile –time and Run-time errors.
GETTING STARTED
To begin developing Java programs, follow
these steps:
 Step 1: Obtain the Software Development Kit (SDK)
for J2SE (Java 2 Platform, Standard Edition)
 Step 2: Install the SDK
SIMPLE JAVA APPLICATION
SIMPLE APPLICATION
 Step 1: Use an editor to enter the following code for the
HelloWoldApp program:

 Save this code in a file called HelloWorldApp.java


SIMPLE APPLICATION
 Step 2: Compile the application with the command line:
> javac HelloWorldApp.java

 This creates the class file (with the bytecode output):


HelloWorldApp.class
SIMPLE APPLICATION
 Step 3: Use the java command to run the program:
> java HelloWorldApp

Hello World! Output

 The output is printed after the command line.


DESCRIPTION
 The keyword class is used to declare that a new class is being
defined.
 HelloWorldApp is an identifier that is the name of the class.
 The entire class definition, including all of its members, will be
between the opening curly brace ({) and the closing curly brace
(}).
 The use of the curly braces in Java is identical to the way they are
used in C, C++.
 In Java, all program activity occurs within class. This is one reason
why all Java programs are object- oriented.
DESCRIPTION
 The next line of code is

 All Java applications begin execution by calling main( ).


 The public keyword is an access specifier, which allows the
programmer to control the visibility of class members. When a class
member is preceded by public, then that member may be accessed
by code outside the class in which it is declared.
 main( ) must be declared as public, since it must be called by code
outside of its class when the program is started.
DESCRIPTION
 The keyword static allows main( ) to be called without
having to instantiate a particular instance of the class.
This is necessary since main( ) is called by the Java
interpreter before any objects are made.
 The keyword void simply tells the compiler that main( )
does not return a value.
DESCRIPTION
 main( ) is the method called when a Java application
begins. Java is case-sensitive. Thus, Main is different
from main.
 It is important to understand that the Java compiler will
compile classes that do not contain a main( ) method.
But the Java interpreter has no way to run these classes.
So, if you had typed Main instead of main, the compiler
would still compile your program. However, the Java
interpreter would report an error because it would be
unable to find the main( ) method.
DESCRIPTION
 String arg[ ] declares a parameter named arg, which is an
array of instances of the class String. Objects of type String
store character strings.
 In this case, arg receives any command-line arguments present
when the program is executed. This program does not make use
of String arg[ ].
 main( ) is simply a starting place for your program.

 A complex program will have dozens of classes, only one of


which will need to have a main( ) method to get things started.
DESCRIPTION
 The next line of code is shown here

 This line outputs string Hello World!


 System is a predefined class that provides access to the
system, and out is the output stream that is connected to
the console.
 Output is actually accomplished by the built-in println( )
method. println( ) displays the string which is passed to
it.
Questions?

You might also like