Chapter One: Structure Programming
Chapter One: Structure Programming
Chapter One
Introduction to OOP and Java
1. Introduction to Programming
2. History of OO methodology
3. Object-Oriented Programming
4. The History of Java
5. Features of Java?
6. Install JDK
7. Understand JDK, JVM and JRE
8. Primitive data type
9. Rules in Java used
10. Create Java Program
1. Introduction to programming
Program is logical sequence of instructions or series of commands for the computer to
perform a specific task. Now programmers are using high level programming languages. In old
days, programmers used low level or machine language.
Low level or machine language
Machine language (The hardware can be controlled using machine language)
01001011001010010010010010101
Assembly language
MOV AX, BX
ADD R3, #32, R9
High level programming languages are:
Pascal
Structure Programming
C
COBOL
Foxpro Database Programming
C++
Java OOP Programming
C#.NET etc.,
To develop a program
Step One: Problem definition
Define the problem in terms of:
- Input data
- Calculate data
- Oupt data
Sept Two: Algorithm
Define the step by step solving of the problem. Algorithms are specified in
flowchart or pseudo.
Step Three: Coding
Writing program codes in text editor using selected programming language.
Step Four: Testing
Page 1
True Hope Core Java (J2SE)
2. History of OO methodology
Programming
1967s Simula 67 developed by Ole-Johan Dahl and Kristen Nygaard, Norway.
1970s Smalltalk developed by Alan Kay's group, inspired from Simula.
1980s C++ by Bjarne Strustrup
Simula 67 introduced most of the key concepts of object-oriented programming: both classes and
subclasses (usually referred to as inheritance), objects.
3. Object-Oriented Programming
Object-oriented programming (OOP) is a programming paradigm based upon objects (having both
data and methods) that aims to incorporate the advantages of modularity and reusability. Objects,
which are usually instances of classes, are used to interact with one another to design applications
and computer programs.
Advantages of OOP
Reusability
Easy to maintain
Reliability
4. The History of Java
1991, Sun was investigating consumer electronic products. James Gosling, the father of Java, was
intent on building a low-cost, hardware-independent software platform using C++. For a number of
technical reasons, C++ was dropped, and a new language, called Oak, was developed, based on
C++, but eliminating its shortcomings. This shortcoming includes problems associated with
multiple inheritances, automatic type conversion, the use of pointers, and memory
management.
Oak was used to develop a small electronics device called *7. This project resulted in the precursors
of many of the components of Java: the development environment, runtime system, and API.
By 1994 the Web emerged, Oak was renamed Java.In 1995, Java was ported to Windows 95 and
Linux.
On January 23, 1996, Java 1.0 was officially released and made available for download over the
Internet.
What is Java?
• Java was developed in early 1991 by Sun Microsystems
• Java is a high level language
• Java programs are portable across platflorm
• Object-oriented programming language
Java Programming Step
Page 2
True Hope Core Java (J2SE)
Host platform
The developer writes programs in the Java language. The developer compiles his or her programs
using the Java compiler. This results in what is known as complied bytecode. Bytecode is in a form
that can be executed on the Java virtual machine, the core of the Java runtime system.
5. Features of Java?
Object Oriented
Distributed Simple
Secured
Multithreaded
Interpreted
Robust
Dynamic Architecture
neutral Portable
Object-oriented
Object-oriented means we organize our software as a combination of different types of objects that
incorporates both data and behaviour.
Basic concepts of OOPs are:
1. Object
2. Class
3. Inheritance
4. Polymorphism
Simple
According to Sun, Java language is simple because:
syntax is based on C++ (so easier for programmers to learn it after C++).
removed many confusing and/or rarely-used features e.g., explicit pointers, operator overloading etc.
No need to remove unreferenced objects because there is Automatic Garbage Collection in java.
Secured
Java is secured because:
Java compiler generates the bytecode from .java file.The bytecode is not the machine code. And it is
this bytecode which travel form one computer to another computer not the machine code. JVM, java
virtual machine creates the machine code from this bytecode .So the machine code of java program
never save on any operating system, program carry the bytecode.
Java is secure because of this bytecode.
Page 3
True Hope Core Java (J2SE)
Platform Independent
Robust
Java is robust as it is capable of handling run-time errors, supports automatic garbage collection and
exception handling, and avoids explicit pointer concept. Java has a strong memory management
system. It helps in eliminating errors as it checks the code during both compile and runtime.
Java is garbage-collected language – JVM automatically deallocates the memory blocks and
programmers do not have to worry about deleting the memory manually as in case of C/C++.
Architecture-neutral
It means that the program written on one platform or OS is independent of other platforms or
environments and can run on any other Operating System without recompiling them.
In other words, it is based on the ‘Write-once-run-anywhere’ (WORA) or ‘Write-once-run-
everywhere’ (WORE) approach.
High-performance
Java is faster than traditional interpretation since byte code is "close" to native code still somewhat
slower than a compiled language (e.g., C++)
Distributed
A distributed application is software that is executed or run on multiple computers within a
network. We can create distributed applications in java. RMI( Remote Method Invocation)
Page 4
True Hope Core Java (J2SE)
and EJB are used for creating distributed applications. We may access files by calling the methods
from any machine on the internet.
Multi-threaded
A thread is an independent path of execution within a program, executing concurrently.
Multithreaded means handling multiple tasks simultaneously or executing multiple portions
(functions) of the same program in parallel. It shares a common memory area. Threads are important
for multi-media, Web applications etc.
Dynamic
Java is dynamic and extensible means with the help of OOPs, we can add classes and add new
methods to classes, creating new classes through subclasses. This makes it easier for us
to expand our own classes and even modify them.
Step 2) Next,
Accept License Agreement
Download latest Java JDK for your version (32 or 64 bit) of java for Windows.
Step 3) Once the download is complete, run the exe for install JDK. Click Next
Page 5
True Hope Core Java (J2SE)
Let's look into the steps to set the PATH and CLASSPATH
Step 1) Right Click on the My Computer and Select the properties
Step 2) Click on advanced system settings
Step 3) Click on Environment Variables
Step 4) Click on Edit Button of User variables
Step 5) Click New Button
Step 7) Paste Path of bin folder in Variable value and click on OK Button.
Step 8) click on OK Button.
JDK
The Java Development Kit (JDK) is a software development environment which is used to develop
Java applications and applets. It physically exists. It contains JRE + development tools.
JDK is an implementation of any one of the below given Java Platforms released by Oracle
Corporation:
o Standard Edition Java Platform
o Enterprise Edition Java Platform
o Micro Edition Java Platform
Page 6
True Hope Core Java (J2SE)
Page 7
True Hope Core Java (J2SE)
Page 8
True Hope Core Java (J2SE)
- After finished download you will see eclipse zip file in download folder
- To install on windows, you need a tool that can extract the contents of a zip file.
- Using any one of these tools, extract the contents of the eclipse zip file to any folder of your choice.
Page 9
True Hope Core Java (J2SE)
Launching Eclipse
On the windows platform, if you extracted the contents of the zip file to c:\
- Select a folder for your workspace (for Java files are stored locally)
Page 10
True Hope Core Java (J2SE)
Page 11
True Hope Core Java (J2SE)
Run Program
Select : Run button in tools bar
You will see output in console window
Example 2: Write a Java program to assign values and print the variables.
// Assign the values to variables and to print the variables
import java.io.*;
public class VariablesApp {
public static void main(String args[]) {
boolean flag = true;
char ch = 'B';
int n = 20;
float x=3.14159f;
String str=null;
String country =” United States”;
System.out.println("flag = " + flag);
System.out.println("ch = " + ch);
System.out.println("n = " + n);
System.out.println("x = " + x);
System.out.println("str = " + str);
System.out.println("Country = " + country);
}
}
---------------------------------------------------------------------------------------------------------------------
Exercises for chapter one
1. Write a Java program to print the result of c.
float a = 1.5f;
int b = 20;
float c = a*b;
System.out.println(“C = " + c);
2. Write a Java program to print the following text on screen
Hello
My name is Java.
Easy to understand.
3. Define JRE i.e. Java Runtime Environment?
Java Runtime Environment is an implementation of the Java Virtual Machine which executes Java
programs. It provides the minimum requirements for executing a Java application;
Page 12