Java Manual Summer

Text-only Preview

Ibri College of Technology
Information Technology Department

ITSE 2100: Object Oriented
Programming Using Java

Part I


Page No
1. Introduction

1.1. Structured Programming

1.2. Object Oriented Programming

1.3. Graphical User Interface

1.4. Character User Interface

1.5. Brief Java History

2. Features of Java

2.1. Java features

2.2. Java Platform

2.3. Java virtual machine

2.4. Compiling and running java programs

3. Introduction to Object Oriented Programming

3.1. Object Oriented Concepts

3.2. First steps with Java Programming

3.3. Writing Java programs

3.4. Variables and data types

3.5. Displaying values of Variables

3.6. Operators in Java

4. Reading User Input and Math class

4.1. Steps in reading User input

4.2. Using Math class

5. Control structures and repetition statements

5.1. If statement

5.2. If else statement

5.3. Switch statement

5.4. Looping statements

5.5. break statement

5.6. continue statement

5.7. Self-test exercises

Annexure: Working IDE`s

O b j e c t O r i e n t e d P r o g r a m m i n g ( J a v a )
Page II

6. Arrays

6.1. Understanding Arrays
6.2. Creating arrays
6.3. Initializing arrays
6.4. Using loops in arrays
6.5. Self-Test exercises
6.6. Multi-dimensional arrays
7. Classes and Objects

7.1. Declaring a class and method
7.2. Adding instance variables
7.3. Constructors
7.4. Method overloading
7.5. Constructor overloading
8. Inheritance


8.1. Inheritance basics
8.2. Creating sub classes
8.3. Protecting class variables
8.4. Inheritance and constructors
8.5. Overriding methods
8.6. Final keyword
9. Exception Handling

9.1. Uncaught exceptions
9.2. Common exceptions
9.3. Using try, catch and throw
10. Java File I / O

10.1 Introduction
10.2 Reading data from a file
10.3 Writing data to a file.

O b j e c t O r i e n t e d P r o g r a m m i n g ( J a v a )
Page III

Chapter 1: Introduction

The objective of this chapter is to get an overview on structured programming and
OOP. This chapter focuses on character user interface and graphical user interface.
Structured Programming
Object Oriented Programming
History and Java applications
Outcome coverage: 01

1.1 Structured Programming
A technique for organizing and coding computer programs in which a hierarchy of modules is
used, each having a single entry and a single exit point, and in which control is passed
downward through the structure without unconditional branches to higher levels of the
Structured programming frequently employs a top-down design model, in which developers
map out the overall program structure into separate subsections. A defined function or set of
similar functions is coded in a separate module or sub module, which means that code can be
loaded into memory more efficiently and that modules can be reused in other programs. After a
module has been tested individually, it is then integrated with other modules into the overall
program structure.
1.2 Object- Oriented Programming (OOP)
OOP represents an attempt to make programs more closely model the way people think about
and deal with the world. In the older styles of programming, a programmer who is faced with
some problem must identify a computing task that needs to be performed in order to solve the
problem. Programming then consists of finding a sequence of instructions that will accomplish
that task. But at the heart of OOP, instead of tasks we find objects - entities that have
behaviours, that hold information, and that can interact with one another. Programming
consists of designing a set of objects that model the problem at hand.
OBJECT-ORIENTATION is a set of tools and methods that enable software engineers to build
reliable, user friendly, and maintainable, well documented, reusable software systems that
fulfils the requirements of its users. Object-orientation provides a new view of computation. A
software system is seen as a community of objects that cooperate with each other by passing
messages in solving a problem.
OOP is a programming paradigm that treats program elements as objects that have data fields
and functions that acts on the data fields. The three main characteristics of OOP are
encapsulation, inheritance, and polymorphism.

O b j e c t O r i e n t e d P r o g r a m m i n g ( J a v a )
Page 1

1.3 Graphical User Interface (GUI)
GUI stands for Graphical User Interface; it means that you no need to type commands to
interact with your computer instead you have to click with the help of mouse on the icons.
GUI uses windows, icons and menus which can be manipulated by a mouse.
A window is a rectangular portion of the monitor screen that can display its contents (Example:
a program, icons,). A major feature is the ability for multiple windows to be open
simultaneously. Each window can display a different application, or each can display different
An icon is a small picture or symbol in a GUI that represents a program, a file, a directory or a
device. Commands are issued in the GUI by using a mouse, track ball or touchpad to first
move a pointer on the screen, or on top, the icon, menu item or window of interest in order to
select that object. For example, icons and windows can be moved by dragging and objects or
programs can be opened by clicking on their icons.
GUI allows to take full advantage of the powerful multitasking (the ability for multiple programs
and/or multiple instances of single programs to run simultaneously) capabilities of modern
operating systems.
1.4 Character User Interface (CUI)
A character user interface (CUI) - also known as Command Line Interface (CLI) - is a method
of interacting with the computer of the software by typing commands to perform certain tasks.
In CUI interfaces text is displayed as a fixed set of rows and columns on screen with mono
spaced characters and a single font. CUI-based computers were operated by the keyboard,
using a combination of function keys, key sequences and by typing in commands in a
prescribed syntax.
Although CUI interfaces has their origins in the 1950s, they continue to co-evolve with GUIs
like those provided by Microsoft Windows, Mac OS and the X Window System. In some
applications, such as MATLAB, AutoCAD or EAGLE, a CLI is integrated with the GUI, with
some benefits of both.

O b j e c t O r i e n t e d P r o g r a m m i n g ( J a v a )
Page 2

1.5 Brief history and Java applications
Java is an object oriented programming languages that was invented initially for home
appliances such as TV sets and washing machines. Java was invented in 1995 by James
Gosling at Sun Microsystems. With the commercialization of the Internet in 1991 and with the
invention of the WWW in the same year, Java found an opportunity in the web. The Internet
created a platform for Java programs. Thus a lot of people associate Java with Internet
programming. Today java is used for more than just Internet applications. In fact Java covers
four main domains:
Desktop Applications.
Server and Enterprise Application.
Mobile and Small devices applications.
Smart card applications
Each one of the domains is covered by a certain platform. Desktop application development is
supported by the Java Standard Edition (JSE) platform. Server and Enterprise Application
Development is supported by Java Enterprises Edition (JEE) platform. Mobile Application and
Smart Card applications development is supported by Java Mobile Edition (JME) platform.

O b j e c t O r i e n t e d P r o g r a m m i n g ( J a v a )
Page 3

Chapter 2 : Features of Java
The objective of this chapter is to explore different java features.
Structured Programming
Object Oriented Programming
History and Java applications
Outcome coverage: 01

2.1 Java Features
The Java Programming has many features. The following are a number of them:
2.1.1. Java is Object-oriented
The object oriented language must support the characteristics of the OOPs. And Java is a fully
object oriented language. It supports all the characteristics needed to be object oriented. In the
Java everything is treated as objects to which methods are applied. As the language C++
fulfills the above four characteristics yet it is not fully object oriented language because it is
structured as well as object oriented languages. But in case of java, it is a fully Object Oriented
language because object is at the outer most level of data structure in java. No stand-alone
methods, constants, and variables are there in java. Everything in java is object even the
primitive data types can also be converted into object by using the wrapper class.
2.1.2. Compiled and interpreted
Java combines both these approaches. Java compiler first translates source code into byte
code instructions. Byte codes are not machine instructions and in the second stage, Java
interpreter generates machine code that can be directly executed by the machine that runs the
Java program. Thus Java is both a compiled and interpreted language. This is done using
Java Virtual Machine concept.



(In JVM)

2.1.3. Platform Independent
Java provides the facility to "Write once Run anywhere"(Known as platform independent). Not
even a single language is idle to this feature but java is closer to this feature. Java Provide the
facility of cross-platform programs by compiling in intermediate code known as byte code

O b j e c t O r i e n t e d P r o g r a m m i n g ( J a v a )
Page 4

2.1.4 Multithreaded
Java is also a Multithreaded programming language. Multithreading means a single program
having different threads executing independently at the same time. Multiple threads execute
instructions according to the program code in a process or a program. Multithreading works the
similar way as multiple processes run on one computer. Multithreading programming is a very
interesting concept in Java. In multithreaded programs not even a single thread disturbs the
execution of other thread. Threads are obtained from the pool of available ready to run threads
and they run on the system CPUs. This is how Multithreading works in Java which you will
soon come to know in details in later chapters.
2.1.5. Distributed
It is a distributed language for creating applications on networks. The widely used protocols
like HTTP and FTP are developed in java. Internet programmers can call functions on these
protocols and can get access the files from any remote machine on the internet rather than
writing codes on their local system.
2.1.6. Robust
Java has the strong memory allocation and automatic garbage collection mechanism. It
provides the powerful exception handling and type checking mechanism as compare to other
programming languages. Compiler checks the program whether there any error and interpreter
checks any run time error and takes the system secure from crash. All of the above features
make the java language robust.
2.1.7. Secure
Java does not use memory pointers explicitly. All the programs in java are run under an area
known as the sand box. Security manager determines the accessibility options of a class like
reading and writing a file to the local disk. Java uses the public key encryption system to allow
the java applications to transmit over the internet in the secure encrypted form. The byte code
Verifier checks the classes after loading.
2.1.8. Rich standard set of libraries
The standard Java (JSE) comes with a huge library of class. This standard library supports
almost all the tasks that you can think of (networking, databases, graphical user interface, I/O
and file handling, filer compression, etc. ...). The documentation of all class is accessible via
the web through the Java platform (Application Programming Interface) API specification
(,5.0/docs/api/). The API specification provides an
explanation about all the standard Java classes and how to use them in your code.
2.2 The Java Platform
The Java platform consists of the Java application programming interfaces (APIs) and the Java
virtual machine (JVM). Java APIs are libraries of compiled code that you can use in your
programs. They let you add ready-made and customizable functionality to save you
programming time.

O b j e c t O r i e n t e d P r o g r a m m i n g ( J a v a )
Page 5

Java Programs
a APIs
The Java Virtual Machine (JVM)
Your Computer System
Figure : Java Platform Components

Java programs are run (or interpreted) by another program called the Java VM. If you are
familiar with Visual Basic or another interpreted language, this concept is probably familiar to
you. Rather than running directly on the computer processor (like C programs), the program is
interpreted by the Java VM for the native operating system. This means that any computer
system with the Java VM installed can run Java programs regardless of the computer system
on which the applications were originally developed.
For example, a Java program developed on a Personal Computer (PC) with the Windows 7
operating system should run equally well without modification on a workstation with the Linux
operating system, and vice versa. (Hence platform independence).
2.3 The Java Virtual Machine (JVM)
The Java Virtual Machine is the software implementation of a "CPU" designed to run compiled
Java code.
This is a software process that converts the compiled Java byte code to machine code. Byte
code is an intermediary language between Java source and the host system.
JVM is the main component of Java architecture and it is the part of the JRE (Java Runtime
Environment). It provides the cross platform functionality to java. Most programming language
like C and Pascal converts the source code into machine code for one specific type of machine
as the machine language vary from system to system. Mostly compiler produces code for a
particular system but Java compiler produce code for a virtual machine. JVM provides security
to java.
The programs written in Java or the source code translated by Java compiler into byte code
and after that the JVM converts the byte code into machine code for the computer one wants
to run. JVM is a part of JRE that is required by every operating system requires a different

O b j e c t O r i e n t e d P r o g r a m m i n g ( J a v a )
Page 6

2.4 Compiling and Running Java programs
As mentioned before, you need the Java Platform installed in your PC in order to run Java
Programs. When you install the (JSE) in your computer, it comes with two tools: javac and
java. The Java javac invokes the java compiler. The java compiler compiles a Java program -
usually stored in a .java file to a java byte code (.class file). The byte code - contained in the
.class file - is not human readable code. It is rather contains instructions to be executed by the
JVM. The JVM interprets the byte code contained in the .class file and produces the result
intended from the java program. The JVM is invoked by the java tool mentioned earlier. Figure
4 shows the process of compiling and running Java programs.

Figure : From Source Code to a running program

Annexure: The Java Sandbox
Discussions of Java`s security model are often centered on the idea of a sandbox model. The
idea behind this model is that when you allow a program to be hosted on your computer, you
want to provide an environment where the program can run, but you want to confine the
program's play area within certain bounds. You may decide to give the program certain toys to
play with (i.e., you may decide to let it have access to certain system resources), but in
general, you want to make sure that the program is confined to its sandbox. The Java sandbox
is responsible for protecting a number of resources, and it does so at a number of levels.

O b j e c t O r i e n t e d P r o g r a m m i n g ( J a v a )
Page 7