ACFrOgBxRfC6KVml7bPOy0GwMUhmva4sD3xQgLHGsbD2rGLB3pyngFwR8JSgscFxGh71h1n e23Qzia2gF6hlMq7e7-6LbRl5Tj86d9jaI3Xb iG-dO5G189gbsFDoHmeA1Zy85nsslrbiTbfqhv
ACFrOgBxRfC6KVml7bPOy0GwMUhmva4sD3xQgLHGsbD2rGLB3pyngFwR8JSgscFxGh71h1n e23Qzia2gF6hlMq7e7-6LbRl5Tj86d9jaI3Xb iG-dO5G189gbsFDoHmeA1Zy85nsslrbiTbfqhv
Object- oriented design (OOD) is a widely used programming methodology. In OOD, the first Step in the
programming-solving process is to identify the components called objects, which Form the basis of the solution, and to
determine how these objects interact with one another. For example, suppose you want to write a program that automates
the video rental process for a local video store. The two main objects in this problem are the video and the customer. The
programming language that implements OOD is the Object-Oriented Programming (OOP).
Two popular approaches to programming design are the structured approach and the object-oriented approach, which are
outlined below.
1. Structured programming
- Dividing a problem into smaller sub-problems is called structured design. Each sub-problem is then analyzed, and a
solution for the sub-problem is obtained. The solutions to all the sub-problems are then combined to solve the overall
problems. This process of implementing a structured design is called structured programming. The structured design
approach is also known as top-down, bottom-up design, stepwise refinement, and modular programming. This is also
refers to procedural Programming.
- It is a style of programming in which operations are executed one after another sequence. It involves knowledge of a
programming language to create applications, which program that task for a user.
- Is an extension of procedural programming in which take a slightly different approach in writing computer programs. It
involves creating classes (blueprint of creating objects); creating objects from those classes; and creating applications
that use those objects. Classes can be reused in creating a new program.
- Teaches you the core concepts behind object-oriented programming: objects, messages, classes, and inheritance.
- OOP involves envisioning program components as objects that belongs to the classes and that are similar to concrete
objects in the real world which manipulate and interrelate with each other to achieve the desired result.
1. Computer Simulation – This attempt to mimic real-world activities to improve processes for the user to understand
the real-world operations involving each process.
In OOP terminology, a class is a term that describes a group or collection of objects with common properties. A class definition
describes what attributes its objects will have and what those objects will be able to do.
Attributes are the characteristics that define an object; they are properties of the object. The values contained in an object’s
properties differentiate objects of the same class from one another. An object is a specific, concrete instance (example) of a
class. Object state – the values of the properties of an object.
Instance Variables - Each object has its unique set of instance variables. An object's state is created by the values assigned
to these instance variables. Note: When you create an object instance, you instantiate it.
Let us now look deep into what are objects. If we consider the real-world, we can find many objects around us, cars, dogs,
humans, etc. All these objects have a state and a behavior. If we consider a dog, then its state (class) is - name, breed, color,
and the behavior is - barking, wagging the tail, running (method).
If you compare the software object with a real-world object, they have very similar characteristics. Software objects also
have a state and a behavior. A software object's state is stored in fields and behavior is shown via methods. So in software
development, methods operate on the internal state of an object and the object-to-object communication is done via
methods.
ICT 103
P a g e |2
Nature of Method: Is a self-contained block of program code that carries out some action, similar to the procedure in a
procedural program (Structured Program). Besides defining properties, classes define method that object can use. In object-
oriented classes, attributes and method are encapsulated into objects that are then used much like real-world objects.
Class - A class can be defined as a template/blueprint that describes the behavior/state that the object of its type
supports: In this example DOG is a class.
Object - Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behavior such
as wagging their tail, barking, and eating. An object is an instance (example) of a class. In this example the specific
Methods - A method is basically a behavior. A class can contain many methods. It is in methods where the logics are
written, data is manipulated and all the actions are executed. In this example the way how different DOG (object)
wagging their tail, barking, and eating are some of the behavior associated in the DOG class is a Method.
After identifying the objects, the next step is to specify for each object the relevant data and possible operations to be
performed on that data. For example, for a movie object, the data might include:
The Object-Oriented Programming differs from traditional procedural (Structured) programming in three main principles.
1. Polymorphism
- Is the final important concept in OOP terminology which literally means “many forms”. It describes the feature of
the languages that allows the same word or symbol to be interpreted correctly in different situations based on the
context.
ICT 103
For Example:
Vehicle is a class where Automobile, Sailboat, and Airplane inherit each class; however; the turn and stop methods
work differently for instance of those classes.
P a g e |3
The advantages of polymorphism will become more apparent when you begin to create GUI applications containing features
such as window, buttons, and menu bars. In GUI application, it is convenient to remember one method name.
Interface: An interface is a contract between a class and the outside world. When a class implements an interface, it promises
to provide the behavior published by that interface.
Package: A package is a namespace for organizing classes and interfaces in a logical manner. Placing your code into
packages makes large software projects easier to manage. This is useful, and introduces you to the Application Programming
Interface (API) provided by the Java platform.
The Java Application Programming Interface (API) contains hundreds of predefined classes that you can use in your
programs. These classes are organized into what we call packages.
2. Inheritance
An importance features of object-oriented programs. This refers to the ability to create classes that share the attributes
and methods of existing classes, but with more specific features.
For example:
Automobile is a class, and all Automobile objects share many traits and abilities. Convertible is a class that inherits from
Automobile class; a Convertible is a type of Automobile that has and can do everything a “plain” Automobile does-but
with an added ability to lower its top. Convertible is not an object-it is a class. A specific Convertible is an object:
my1978MustangConvertible.
3. Encapsulation as it’s applies to classes as objects. Encapsulation refers to the hiding of data and methods with in an
object. It provides security that keeps data and methods safe from inadvertent changes. Programmers sometimes refer
to encapsulation as using of “black box”, or a device that you can use without regard to the internal mechanism.
Java program uses OOP concept which can be defined as a collection of objects that communicate via invoking each other's
methods, class, object, and instance variable.
HISTORY OF JAVA
Java programming language was originally developed by Sun Microsystems which was initiated by James Gosling and
released in public 1995 as core component of Sun Microsystems' Java platform (Java 1.0 [J2SE]). Moreover, the first stable
WORA (Write once, run anywhere) is a slogan developed by Sun Microsystem to describe the ability of one Java program
version to work correctly on multiple platforms. On 13 November, 2006, Sun released much of Java as free and open source
software under the terms of the GNU General Public License (GPL). On 8 May, 2007, Sun finished the process, making all of
Java's core code free and open source, aside from a small portion of code to which Sun did not hold the copyright. The latest
release of the Java Standard Edition is Java SE 8. With the advancement of Java and its widespread popularity, multiple
configurations were built to suit various types of platforms. For example: J2EE for Enterprise (Edition), J2ME for Mobile
(Edition) applications.
Java technology is both a programming language and a platform. The Java programming language is a high-level object-
oriented language that has a particular syntax and style. A Java platform is a particular environment in which Java
programming language applications run.
All Java platforms consist of a Java Virtual Machine (VM) and an Application Programming Interface (API). The Java Virtual
Machine is a program, for a particular hardware and software platform, that runs Java technology applications. An API is a
collection of software components that you can use to create other software components or applications. Each Java platform
provides a virtual machine and an API, and this allows applications written for that platform to run on any compatible system
with all the advantages of the Java programming language: platform-independence, power, stability, ease-of-development,
and security.
1. Java SE: When most people think of the Java programming language, they think of the Java SE API. Java SE's API provides
the core functionality of the Java programming language. It defines everything from the basic types and objects of the
Java programming language to high-level classes that are used for networking, security, database access, graphical
user interface (GUI) development, and XML parsing. In addition to the core API, the Java SE platform consists of a virtual
machine, development tools, deployment technologies, and other class libraries and toolkits commonly used in Java
technology applications.
2. Java EE: The Java EE platform is built on top of the Java SE platform. The Java EE platform provides an API and runtime
environment for developing and running large-scale, multi-tiered, scalable, reliable, and secure network applications.
3. Java ME: The Java ME platform provides an API and a small-footprint virtual machine for running Java programming
language applications on small devices, like mobile phones. The API is a subset of the Java SE API, along with special
class libraries useful for small device application development. Java ME applications are often clients of Java EE
platform services.
Object Oriented: In Java, everything is an Object. Java can be easily extended since it is based on the Object model.
Platform Independent: Unlike many other programming languages including C and C++, when Java is compiled, it is not
compiled into platform specific machine, rather into platform independent byte code. This byte code is distributed over
the web and interpreted by the Virtual Machine (JVM) on whichever platform it is being run on.
Simple: Java is designed to be easy to learn. If you understand the basic concept of OOP Java, it would be easy to master.
Secure: With Java's secure feature it enables to develop virus-free, tamper-free systems. Authentication techniques
are based on public-key encryption.
Architecture-neutral: Java compiler generates an architecture-neutral object file format, which makes the compiled
code executable on many processors, with the presence of Java runtime system.
Portable: Being architecture-neutral and having no implementation dependent aspects of the specification makes Java
portable. Compiler in Java is written in ANSI C with a clean portability boundary, which is a POSIX subset.
Robust: Java makes an effort to eliminate error prone situations by emphasizing mainly on compile time error checking
ICT 103
Multithreaded: With Java's multithreaded feature it is possible to write programs that can perform many tasks
simultaneously. This design feature allows the developers to construct interactive applications that can run smoothly.
P a g e |5
Interpreted: Java byte code is translated on the fly to native machine instructions and is not stored anywhere. The
development process is more rapid and analytical since the linking is an incremental and light-weight process.
High Performance: With the use of Just-In-Time compilers, Java enables high performance.
Dynamic: Java is considered to be more dynamic than C or C++ since it is designed to adapt to an evolving environment.
Java programs can carry extensive amount of run-time information that can be used to verify and resolve accesses to
objects on run-time.
Hardware
You will need a Pentium 200-MHz computer with a minimum of 64 MB of RAM (128 MB of RAM recommended).
Java Virtual Machine (JVM): Hypothetical computer which execute the instruction not directly to the computer itself.
Interactive applications are those in which a user communicates with a program by using an input device such as the
keyboard or a mouse.
Java Interpreter: Enables to check the bytecode and communicates with the operations system by converting bytecodes to
machine language.
If you are still willing to set up your environment for Java programming language, then this section guides you on how to
ICT 103
download and set up Java on your machine. Following are the steps to set up the environment. Java SE is freely available
from the link Download Java. You can download a version based on your operating system.
Follow the instructions to download Java and run the .exe to install Java on your machine. Once you installed Java on your
machine, you will need to set environment variables to point to correct installation directories:
P a g e |6
Setting up the Path for Windows: Assuming you have installed Java in c:\Program Files\java\jdk directory:
A. Right-click on 'My Computer' and select 'Properties'.
B. Click the 'Environment variables' button under the 'Advanced' tab.
C. Now, alter the 'Path' variable so that it also contains the path to the Java executable.
Example, if the path is currently set to 'C:\WINDOWS\SYSTEM32', then change your path to read
'C:\WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin'.
Setting Up the Path for Linux, UNIX, Solaris, FreeBSD : Environment variable PATH should be set to point to where the Java
binaries have been installed. Refer to your shell documentation, if you have trouble doing this. Example, if you use bash as
your shell, then you would add the following line to the end of your '.bashrc: export PATH=/path/to/java:$PATH'
To write your Java programs, you will need a text editor. There are even more sophisticated IDEs available in the market.
Notepad: On Windows machine, you can use any simple text editor like Notepad, Notepad++ TextPad.
Netbeans: A Java IDE that is open-source and free, which can be downloaded from
http://www.netbeans.org/index.html.
Eclipse: A Java IDE developed by the eclipse open-source community and can be downloaded from
http://www.eclipse.org/.
• Case Sensitivity: Java is case sensitive, which means identifier Hello and hello would have different meaning in Java.
• Class Names : For all class names the first letter should be in Upper Case. If several words are used to form a name of
the class, each inner word's first letter should be in Upper Case.
public is an access static means this method works without void is the method’s return
specifier/modifier. instantiating an object of the class. type
String is a class. Any arguments to The square brackets mean the argument args is the identifier of the array of
this method must be a String objects. to this method is an array of Strings. Strings that is the method argument.
Figure 4. The Parts of a typical main ( ) method
• Program File Name - Name of the program file should exactly match the class name. When saving the file, you should
save it using the class name (Remember Java is case sensitive) and append '.java' to the end of the name (if the file name
and the class name do not match, your program will not compile).
Example: Assume MyFirstJavaProgram is the class name. Then the file should be saved as MyFirstJavaProgram.java
• public static void main(String[] args) - Java program processing starts from the main() method which is a mandatory
part of every Java program.
Statements
Statements are roughly equivalent to sentences in natural languages. A statement forms a complete unit of
execution. The following types of expressions can be made into a statement by terminating the expression with a semicolon
(;).
• Assignment expressions
System is a class. .out is an object defined in the “First Java Application” is a literal string
System class that is the argument to the println( )
method
Dots (.) separate, classes, println( ) is a method. Method name are Every Java statements
objects and methods always followed by parentheses end with semi-colon
Figure 3. Anatomy of the Java statement
An access specifier or modifier defines the circumstances under which a class can access and the other classes
that have the right to use a class. Whitespace is any combination of non-printing characters like spaces, tabs, and carriage
returns.Such statements are called expression statements. Here are some examples of expression statements.
Blocks: A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement
is allowed.
class BlockDemo {
public static void main(String[] args) {
boolean condition = true;
if (condition) { // begin block 1
System.out.println("Condition is true.");} // end block one
else { // begin block 2
System.out.println("Condition is false.");
} // end block 2
}}
Java Keywords: There are 50 keywords currently defined in the Java language. These keywords, combined with the syntax of
the operators and separators, form the foundation of the Java language. These keywords cannot be used as names for a
variable, class, or method.
Java Separators: There are a few characters that are used as separators. The most commonly used separator in Java is the
semicolon. As you have seen, it is used to terminate statements. The separators are shown in the following table:
Coding/Indent Style
ICT 103
A. Allman Style is the indent style in which curly braces are aligned and each occupies its own line by Eric Allman. For
Example:
public status void main(String[] args)
P a g e |9
{
System.out.println(“First Java application”);
}
B. K & R Style named after Kernighan and Ritchie where indent style in which opening braces are not in separate lines. For
Example:
public status void main(String[] args) {
System.out.println(“First Java application”);
}
Program Comments are non-executing statements that you add to a program for the purpose of documentation. There three
types of comments in Java:
1. Line comments start with two forward slashes ( // ) and continue to the current line. A line comment can appear on
a line by itself or at the end of the line following executable code. It does require an ending symbol.
2. Block comments start with a forward slash and an asterisk ( /* ) and end with an asterisk and a forward slash ( */ ).
3. Javadoc comments are a special case of block comments. They begin with a forward slash and two asterisk ( /** )
and end with an asterisk and a forward slash ( */ ).
• Compile-time error – one of which the compilers detects a violation of languages syntax rules and is unable to
translate the source code to machine code.
• Logic error – error occurs when the syntax of the program is correct and the program compiles but produces
incorrect results when you execute it. A logic error is a type of a run time error.
• Run Time Error – an error not detected until the program asks the compiler to do something wrong. Or even
illegal, while executing.
• Syntax Error – compilers and interpreters issue this error each time it encounter an invalid program
• Semantic errors – refers to as a Logical errors. Like misspelled programming language word (syntax error)
and using correct words in the wrong context (semantic error).
JAVA IDENTIFIERS
All Java components require names. Names used for classes, variables, and methods are called identifiers.
In Java, there are several points to remember about identifiers. They are as follows:
- All identifiers should begin with a letter (A to Z or a to z), currency character ($) or an underscore (_).
- After the first character, identifiers can have any combination of characters.
- A key word cannot be used as an identifier.
- Most importantly, identifiers are case sensitive.
- Examples of legal identifiers: age, $salary, _value, __1_value.
- Examples of illegal identifiers: 123abc, -salary.
The Java programming language is statically-typed, which means that all variables must first be declared before
they can be used. This involves stating the variable's type and name: Ex. int gear = 1;
A variable's data type determines the values it may contain, plus the operations that may be performed on it.
Variables are reserved memory locations to store values. This means that when you create a variable you reserve some
P a g e | 10
space in the memory. Based on the data type of a variable, the operating system allocates memory and decides what can be
stored in the reserved memory.
A. Primitive Datatypes
There are eight primitive datatypes supported by Java. Primitive datatypes are predefined by the language and named
by a keyword. Let us now look into the eight primitive data types in detail.
byte:
2. short: Short datatype is a 16-bit signed two's complement integer. Short datatype can also be used to save memory as
byte data type. A short is 2 times smaller than an integer
- Minimum value is -32,768 (-2^15)
- Maximum value is 32,767 (inclusive) (2^15 -1)
- Default value is 0 Example: short s = 10000, short r = -20000;
3. int: Int datatype is a 32-bit signed two's complement integer. Integer is generally used as the default data type for
integral values unless there is a concern about memory.
- Minimum value is - 2,147,483,648 (-2^31)
- Maximum value is 2,147,483,647(inclusive) (2^31 -1)
- The default value is 0 Example: int a = 100000, int b = -200000;
B. Reference Datatypes
Reference variables are created using defined constructors of the classes. They are used to access objects. These
variables are declared to be of a specific type that cannot be changed.
A literal is a source code representation of a fixed value. They are represented directly in the code without any
computation. Literals can be assigned to any primitive type variable.
For example: byte, int, long, and short can be expressed in decimal (base 10), hexadecimal (base 16) or octal(base 8)
number systems as well.
byte a = 68;
char a = 'A'
Prefix 0 is used to indicate octal, and prefix 0x indicates hexadecimal when using these number systems for literals. For
example:
int decimal = 100;
int octal = 0144;
int hexa = 0x64;
String literals in Java are specified like they are in most other languages by enclosing a sequence of characters between a
pair of double quotes. Example:
"Hello World"
"two\nlines"
"\"This is in quotes\""
String and char types of literals can contain any Unicode characters. For example:
char a = '\u0001';
String a = "\u0001";