Java Chapter01gutema
Java Chapter01gutema
1
What is a Computer Program?
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.
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
15
Object oriented programming
16
Object oriented programming
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