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

Chapter Two: Fundamental Programming Structures in Java

This document discusses the fundamentals of object-oriented programming in Java. It covers key characteristics of Java including that it is simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, and dynamic. It also discusses Java integrated development environments and tools for compiling, linking, and debugging code. The document notes that Java can be used to develop applications for various sectors like desktop, server, and mobile devices.

Uploaded by

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

Chapter Two: Fundamental Programming Structures in Java

This document discusses the fundamentals of object-oriented programming in Java. It covers key characteristics of Java including that it is simple, object-oriented, distributed, interpreted, robust, secure, architecture-neutral, portable, and dynamic. It also discusses Java integrated development environments and tools for compiling, linking, and debugging code. The document notes that Java can be used to develop applications for various sectors like desktop, server, and mobile devices.

Uploaded by

Amsalu Fentie
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 64

Object Oriented

Programming (OOP)

CHAPTER TWO

Fundamental Programming
Structures in Java

10/02/22 by: A.F 1


java
• Java is a general purpose object oriented
programming language, it enables users to
develop and deploy applications on the Internet
for servers, desktop computers, and small hand-
held devices.
• by James Gosling and Sun Microsystems
• First name Oak
• Java, May 20, 1995, Sun World
• HotJava is the first Java-enabled Web browser

10/02/22 by: A.F 2


Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Architecture-Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic

10/02/22 by: A.F 3


Characteristics of Java
• Java Is Simple Java is partially modeled on
• Java Is Object-Oriented C++, but greatly simplified and
• Java Is Distributed improved. Some people refer to
• Java Is Interpreted Java as "C++--" because it is
• Java Is Robust like C++ but with more
functionality and fewer
• Java Is Secure
negative aspects.
• Java Is Architecture-
Neutral
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic
10/02/22 by: A.F 4
Characteristics of Java
• Java Is Simple Java is inherently object-
• Java Is Object-Oriented oriented. Although many
• Java Is Distributed object-oriented languages
• Java Is Interpreted began strictly as procedural
• Java Is Robust languages, Java was
• Java Is Secure designed from the start to be
• Java Is Architecture- object-oriented. Object-
Neutral oriented programming
• Java Is Portable (OOP) is a popular
• Java's Performance programming approach that
• Java Is Multithreaded is replacing traditional
• Java Is Dynamic procedural programming
10/02/22
techniques.
by: A.F 5
Characteristics of Java
• Java Is Simple Distributed computing
• Java Is Object-Oriented involves several computers
• Java Is Distributed working together on a
• Java Is Interpreted network. Java is designed to
• Java Is Robust make distributed computing
easy. Since networking
• Java Is Secure
capability is inherently
• Java Is Architecture-
integrated into Java, writing
Neutral
network programs is like
• Java Is Portable sending and receiving data
• Java's Performance to and from a file.
• Java Is Multithreaded
• Java Is Dynamic
10/02/22 by: A.F 6
Characteristics of Java
• Java Is Simple You need an interpreter to
• Java Is Object-Oriented run Java programs. The
• Java Is Distributed programs are compiled into
• Java Is Interpreted the Java Virtual Machine
• Java Is Robust code called bytecode. The
bytecode is machine-
• Java Is Secure
independent and can run on
• Java Is Architecture-
any machine that has a Java
Neutral
interpreter, which is part of
• Java Is Portable the Java Virtual Machine
• Java's Performance (JVM).
• Java Is Multithreaded
• Java Is Dynamic
10/02/22 by: A.F 7
Characteristics of Java
• Java Is Simple Java compilers can detect
• Java Is Object-Oriented many problems that would
• Java Is Distributed first show up at execution
• Java Is Interpreted time in other languages.
• Java Is Robust
Java has eliminated certain
• Java Is Secure
types of error-prone
• Java Is Architecture-
programming constructs
Neutral
found in other languages.
• Java Is Portable Java has a runtime
• Java's Performance exception-handling feature
• Java Is Multithreaded to provide programming
• Java Is Dynamic
10/02/22
support for robustness.
by: A.F 8
Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
Java implements several
• Java Is Robust security mechanisms to
• Java Is Secure protect your system against
• Java Is Architecture- harm caused by stray
Neutral programs.
• Java Is Portable
• Java's Performance
• Java Is Multithreaded
• Java Is Dynamic
10/02/22 by: A.F 9
Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Architecture- Write once, run anywhere
Neutral
• Java Is Portable With a Java Virtual
Machine (JVM), you can
• Java's Performance
write one program that will
• Java Is Multithreaded run on any platform.
• Java Is Dynamic
10/02/22 by: A.F 10
Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Architecture-
Neutral Because Java is architecture
• Java Is Portable neutral, Java programs are
• Java's Performance portable. They can be run
on any platform without
• Java Is Multithreaded
being recompiled.
• Java Is Dynamic
10/02/22 by: A.F 11
Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure Java’s performance Because
• Java Is Architecture- Java is architecture neutral,
Neutral Java programs are portable.
• Java Is Portable They can be run on any
platform without being
• Java's Performance
recompiled.
• Java Is Multithreaded
• Java Is Dynamic
10/02/22 by: A.F 12
Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed
• Java Is Interpreted
• Java Is Robust
• Java Is Secure
• Java Is Architecture- Multithread programming is
Neutral
smoothly integrated in Java,
• Java Is Portable
whereas in other languages you
• Java's Performance
have to call procedures specific
• Java Is Multithreaded
to the operating system to
• Java Is Dynamic
enable multithreading.
10/02/22 by: A.F 13
Characteristics of Java
• Java Is Simple
• Java Is Object-Oriented
• Java Is Distributed Java was designed to adapt
• Java Is Interpreted to an evolving environment.
• Java Is Robust New code can be loaded on
• Java Is Secure the fly without
• Java Is Architecture- recompilation. There is no
Neutral need for developers to
• Java Is Portable create, and for users to
• Java's Performance install, major new software
• Java Is Multithreaded versions. New features can
• Java Is Dynamic be incorporated
transparently as needed.
10/02/22 by: A.F 14
Java IDE( integrated development environment) Tools

