You are on page 1of 41

DCS 115

Object Oriented Programming


with Java
By
Salamudeen Alhassan
Introduction to Java
Overview of Object-Oriented
Programming
 What Is Programming?
 Developing instructions for carrying out a task
involving a set of objects.
 Computer
 A programmable device that can store,
retrieve, and process data.
 Data
 Information in a form that a computer can use
(e.g. letters, words, integer numbers, real
numbers, dates, times, coordinates on a map, e.c.t.)
Overview of Object-Oriented
Programming
 Information
 Any knowledge that can be communicated including

abstract ideas and concepts such as “the Earth is


round.”
 Object
 A collection of data values and associated Operations.

Virtually any kind of information can be represented


as an object.
 Computer program (computer programming)
 Instructions defining a set of objects and

orchestrating their interactions to solve a problem


How Do We Write a Program?
 A computer is not intelligent. It cannot analyze a
problem and come up with a solution.
 A human (the programmer) must analyze the

problem, develop the objects and instructions for


solving the problem, and then have the computer
carry out the instructions.
 What’s the advantage of using a computer if it can’t

solve problems? Once we have written a solution for


the computer, the computer can repeat the solution
very quickly and consistently, again and again.
How Do We Write a Program?
 In this way, the computer frees people from
repetitive and boring tasks.
 To write a program for a computer to follow,

we must go through a two phase process:


◦ Problem solving
◦ implementation
How Do We Write a Program?
How Do We Write a Program?
 Problem-Solving Phase
◦ Analysis and specification. Understand
(define) the problem and identify what the
solution must do.
◦ General solution (algorithm). Specify the
objects and their interactions that solve the
problem.
◦ Verify. Follow the steps exactly to see if the
solution really does solve the problem.
How Do We Write a Program?
 Implementation Phase
◦ Concrete solution (program).Translate the object
specifications and algorithms (the general solution) into
a programming language.
◦ Test. Have the computer carry out the program and then
check the results. If you find errors, analyze the
program and the general solution to determine the
source of the errors, and then make corrections.
 Once a program has been written, it enters a third

phase: maintenance.
How Do We Write a Program?
 Maintenance Phase
◦ Use. Use the program.
◦ Maintain. Modify the program to meet changing
requirements or to correct any errors that show up
in using it.
How Do We Write a Program?
 Class
 A description of the representation of a specific kind of
object, in terms of data and operational behaviours.

 Algorithm
 Instructionsfor solving a problem in a finite amount of
time using a finite amount of data.
 A program is an algorithm that is written for a computer.

Java programs are generally referred to as applications


How Do We Write a Program?
 Programming language
 A set of rules, symbols, and special words used to
construct a computer program.
 A programming language is a simplified form of

English (with math symbols) that adheres to a strict


set of grammatical rules.
 Code
 Instructions for a computer that are written in a
programming language.
 Translating an algorithm into a programming

language is called coding the algorithm.


How Do We Write a Program?
How Do We Write a Program?
How Do We Write a Program?
 Documentation
◦ The written text and comments that make an
application easier for others to understand, use, and
modify
How Is Java Code Converted into a
Form That a Computer Can Use?
 All data in a computer are stored and used in binary
codes, consisting of strings of 1s and 0s.
 Instructions and data are stored together in the
computer’s memory using these binary codes.
 Machine language
 The language, made up of binary coded instructions, that
is used directly by the computer.
 Assembly language
 A low level programming language in which a mnemonic
represents each machine language instruction for a
particular computer.
How Is Java Code Converted into a
Form That a Computer Can Use?
 Typical instructions for addition and subtraction
might look like this:

Assembly Language Machine Language


ADD 100101
SUB 010011
How Is Java Code Converted into a
Form That a Computer Can Use?
 Although humans find it easier to work with
assembly language, the computer cannot directly
execute the instructions. Because a computer can
process its own instructions as a form of data, it is
possible to write a program to translate assembly
language instructions into machine code. Such a
program is called an assembler.
How Is Java Code Converted into a
Form That a Computer Can Use?
 Assembly language represents a step in the right
direction, but it still forces programmers to think in
terms of individual machine instructions.
 Eventually, computer scientists developed high-level

programming languages. These languages are easier


to use than assembly languages or machine code
because they are closer to English and other natural
languages.
How Is Java Code Converted into a
Form That a Computer Can Use?
 Assembler
◦ A program that translates an assembly language program into
machine code.
 A program called a compiler translates algorithms
written in certain high-level languages (Java, C+
+,Visual Basic, and Ada, for example) into machine
language.
 Compiler

◦ A program that translates code written in a high-level language


into machine code.
How Is Java Code Converted into a
Form That a Computer Can Use?
 The text of an algorithm written in a high-level
language is called source code. To the compiler,
source code is just input data—letters and numbers. It
translates the source code into a machine language
form called object code
 Source code

◦ Instructions written in a high-level programming


language
 Object code

◦ A machine language version of a source code.


