Introduction To Programming
Introduction To Programming
What is program?
• A set of such structured instructions is called a Program which
tells the computer what to do.
• The process of writing specific instruction in a computer language
is called programming.
• Person who writes such programs is known as a programmer or
coder.
• The language used in a computer program is called as
Programming Language.
Ex: BASIC, FORTRAN, Pascal, COBOL, C, C++ and JAVA etc.
What is java?
• Java (originally known as oak) is a computer programming
language (which is also a high-level language).
• Its is similarity to ordinary English language) has a set of rules that
determine how the instructions are to be written. These rules are
known as its syntax.
History of Java
• James Gosling (a Canadian Computer Scientist, Born 19th May 1955)
initiated the Java language project in June 1991 for use in one of his
many set- top box projects.
• The language, initially called Oak after an oak tree that stood outside
Gosling‘s office, also went by the name Green and ended up later being
renamed as Java, from a list of random words.
• Sun released the first public implementation as Java 1.0 in 1995. It is a
Write Once, Run Anywhere(WORA) type language, providing no-cost
run- times on popular platforms.
• On 13 November 2006, Sun released much of Java as free and open
source software under the terms of the GNU (GNU is an operating system
which is completely free software system) 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.
Advantages / Characteristics of java :
• Simple :
➢ Java is Easy to write and more readable.
➢ Java has a concise, cohesive set of features that makes it
easy to learn and use.
➢ Most of the concepts are drawn from C++, thus making
Java learning simpler.
• Secure :
➢ Java program cannot harm other system thus making it
secure.
➢ Java provides a secure means of creating Internet
applications.
➢ Java provides secure way to access web applications.
• Portable :
➢ Java programs can execute in any environment for which
there is a Java run-time system.
➢ Java programs can run on any platform (Linux, Window,
Mac)
➢ Java programs can be transferred over world wide web (e.g
applets)
• Object-oriented :
➢ Java programming is object-oriented programming
language.
➢ Like C++, java provides most of the object oriented
features.
➢ Java is pure OOP Language. (while C++ is semi object
oriented)
• Platform independent:
➢ Languages can be platform dependent or independent.
➢ But Java is a Platform independent which is portable &
easily executed on all operating systems (windows, Linux,
solaris) or Java programs can run on any platform.
• Robust :
➢ Java encourages error-free programming by being strictly
typed and performing runtime checks.
• Multithreaded :
➢ Java provides integrated support for multithreaded
programming.
• Architecture-neutral :
➢ Java is not tied to a specific machine or operating system
architecture.
➢ Java is machine independent.
• Interpreted :
➢ Java supports cross-platform code through the use of Java
bytecode.
➢ Bytecode can be interpreted on any platform by JVM (Java
Virtual Machine).
• High performance :
➢ Bytecodes are highly optimized.
➢ JVM can execute bytecodes much faster .
• Distributed :
➢ Java is designed with the distributed environment.
➢ Java can be transmitted over internet.
• Dynamic :
➢ Java programs carry substantial amounts of run-time type
information with them that is used to verify and resolve
accesses to objects at run time.
What JVM and its work:
• JVM(Java Virtual Machine) acts as a run-time engine to run Java
applications.
• JVM is the one that actually calls the main method present in a
Java code.
• JVM is a part of JRE(Java Runtime Environment).
• Java applications are called WORA (Write Once Run Anywhere).
This means a programmer can develop Java code on one system
and can expect it to run on any other Java-enabled system without
any adjustment. This is all possible because of JVM.
• When we compile a .java file, .class files(contains byte-code)
with the same class names present in .java file are generated by the
Java compiler.
• This .class file goes into various steps when we run it. These steps
together describe the whole JVM.
Source Code :
• The core program or text written in any computer language
(like C, C++, Java, etc.) is called source code.
• Usually it is a collection of computer instructions written by
using any human readable computer language.
• Source code files have the extension class.
Object Code:
• The program in the form of machine instructions or binary instructions
(i.e., in computer readable form).
• It is generally produced by compiler/interpreter, but in case of Java, it is
produced by JVM.
Byte Code:
• In Java, when a source code is compiled, it doesn‘t directly convert into
object code, rather it converts into what, is known as byte code.
• So, a byte code is machine instruction that the Java compiler (Javac)
generates.
Note :
❖ Javac –compiler that converts source code to byte code.
❖ JVM- interpreter that converts byte code to machine language code.
❖ Source Code (program.java) Æ compile Æ Byte Code (program. class).
❖ Byte Code (program. class) Æ JVM Æ Machine Code.
RAPID APPLICATION DEVELOPMENT (RAD) USING IDE:
• Rapid application development is abbreviated as RAD.
• It is an object- oriented approach for software development, being
introduced by James Martin in the 1980s.
• It includes a method of development as well as software tools
commonly known as ICU. where,
i. I-Iterative development.
ii. C-Construction of prototypes.
iii. U-Use of Computer-Aided Software Engineering (CASE) tools.
• RAD includes analyzing, designing, building and testing phases the form
of short iterative cycle which has a number of rare advantages over the
traditional sequential development methods.
• Rapid application development is a response of different Processes
developed in the 1970s and 1980s, such as the Structured Systems
Analysis and Design Method and other Waterfall Model.
• Following are two most popular RAD systems;
i. NetBeans IDE is a cross-platform RAD tool for creating visual
desktop, mobile and web applications for Linux, Windows and
Mac OS X.
ii. MyEclipse is a RAD environment, focusing on enterprise Java and
web application development.
Advantages of RAD are:
✓ Increased speed of software development via including rapid prototyping,
virtualization of system related routines, the use of CASE tools, and other
techniques.
✓ Reduced complexity of development.
✓ More focus on simplicity and usability of GUI components.
Java Development Kit (JDK):
• Java programs are of mainly two types-applets and applications.
• Applets are the Java programs executed by a web browser while
applications are the programs that execute directly on your machine.
• JDK is a program development environment for writing Java applets and
applications.
• Sun decided to give away a Java Development Kit (JDK) that would
provide basic tools needed for Java programming.
• JDK provides all the tools needed to write powerful Java applications or
applets. JDK includes JRE (Java Runtime Environment) and some other
basic tools, like an interpreter/loader (Java), a compiler (javac), an
archiever (jar), a documentation generator (javadoc), etc.
These tools are described below:
i. JRE (Java Runtime Environment): It is the environment which is used
to run the predeveloped applets or applications in JDK.
ii. Javac: Java compiler is named as Javac. The Javac takes input source
code and converts them into compiled byte code.
iii. Java: The Java interpreter/loader, normally called as Java. It can be
used to execute Java applications. The interpreter translates byte code
directly into program actions.
iv. Java doc : It is used to generate our source code into HTML files
documentation.
v. Jar: It is an archiever used to create jar files. A JAR file is a file that
contains the class, image and sound files for a Java application or applet
gathered into a single file and possibly compressed.
vi. Applet Viewer: This small program provides a real Java environment
for testing applets. It loads the HTML files (in which the applet has been
embedded) and displays the application in a browser like window.
Integrated Development Environment (IDE):
• An Integrated Development Environment is abbreviated as IDE.
• It is a software tool which provides a way to create and debug programs
easily.
• IDEs are designed to maximize programmer productivity by providing
developing of components with similar user interfaces.
• An IDE brings all of the programming tools into one convenient place.
• There was a time when programmers had to edit files, save them, run the
compiler and linker, build the application and then run it through a
debugger and all these works were done separately. IDEs overcome this
problem.
• Today, IDEs bring editor, compiler, linker and debugger into one place
along with project management tools to increase programmer
productivity.
• In other words, IDE is a software program that is designed to help
programmers and developers to build software.
• One of the most important features of an IDE tool is that the program
code can be continuously parsed while it is being edited and provides
instant feedback when syntax errors are occurred.
Most IDEs include :
1. A source code editor, similar to an HTML text editor. It is where
programmers write the source code for their programs.
2. A compiler which converts the source code into intermediate code
(Byte code) and an interpreter (JVM) to execute that compiler
generated byte code.
3. Build automation tools help to automate the processes that need to
happen with most software development like compiling debugging
and deployment.
4. A debugger helps to point out the exact spot where there is a
problem in the source code.
5. A Graphical User Interface (GUI) builder simplifies the creation
of GUIs.
NetBeans IDE:
• NetBeans IDE is a free, open source, integrated development
environment (IDE) that enables you to develop desktop, mobile and web
applications.
• The IDE supports application development in various languages,
including Java, HTML5, PHP and C++.
• The IDE provides integrated support for the complete development cycle,
from project creation through debugging, profiling and deployment.
• The IDE runs on Windows, Linux, Mac OS X, and other UNIX- based
systems.
• The IDE provides comprehensive support for JDK technologies and the
most recent Java enhancements.
• It is the first IDE that provides support for JDK 8, Java EE 7, and JavaFX
2. The IDE fully supports Java EE using the latest standards for Java,
XML, Web services, and SQL and fully supports the Glassfish Server, the
reference implementation of Java EE.
• NetBeans IDE 8.1 is a free and open-source Java Integrated Development
Environment. It is the first free IDE providing support for JDK 8
previews, JDK7, Java EE 7 including its related HTML5 enhancements
and JavaFX 2.
• NetBeans IDE incorporates many tools to facilitate creating, managing
and running a program, includingfeatures like a Graphical User Interface
(GUI)fordevelopment, a fancy text editor, an output window, links
forcompilation errors, buttons to compile and run the program,a debugger
and many more.
Integrated modules of a NetBeans IDE are :
1. NetBeans Profiler:
➢ It is a tool for the monitoring of Java applications.
➢ It helps developers to find memory leaks and optimize
speed.
❖ GUI Design Tool :
It enables developers to prototype and design Swing
GUI by dragging and positioning graphical components.
❖ NetBeans JavaScript Editor:
The NetBeans JavaScript editor provides
extendedsupport for JavaScript, Ajax, and CSS.
Some advantages of NetBeans IDE are :
a. Best support for latest advanced Java technology.
b. Fast and smart code editing.
c. Easy and efficient project management.
d. Supports rapid applicationdevelopment mechanism.
e. Enable users to write bug free code.
2. Java Editors:
To write your Java programs, you will need a text editor. There
are even more sophisticated IDEs available in the market. But for
now, you can consider one of the following:
❖ Notepad: On Windows machine you can use any simple
text editor like Notepad (Recommended for this tutorial),
Text Pad.
❖ NetBeans: is a Java IDE that is open-source and free
which can be downloaded from
http://www.netbeans.org/index.html.
❖ Eclipse: is also a Java IDE developed by the eclipse
open-source community and can be downloaded from
http://www.eclipse.org/.
The Net Beans IDE contains various controls and menus via which, a user can
easily interact and work according to their requirements.
1. Title Bar: It displays the name of the application or a program.
2. Menu Bar: It provides the various commands used to work with
Net Beans IDE, such as file, edit, view, etc.
3. Tool Bar: It provides a way to quickly access the commonly used
commands.
4. Palette Window: This window contains all the containers and
other graphical components such as labels, panels, text fields etc.
5. Projects Window: This window shows a logical structure of
different project contents.
6. Inspector Window: This window displays a hierarchical tree
structure of all the components used in the current form.
7. Properties Window: This window displays the editable properties
of the currently selected graphical component.
8. Design Area: It is the primary area used for creating and editing of
Java forms.
9. Source Editor:Itis a fully featured text editor program which
includes a compiler, debugger and other components of IDE.
10. Output Window: This window shows the output messages from
the Net Beans IDE. Output window automatically shows the
messages after compilation of a program.
GETTING FAMILIAR WITH JAVA SWING USER INTERFACE
COMPONENTS :
Components & Container:
• In Java, JFC (Java Foundation Classes) support GUI features.
• JFCs are pre-written code in the form of class libraries (coded routines)
that give the programmer a comprehensive set of GUI routines to use.
• JFC is an extension of the original Java Abstract Window Toolkit
(AWT).
Graphical User Interface (GUI) :
• It is a type of user computer interface that allows users to take advantages
of computer graphical capabilities to make program easier.
• A GUI is developed with the help of graphical elements such as buttons,
scroll bars, list and text fields.
• These elements are called components of GUI Java GUI programming
involves two packages, the original abstract windows kit and newer
swing toolkit.
• Components present in AWT are all present in swing. Swing components
have the prefix J to differ them from AWT. For example, JLabel instead
of Label.
Java Swing:
• Java Swing is a group of classes that provides more powerful and
flexible GUI (Graphical User Interface) components than does the AWT
(Abstract Window Toolkit).
• Swing is built on the foundation of AWT and concentrate to the
limitations present in the AWT.
• The two key features of swing are— light weightcomponents and a
pluggable look and feel.
• Together they provide an elegant, yet easy to use solution to the
problems occurred in AWT.
Components in Swing:
• A component is an independent visual control, such as a push button or
slider.
• Swing components are derived from the JComponent class.
• For example, JComponent supports the Pluggable Look And Feel
(PLAF), which means that each swing component is provided by java
rather than by native peers.
• The look and feel of a component is under the control of swing.
• JComponent inherits the two AWT classes; Container and Component,
to give GUI data item a better look. There are two types of graphical
components available in java swing :
(i) Container Control It is the control that holds some other swing
controls. For example, Panel, Frame etc.
(ii) Child Control Those controls which are available in the container
control are known as child control. For example, Button, List etc.
Some Basic Component of Swing :
• Swing components are represented by a class within the package
javax.swing.
• This package must be imported into every swing program. The package
javax.swing contains several classes that implements the basic GUI swing
components shown in the table below :
• The components given the table provide an easy way to create graphical
inbuilt software programs.Few of them are described below with their
properties and methods.
1) Label: Label is the display area for a short text string or an
image. It is created through JLabel component.It is easy to
use component which does not react on the input actions
performed by an user. A label displays a single line of read
only text. The text can be changed by the application, but a
common user cannot edit it directly.
Constructor:
❖ A java constructor has the same name as the name of the class to which
it belongs.
❖ Constructor‘s syntax does not include a return type, since constructors
never return a value.
❖ It is executed when we create new object of that class. JLabel provides
following constructors
JLabel (Icon i)
JLabel (String str)
JLabel (String str, Icon i, int align)
❖ Here, str and i are the text and icon respectively used for the label.
❖ The align argument specifies the horizontal alignment of the data item
within the dimensions of the label It must be one of the following values
LEFT, RIGHT, CENTER, TOP and DOWN.
Properties:
a. Background :This property is used to set the background colour of a
label. The colour of the label is displayed when opaque property is set to
true. The default setting value is false.
b. Enabled :This property is used to set whether the label is active or not at
run a. time.
c. Font :This property is used to set a font style for the text displayed on the
label.
d. Foreground :This property is used to set the foreground colour to be
displayed on the label. The foreground colour of the label is displayed
when opopaque property is set to be true but the default setting value is
false.
e. Text :This property is used to specify the text to be displayed on the label.
Methods :
a. voidsetText(String) :This method is used to set the text which will be
displayed with the label.
For example,<label_name>.setText(“Name”); It would set the
“Name” text on the specified label.
b. String getText() :This method is used to fetch the text displayed on the
label. For example,String str = <label_name>.getText();
It returns the text value available on the specified label.
2) Text Field Text Field is the simplest text component which
allows a user to feed input text at the run-time. It is derived
from JTextField component, which provides the basic
functionality common to swing components.JTextField
allows you to edit one line of text. If you enter numeric data
then you must have to convert it into text format because
numeric data could not be displayed in a textfield.