• Borland JBuilder
• NetBeans Open Source by Sun
• Sun ONE Studio by Sun MicroSystems
• Eclipse Open Source by IBM

10/02/22 by: A.F 15


Java development environment
Basics about IDEs
First : An environment for text editing

Second : A tool for Compiling and Linking?(the process of


taking one or more object files generated by a compiler and
combines them into a single executable file.)

Example:

•The Java Programming Language Compiler (javac)

•Third : An environment for debugging


10/02/22 by: A.F 16
Java actually has two meaning
I. A programing language:-it is object oriented language whose
syntax is similar to c++.
II. An environment providing all possible service and interface:- the
inventor of the language has also referred to java as a plate form.
Sun has three different editions aimed as different sectors.
a) Java Standard Edition (J2SE) for desktop client or
PC. J2SE can be used to develop client-side
standalone applications or applets.
b) Java Enterprise Edition (J2EE) for application server.
J2EE can be used to develop server-side applications
such as Java servlets and Java ServerPages.
c) Java Micro Edition (J2ME) for miniature devices.
J2ME can be used to develop applications for mobile
devices such as cell phones.
10/02/22 by: A.F 17
Cont..
• In java development kit (JDK), the run time system
(Java Runtime Environment(JRE)) and plate form is in
one.
• JRE consist of virtual machine.

What is virtual machine?

10/02/22 by: A.F 18


Cont…
• A virtual machine (VM) is a software
implementation of a machine (i.e. a computer) that
executes programs like a physical machine
• Virtual machines are separated into two major
categories, based on their use and degree of
correspondence to any real machine
• System virtual machine
• Multiple OS environments can co-exist on
the same computer, in strong isolation from
each other
• Example: VMware workstation

10/02/22 by: A.F 19


Cont..
• Process virtual machine
• A process VM, sometimes called an application
virtual machine
• runs as a normal application inside a host OS
and supports a single process.
• Example: Java Virtual Machine
• A process VM is created when that process is
started and destroyed when it exits
• Its purpose is to provide a platform-independent
programming environment

10/02/22 by: A.F 20


Types of Java Applications
• There are mainly 4 type of applications that can be
created using java programming:
1. Standalone Application: It is also known as
desktop application or window-based application.
An application that we need to install on every
machine such as media player, antivirus etc.
AWT and Swing are used in java for creating
standalone applications.(we will see in chapter 7)
2.Web Application: An application that runs on the
server side and creates dynamic page, is called web
application.

10/02/22 by: A.F 21


Types of Java Applications

3. Enterprise Application: An application


that is distributed in nature, such as banking
applications etc. It has the advantage of high
level security, load balancing and clustering.
4. Mobile Application: An application that is
created for mobile devices.
Example ,Android.

10/02/22 by: A.F 22


Steps in Program Development
1. Define the problem into three separate
components:
• inputs
• outputs
• processing steps to produce required
outputs.
2. Outline the solution.
• Decompose the problem to smaller steps.
• Establish a solution outline.
3. Develop the outline into an algorithm.
• The solution outline is now expanded into an algorithm.

10/02/22 by: A.F 23


Steps in Program Development

4. Test the algorithm for correctness.


• Very important in the development of a program,
but often forgotten
• Major logic errors can be detected and corrected at
an early stage.
5.Code the algorithm into a specific programming
language.
6.Run the program on the computer.
• This step uses a program compiler and programmer-
designed test data to machine-test the code for
• syntax errors
• logic errors
7.Document and maintain the program.
10/02/22 by: A.F 24
Identifiers
 A Java identifier(variables name, class name method
name) are a series of characters consisting of letters,
digits, underscores ( _ ) and dollar signs ($) that does
not begin with a digit and does not contain spaces.

 By convention, all class names in Java begin with a