How Is Java Code Converted into a
Form That a Computer Can Use?

High-Level Programming Language allow


applications to be compiled in different systems
How Is Java Code Converted into a
Form That a Computer Can Use?
 Java source code is translated into a standard machine
language called Bytecode.
 Bytecode

◦ A standard machine language into which Java


source code is compiled.
 No computers actually use Bytecode as their machine

language.
 Instead, for a computer to run Bytecode, it must have

another program called the Java Virtual Machine


(JVM) that serves as a language interpreter for the
Bytecode
How Is Java Code Converted into a
Form That a Computer Can Use?

A Java Compiler Produces Bytecode that Can Be Run on Any


Machine with the JVM
How Is Java Code Converted into a
Form That a Computer Can Use?
 Direct execution of code differs significantly from
interpretation of code.
 A computer can directly execute a program that is

compiled into machine language.


 Direct execution

◦ The process by which a computer performs the


actions specified in a machine language program.
 Interpretation

◦ The translation, while a program is running, of


nonmachine language instructions (such as Bytecode)
into executable operations.
How Is Java Code Converted into a
Form That a Computer Can Use?
 When a program is used to make one computer act
like another computer, we call it a virtual machine.
 Virtual machine

◦ A program that makes one computer act like


another
Instructions that Can Be Written in a
Programming Language
 The instructions in a programming language reflect
the operations a computer can perform:
◦ A computer can transfer data from one place to
another.
◦ A computer can input data from an input device (a
keyboard or mouse, for example) and output data to
an output device (a screen, for example).
◦ A computer can store data into and retrieve data
from its memory and secondary storage (parts of a
computer that we discuss in the next section).
Instructions that Can Be Written in a
Programming Language
 The instructions in a programming language reflect
the operations a computer can perform:
◦ A computer can compare data values for equality or
inequality and make decisions based on the result.
◦ A computer can perform arithmetic operations
(addition and subtraction, for example) very
quickly.
◦ A computer can branch to a different section of the
instructions.
Instructions that Can Be Written in a
Programming Language
 A programming language contains instructions, called
declarations, which we use to specify the data and
operations in classes.
 Programming languages require that we use certain

control structures to organize the instructions that


specify the behaviors of objects.
 Instructions that describe behavior can be organized

in four ways in most programming languages:


sequentially, conditionally, repetitively, and with
subprograms. Java adds a fifth way: asynchronously
Instructions that Can Be Written in a
Programming Language
 A sequence is a series of operations that are executed
one after another.
Instructions that Can Be Written in a
Programming Language
 Selection, the conditional control structure, executes
different operations depending on certain conditions.
Instructions that Can Be Written in a
Programming Language
 The repetitive control structure, the loop, repeats
operations while certain conditions are met.
Instructions that Can Be Written in a
Programming Language
 The subprogram allows us to organize our code into
units that correspond to specific object behaviors;
Java calls these units methods.
Instructions that Can Be Written in a
Programming Language
 Asynchronous control lets us write code that handles
events, such as the user clicking a button on the
screen with the mouse.
Object-Oriented Programming
Languages
 Early programming languages focused their attention
on the operations and control structures of
programming. These procedural languages paid little
explicit attention to the relationships between the
operations and the data.
 At that time, a typical computer program used only

simple data types such as integers and real numbers,


which have obvious sets of operations defined by
mathematics.
 Problem with new data type such as date, time etc.
Object-Oriented Programming
Languages
 Data type
◦ The specification in a programming language of
how information is represented in the computer as
data and the set of operations that can be applied
to it.
 Modern programming languages such as Java
allow us to collect data and its associated
operations into objects. For this reason, they
are called object-oriented programming
languages.
Object-Oriented Programming
Languages
 Advantage of an object:
◦ It makes the relationships between the data and
operations explicit.
◦ Each object is a complete, self-contained unit
that can be reused again in other applications.
◦ Reusability enables us to write a
significant portion of our code using
existing objects, thereby saving a
considerable amount of time and effort.
Object-Oriented Programming
Languages
 A class is a description of an object. When we need
an object in an application, we must create one from
such a description. Java provides an operation to let
us do so, and we say that the operation instantiates
the class.
 Instantiate

◦ To create an object based on the description


supplied by a class.
Object-Oriented Programming
Languages
 One characteristic of an object-oriented programming
language is the presence of a large library of classes.
Within the library, classes are usually collected into
groups called packages.
 Package

◦ A collection of related classes


Problem-Solving Techniques
 We solve problems everyday
 learning environment, we usually are given most of

the information we need: a clear statement of the


problem, the necessary input, and the required output.
 In the problem-solving phase of computer

programming, we actually design algorithms. This


means we must be conscious of the strategies we use
to solve problems so that we can apply them to
programming problems effectively.
Problem-Solving Techniques
 Ask Questions
 Look for Things That Are Familiar
 Solve by Analogy
 Means-Ends Analysis
 Divide and Conquer
 The Building-Block Approach
 Merging Solutions

You might also like