0% found this document useful (0 votes)
5 views

Java Chapter01gutema

Uploaded by

demissie ejo
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Java Chapter01gutema

Uploaded by

demissie ejo
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 20

Dambi Dollo university 2013

Department of Computer science

Object-Oriented Programming Using Java


Compiled by: Gutema Bulcha

1
What is a Computer Program?

For a computer to be able to do


anything (multiply, play a song, run
a word processor), it must be given
the instructions to do so.

A program is a set of instructions


written by humans for computers to
perform tasks.

The instructions are written in


programming languages such as
C, C++, Java, etc.
2
When you write programs, you write program
statements that are instructions that are similar to
English-language sentences. The statements you
write in a programming language must be
subsequently translated into machine language.
Machine language is the language that computers
can understand; it consists of 1s and 0s.

3
Compiling Programs
 Computers do not understand the languages (C++,
Java, etc) that programs are written in.
 Programs must first be compiled (converted) into
machine code that the computer can run.
 A compiler is a program that translates a
programming language into machine code.

4
Running Programs
All programs follow a simple format:
Input Output Execution
 Inputs can be from users, files, or other computer
programs
Outputs can take on many forms: numbers, text,
graphics, sound, or commands to other programs

5
Multiple Compilers
 Because different operating systems (Windows, Macs,
Unix) require different machine code, you must
compile most programming languages separately for
each platform
program

compiler compiler
compiler

Unix
Win
MAC

6
Java Interpreter
Java is a little different.
Java compiler produces bytecode not
machine code.
Bytecode can be run on any computer
with the Java interpreter installed. Win

te r
Java pre
te r
Program Java Bytecode I n
MAC

compiler Interpreter
Inte
rpre
ter Unix

7
A translator program (called either a compiler or an
interpreter) checks your program for syntax errors. If
there are no errors, the translator changes your
written program statements into machine language.
Therefore, syntax errors are not a big problem; you
always have an opportunity to fix them before you
actually attempt to run the program. For example, if
you write a computer program in C++ but spell a
word incorrectly or reverse the required order of two
words, the compiler informs you of such errors and
will not let you run the program until you have
corrected them.
8
Finding logical errors is much more time consuming
for a programmer than finding syntax errors.

A logical error occurs when you use a statement


that, although syntactically correct, doesn’t do what
you intended. For a program that is supposed to add
two numbers and show the sum, logical errors arise
when multiplication is used instead of addition, or
when the sum is given before the arithmetic occurs.
The language compiler will not tell you when you
have committed a logical error; only running and
testing your program will enable you to find
inappropriate statements.
9
Programming universal

All modern programming languages share common


characteristics.
All programming languages provide methods for
sending input the data provided by an outside
source, such as a keyboard, scanner, or file into
computer memory so that a program can manipulate
it.

10
 All programming languages provide a way to name
locations in computer memory. These locations are
commonly called variables. For example, if a person
asks, “What is your Age? ”your Age is considered a
variable for two reasons: your Age has different (varied)
values for different people, and any person can have a
change in the value of your Age. When writing a
computer program, your Age becomes the name of a
position or location in computer memory; the value at
that location or the state of that location might be 18 or
80, or it might be unknown. When discussing the
variable your Age, the separate words “your” and “Age”
are run together on purpose.
11
 A variable may have only one value at a time, but it is
the ability of memory variables to change in value that
makes computers and programming worthwhile.
Because one memory location, or variable, can be used
repeatedly with different values, program instructions
can be written once and then used for thousands of
problems.
 In many computer programming languages, including
C++, variables must be explicitly declared, that is, given
a data type as well as a name, before they can be used.
 The data type of a variable defines what kind of values
may be stored in a variable and what kind of operations
can be performed on it.
12
Most computer languages allow at least two types: one
for numbers and one for characters. Numeric variables
hold values like 13 or -6. Character variables hold
values like ‘A’ or ‘&’.
Many languages include even more specialized types,
such as integer (for storing whole numbers) or floating
point (for storing numbers with decimal places).
The distinction between variable types is important
because computers handle the various types of data
differently; each type of variable requires a different
amount of storage and answers to different rules for
manipulation

13
Procedural programming and object
oriented programming
Procedural programming
Procedural programs consist of a series of steps
or procedures that take place one after the other.
The programmer determines the exact conditions
under which a procedure takes place, how often it
takes place, and when the program stops.
Programmers write procedural programs in many
programming languages, such as COBOL,BASIC,
and FORTRAN. You can also write procedural
programs in C++.

14
Procedural programming

Although each language has a different syntax,


they all share many element. Over the years, as
programmers have sought better ways to
accommodate the way people work best on
computers, procedural programming techniques
have evolved into object oriented techniques.
Some older languages do not support object-
oriented techniques, but several newer languages
do, including Visual Basic, Java, C#, and C++.

15
Object oriented programming

 object-oriented programming requires a different


way of thinking and adds several new concepts to
programming:
 You analyze the objects with which you are working
both the attributes of those objects and the tasks
that need to be performed with and on those
objects. You pass messages to objects, requesting the
objects to take action. The same message works
differently (and appropriately) when applied to the
various objects.

16
Object oriented programming

A method can work appropriately with different


types of data it receives, without the need for
separate method names. Objects can assume or
inherit traits of previously created objects, thereby
reducing the time it takes to create new objects.
 The basic principles behind using object-oriented
programming techniques involve: Objects, Classes,
Inheritance Polymorphism.
Each of these principles is complex.

17
OOP Approach
A modern programming paradigm that allows the
Programmer to model a problem in a real-world fashion as an
object
Major objective is to eliminate some of the flaw (error)
encountered in the procedural approach
OOP allows us to decompose a problem into number of
entities called objects and then build data and methods
(functions) around these entities
The data of an object can be accessed only by the methods
associated with the object
Follows bottom-up approach in program design

18
Features of OOP

Emphasis is on data rather than procedure.


Programs are divided into objects.
Data Structures are designed such that they
characterize the objects.
Methods that operate on the data of an object are
tied together in the data structure.
Data is hidden and cannot be accessed by
external functions.
Objects may communicate with each other
through methods.
19
20

You might also like