capital letter and capitalize the first letter of each word
they include (e.g., SampleClassName).

10/02/22 by: A.F 25


• Variables are places where information can be
stored while a program is running.
• Their values can be changed at any point over the
course of a program
• To create a variable, declare its name and the type
of information that it will store.
• Java is a Strongly-Typed Language
• The type is listed first, followed by the name.
• Example:
int highScore ;

type name

10/02/22 by: A.F 26


Variables and Statements

• One way is to declare a variable and then assign a value to it with two
statements:
int e; // declaring a variable
e = 5; // assigning a value to a variable

• Another way is to write a single initialization statement:


int e = 5; // declaring AND assigning

10/02/22 by: A.F 27


Primitive Data Types
• There are eight built-in (primitive) data types in the
Java language
• 4 integer types (byte, short, int, long)
• 2 floating point types (float, double)
• Boolean (boolean)
• Character (char)

float 1.4×10-45 to 3.4×1038


double 4.9×10-324 to 1.7×10308
10/02/22 by: A.F 28
Character Data Types
• Character is a data type that can be used to store
a single characters such as a letter, number,
punctuation mark, or other symbol.

• Example:
• char firstLetterOfName = 'e' ;
• char myQuestion = '?' ;

• Note that you need to use singular quotation


marks when assigning char data types.

10/02/22 by: A.F 29


Introduction to Strings
• Strings consist of a series of characters inside double
quotation marks.

• Examples statements assign String variables:


String coAuthor = "John Smith";
String password = "swordfish786";

• Strings are not one of the primitive data types,


although they are very commonly used.

• Strings are constant; their values cannot be changed


after they are created.
10/02/22 by: A.F 30
Performing Output

• System.out.println( “Hello world!" );


• The System.out object is known as the standard
output object. It allows a Java applications to
display information in the command window from
which it executes.

10/02/22 by: A.F 31


How to add input?
Import Declarations:
import java.util.Scanner;
Scanner input = new Scanner( System.in );
number1 = input.nextInt();
It is a variable declaration statement that specifies
the name (input) and type (Scanner) of a variable
that’s used in this program. A Scanner enables a
program to read data (e.g., numbers and strings) for
use in a program. The data can come from many
sources, such as the user at the keyboard or a file on
disk.
10/02/22 by: A.F 32
input in java

String next()
Finds and returns the next complete token from this
scanner. 
boolean nextBoolean()
Scans the next token of the input into a boolean value
and returns that value. 
byte nextByte()
 Scans the next token of the input as a byte. 
double nextDouble()
 Scans the next token of the input as a double. 
float nextFloat()
Scans the next token of the input as a float
10/02/22 by: A.F 33
Input in java

int nextInt()
Scans the next token of the input as an int. 
String nextLine()
Advances this scanner past the current line and
returns the input that was skipped. 
long nextLong()
Scans the next token of the input as a long. 
short nextShort()
Scans the next token of the input as a short. 
char next().charAt() scans the next token of the input
as char.
10/02/22 by: A.F 34
Comments in java
Comments are ignored by the compiler but
are useful to other programmers.
Java programming language supports three
kinds of comments:
1. Single line comment // comment text
 The compiler ignores everything
from // to the end of the line.

10/02/22 by: A.F 35


Comments in java

Multiple line comment /* comment text */


 The compiler ignores everything from /* to */
3. Documentation comment /** documentation */
This indicates a documentation comment (doc
comment, for short). The compiler ignores this kind of
comment.
 The javadoc tool uses doc comments when preparing
automatically generated documentation.
10/02/22 by: A.F 36
Simple java program

//This program prints Welcome to Java!


public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

10/02/22 by: A.F 37


Arithmetic operators in Java

Operation Symbol
• Compound
expressions are Addition +
formed by
combining simple Subtraction -
expressions using
Multiplication *
arithmetic operators
Division /

Modulus %

10/02/22 by: A.F 38


Java Operators
 Arithmetic operators
 + (addition)
 – (subtraction)
 * (multiplication)
 / (division)
 Integer division by zero is not allowed where as floating-point division by zero yields an
infinite.

 ++ adds 1 to the current value of the variable .


 -- subtracts 1 from the variable.

10/02/22 by: A.F 39


Java Operators

 Assignment operator
 Used to assign value to a variable.
E.g.
int x=34;

10/02/22 by: A.F 40


Java Operators

 Logical operators
 && for the logical “and”
operator and
 || for the logical “or” operator.

10/02/22 by: A.F 41


Java Operators

Relational operators
• == (equal),

• != (not equal)

• < (less than),

• > (greater than),

• <= (less than or equal), and

• >= (greater than or equal) operators.


10/02/22 by: A.F 42
Java Operators
Bitwise Operators
• When working with any of the integer types, you have operators that can
work directly with the bits that make up the integers.

• The bitwise operators are

• & (“and”)

• | (“or”)

• ^ (“xor”)

• ~ (“not”) e.g. used to get one’s complement of a number. (Read more on


page 1522, Java how to program 7th Ed.)

10/02/22 by: A.F 43


Keywords
• are reserved for use by Java and are always spelled
with all lowercase letters.

10/02/22 by: A.F 44


Primitive vs Reference var
• Primitive Variables
• Are created from primitive data types
• E.g. int age;

• Reference Variables
• Are used to store the address of an object
• Created from Classes
• E.g. Scanner input;
10/02/22 by: A.F 45
Conditional Statements

if Conditions
 if condition
 If…else condition
 if …else if…else condition

10/02/22 by: A.F 46


Conditional Statements

 if condition --- Syntax


if (condition)
{ For example:

statement1 if (yourSales >= target)

statement2 {

... performance = "Satisfactory";

} bonus = 100;
}

10/02/22 by: A.F 47


Conditional Statements
 if … else condition --- Syntax
if (condition)
{
statement1
For example:
statement2
if (yourSales >= target)
... {
} performance = "Satisfactory";
else bonus = 100 + 0.01 * (yourSales - target);
{ }
statementx else
statementy {
... performance = "Unsatisfactory";
bonus = 0;
}
}

10/02/22 by: A.F 48


Conditional Statements
 if … else if … else condition --- Syntax
if (condition)
{
statement1
...
}
else if
{
statementx
...
}

else
{
statement…
}

10/02/22 by: A.F 49


Conditional Statements
Multiple Selections—The switch
Statement

The if/else construct can be


cumbersome when you have to
• byte,
deal with multiple selections
• short,
with many alternatives. • int
• char
E.g.

To set up menuing system

10/02/22 by: A.F 50


For loop
• The for statement provides a compact way to iterate over a range of
values.
• The general form of the for statement can be expressed as follows:
for (initialization; termination; increment)
{ statement(s) }
E.g.
for(int i=1; i<5; i++)
{
System.out.print (i); }
Output
1234

10/02/22 by: A.F 51


• The three expressions of the for loop are optional; an infinite loop can be
created as follows:
// infinite loop for ( ; ; )
{
// your code goes here
}
• The for statement also has another form designed for iteration through
Collections and arrays
• This form is sometimes referred to as the enhanced for statement, and can be
used to make your loops more compact and easy to read.

10/02/22 by: A.F 52


The while loop
• The while statement continually executes a block of statements while a
particular condition is true.
• Its syntax can be expressed as:
while (expression)
{ statement(s) }
• The while statement evaluates expression, which must return a boolean value.
int count = 1;
while (count < 11)
{
System.out.println("Count is: " + count);
count++;
}

10/02/22 by: A.F 53


Do-while loop
• The Java programming language also provides a do-while statement, which can
be expressed as follows:
do{
statement(s) }
while (expression);
E.g.
count =1
do {
System.out.println("Count is: " + count);
count++; }
while (count <1);

10/02/22 by: A.F 54


Jumping statements

• Java makes it easier to jump out of loops and to


control other areas of program flow with its break
and continue statements.

10/02/22 by: A.F 55


The continue statement

• When the continue statement is executed inside a loop-statement,


the program will skip over the remainder of the loop-body to the end
of the loop

10/02/22 by: A.F 56


The continue statement (cont.)

• Schematically:

10/02/22 by: A.F 57


The break statement

•When the break statement is executed inside a


loop-statement, the loop-statement is terminated
immediately
• The execution of the program will continue with
the statement following the loop-statement

10/02/22 by: A.F 58


The break statement (cont.)
• Schematically:

10/02/22 by: A.F 59


The new operator
• Is used to create an object or instance from a class.

• E.g.
Scanner s=new Scanner(System.in);
DataInputStream str=new DataInputStream
(System.in);

10/02/22 by: A.F 60


Escape Sequences
• Some Java escape sequences:

Escape Sequence Meaning

\b backspace
\t tab
\n newline
\" double quote
\' single quote
\\ backslash

10/02/22 by: A.F 61


Programming Errors

• Syntax Errors
• Detected by the compiler
• Runtime Errors
• Causes the program to abort
• Logic Errors
• Produces incorrect result

10/02/22 by: A.F 62


Compilation Errors

public class ShowSyntaxErrors {


public static void main(String[] args) {
i = 30
System.out.println(i+4);
}

10/02/22 by: A.F 63


Runtime Errors

public class ShowRuntimeErrors {


public static void main(String[] args) {
int i = 1 / 0;
}
}

10/02/22 by: A.F 64

You might also like