Escolar Documentos
Profissional Documentos
Cultura Documentos
0.0 Introduction
There are, at present, many popular high levels programming languages like,
Fortran, Cobol, C, etc. Each of these languages was developed to meet some specific
need of applications. Fortran was developed for scientific applications, Cobol for
business applications, C for easy development of System programming (like Compiler,
Operating System, etc). A Fortran or a C program is nothing but a collection of
variables and functional procedures or routines necessarily required for solving a
complex problem, part by part. Whereas, Java is an Object-Oriented Programming
(OOP) language and can be regarded as a collection of defined classes and objects
interacting with each other by passing messages. Before going into any further details,
let us have a look into the history of java language development.
Java was conceived in 1991 by a group at Sun Microsystems, Inc. The initial name
“Oak” was replaced by the new name “Java” in 1995. The initial motive behind Java
invention was to meet the demand of having a platform-independent or architecture-neutral
language so that codes can be embedded within any static or mobile devices without taking
any additional burden of compilation for different CPUs. Gosling and others could find out
a solution by inventing the language -- java. In mid-1990s, during java’s development
period, World Wide Web and Internet applications started coming up in vogue. Thus the
question of software downloading and using that for different types of computers, operating
systems, and CPUs became most important and java was supposed to take care of all such
requirements.
Java is ultimately found to be the most suitable language both for general-purpose
programming as well as for mobile and Internet programming. Java’s applets are tiny java
programs that can be dynamically transmitted over an Internet and executed by a Java
compatible Web Browser. Use of applets is very popular to-day.
0.2 Software Development
Software development has two aspects -- i) how requirement analysis & system
design can be made easy and quick and ii) how coding, testing and maintenance of
software can be made efficient. Software developers, while using function oriented
languages, experienced many difficulties with respect to both of these aspects. They were in
continuous search of a better high level language keeping in mind the importance of object
oriented programming. Java emerges as an outcome of that search and found to be most
suitable for meeting all modern age application requirements.
1) In real life, we find presence of many physical objects with separate attribute
values but capable of showing the same kind of behaviour. Such objects can
communicate with each other by passing messages (i. e. asking to take some
actions) and get any complex task performed, step by step. Software developers
can imitate such real-life situations within a computer program to solve
computational problems. This is exactly what is done in an Object Oriented
Programming (OOP).
Java program is nothing but a collection of defined class(es) and creating objects using
them and allowing interactions to take place as per requirements.
Without going into further details on the technical jargons – it can simply be said that Object
Oriented programming is better because it supports re-use of old classes and objects, which
constitute the heart of an Object Oriented program.
As OOP provides encapsulation and data hiding features, a program becomes self-
protected against external misuse and tampering.
3) For easy testing and verification of programs, modular programming (each
module containing a limited line of codes) is essentially required. In OOP
languages, use of classes and objects automatically limits the size of a module.
4) Classes and objects written for one program can be used in many other programs.
This feature of re-use is further augmented due to inheritance characteristics of
OOP languages. Library and user-defined Classes can be imported and shared by
both local and Internet users.
As mentioned earlier, java does not require separate (full) compilers for different
compiling platforms (CPU, OS etc). How can that be made possible?
Java compiler (javac) does not produce directly the machine executable binary codes
but generates an intermediate image of the source program in bytecodes. This intermediate
image of the source code is then placed before a java interpreter (java) to produce CPU
dependent actual machine codes for execution. Java interpreter generates machine codes for
a local CPU. The java interpreter portion will be different for different computer platforms,
but that portion [being very small in size] can easily be loaded at run-time and will never
demand for a large local memory resource.
Java’s source program is saved with an extension ( *).java . Java compiler – javac
-- generates its intermediate image in bytecodes and saves the same as (*).class file. The
local interpreter java makes use of the file (*).class for taking final actions at the execution
end. During execution, the java run-time environment (jre which helps running java
programs in a local computer environment) takes care of all run-time errors which
escaped the notice of the java compiler.
The intermediate bytecodes can be transmitted over Internet and shared safely by
different users placed at different locations. The jre and browser’s Java Virtual Machine
(JVM to run applets) can take care of security problems, if any. Thus java language is
made platform-neutral, portable, secured, distributed and dynamic binding type by
adopting language translation in two different phases.
0.5 Java Development Kit (JDK)
JDK is a Software Development Kit (SDK) for Java language. It includes, along
with the jre, many tools (javac, java, javadoc, debugger, etc) and class libraries that are
essentially required for program development.
The first release of JDK 1.1 having a size of about 9 MB (jre size 2.7 MB only) was
replaced in 1998 by JDK 1.2 which had a size of 21 MB (jre size 5.3 MB only). There after
the JDK 1.3 appeared having a size of 33MB with jre of 5 MB only. The latest JDK is
JDK 1.5 (size 45 MB with jre included). For the users of this book JDK 1.3 will be
sufficient to provide support to the BlueJ Integrated Development Environment
(IDE), which makes java learning much easier and simpler, specially for the beginners.
Before we discuss about BlueJ in details, evolution of java as a language is to be examined
first.
Java continued to evolve since its first release in mid-1990s as Java 1.0 immediately
followed by Java 1.1 where new features like Java Beans, Java DataBase Connectivity
(JDBC), Java Native Interface (JNI), etc were added. With Java 2, further new features like
Swing, Collections, Java Archive (JAR) files, Just-In-Time (JIT) compiler, etc were
introduced. At present, java is available in two editions -- J2SE (Java 2 Standard Edition)
and J2EE (Java 2 Enterprise Edition). Like these two editions, JDK 1.3 also provides
support to java 2.
BlueJ is based on the Blue system, developed at Sydney University and Monash
University, Australia. BlueJ provides a Blue-like environment for the Java language.
BlueJ is implemented in Java, and can be installed under various operating systems
like Solaris, Linux, Macintosh, and various Windows versions. It can run on all
platforms supporting Java Virtual Machine.
The BlueJ environment was developed as part of a university research project about
teaching object-orientation to beginners. The system is being developed and
maintained by a joint research group at Deakin University, Melbourne, Australia, and the
University of Kent in Canterbury, UK. The project is supported by Sun Microsystems.
The aim of BlueJ is to provide an easy-to-use teaching / learning environment for the
Java language programmers. Special emphasis on visualisation and modular
interactions has been given to encourage experimentation and exploration by users.
BlueJ offers most user-friendly development tools, better debugging, better execution
control, and better output display efficiency.
BlueJ (Picture 0.1) is most suitable for the beginners because of the following: ----
• It can be used with the latest version of Java.
• It’s available free of charge -- an advantage not only for the laboratories but also
for students who wish to work with a copy at home.
• It gives a graphical representation of the classes used in a java project.
• It allows you to test individual class/objects without writing any test drivers.
(Warning: These instructions are meant for Windows users. There is a version of
BlueJ for Macintoshes also, but you'll have to figure it out on your own.)
BlueJ is an interactive development tool that uses Sun’s Java compiler and
interpreter. The BlueJ installation file does not include Sun’s Java compiler &
interpreter. So before you install BlueJ, you must have to install Java in your computer.
What you need is one Java SDK (Software Development Kit). (If you’ve heard people
talking about a JDK, this is an older term for the same thing.) If you already have a Java
SDK, version 1.2 or higher, installed in your system – that will serve the purpose of
working with BlueJ.
0.8.2 To install SDK (before installation of BlueJ):
Visit the Sun web site (http://www.java.sun.com/) and follow the links and instructions to
download and install the most recent SDK, which as of July 2005 is 1.3.1. (I don't
recommend trying a Beta version.)
• The most recent version for Windows as of July 2005 is at
http://www.java.sun.com/j2se/1.3/download-windows.html. Alternately, start at
the main Sun web site (http://www.java.sun.com/), click on "Products and APIs"
at the left, then "Java 2 Platform, Standard Edition", then click on the latest
version of the SDK which is not a Beta version.
• Once you've found that page, scroll down to where it says "Download Java 2
SDK" and click on "continue".
• Read the License Agreement (or scroll down the page saying "yeah, yeah") and
click "Accept".
• Click on one of the download sites to download the installer. This is a very large
file and will take a long time if you're using a dial-up connection (or several
minutes with a broadband connection).
• Create a new folder to hold your SDK-- I'll refer to this as your SDK installation
folder. Find your installation file (called something like j2sdk-1_3_1-
win.exe) with Windows Explorer or My Computer and double-click on it. The
installation process will take a while. Click on "Next" and "Yes" when asked, and
give the name of your SDK installation folder. At one point, you will be shown a
list of components. If you're short of space, you can uncheck everything but
"Program Files". After the installation is done, you will be asked to reboot.
• I strongly recommend downloading a copy of the Java documentation. You can
browse it online, but it's much faster to have a local copy. To get one, go back to
the original download page and click on "Browse and Download Java 2 SDK, v
1.3.1 Documentation". From there, find "Download Java TM 2 SDK 1.3.1 Docs -
HTML Format" and click on "Continue" underneath it. Accept the license policy
and you'll get to a download page. Un-zip it right into your SDK installation
folder and it will create a "docs" sub-folder containing many, many pages of
HTML.
Once you've got the SDK, you can install BlueJ itself. Go to
http://bluej.org/download/download.html the download and installation procedure isn't
too hard, but here are detailed steps:
• Click "Download",
Unfortunately, BlueJ doesn't have the normal help menu stuff we're all used to. It does
have a tutorial in PDF format. (If you don't have a PDF reader, you can get a free one
from http://www.adobe.com/.) It's a pretty good tutorial, but not well organized for
getting started quickly
To download the BlueJ tutorial from the BlueJ web site, click on "Documentation", and
then click on "The BlueJ Tutorial". If you've got your browser set up to display PDF
files directly, you'll see the tutorial.
• There's also a BlueJ Reference Manual available from the Documentation page.
As they warn on the web page, it's a work in progress and some helpful-sounding
sections are not filled in yet, but what's there can be useful. It may be worth
checking back from time to time in case they write more.
To run BlueJ, double-click on the file BlueJ icon. This momentarily creates a DOS
window, which vanishes and starts showing a small BlueJ window.
[A Space-Saving Suggestion: Once you've verified that BlueJ runs correctly, you can
save disk space by deleting or backing up both the BlueJ and the SDK installation files.]
0.8.6 A Quick Start to Using BlueJ:
This section will walk you through writing and running a simple Java program with
BlueJ. Before you start, you need to understand how BlueJ views projects (Picture 0.1).
By project BlueJ means an application development for a particular purpose defining one
or more classes within its folder.
With BlueJ, you can't edit, compile or run a class file existing within a project. If a
program consists of several Java files (or classes), they must be grouped together within
the same project. BlueJ creates a separate folder for each project. As you create,
compile and run the project, you will notice that BlueJ creates all necessary internal files
for that folder.
1. Start BlueJ using your shortcut.
2. Create a new project: from the Project menu, by choosing New Project. You'll
get a file open dialog; this will create the folder [say ToStartWith (Picture 0.2)]
where your project files (i.e. classes) will be stored. The main BlueJ window
always
3. shows you the classes your project contains. There will be as many files as many
new class-names you have entered along with a text file (Picture 0.2). If you
double-click on that text file icon (white sheet with horizontal lines and top right
corner
4. folded), you will see that it is a README.TXT file, in which you can record
information
If you carefully examine first few lines, you will find that the first line starts with
/*.......... ...... and then after a few lines of textual entries finally ends with */ .
This is a comment often used to pass some information to anyone reading the program.
This type of comment is called a multiline comment, which is totally ignored by the
compiler.
Single-line comments can also be used in a source program.
In Picture 0.3 you can find a single line comment as shown below --
// instance variables – replace the example below with your own
Like multiline comment, the compiler also ignores single-line comments. Such
comments add clarity to a program and make software maintenance much easier. Single-
line comments can start from anywhere within a line length. For example --
public class <class-name> { // starts defining a class
.............. } // end of class definition
Besides instance variables, a class may contain constructor(s) and function(s) whose
general structural constructs are also shown in Picture 0.3. Carefully note them for your
future use and guidance.
You can delete or modify any portions not required and can enter a new java source
code according to your requirements. Suppose you would like to create a new java class
"Hello”. Using the main BlueJ menu, create a class named “Hello” and opening the
editor window delete everything written there and enter the following codes only.
public class Hello {
public static void main(String args [])
{
System.out.println ("hello, world!");
} // end main
} // end class Hello
5. Then you save it, and push the Compile button (or type control-K) to compile it.
You'll get a message at the bottom of your screen indicating whether the
compilation was successful.
6. Once you see that your defined class has compiled successfully, close or
minimize the editor window and return to the main BlueJ window.
7. If the compilation is successful, you will see that the diagonal hatch lines have
been removed from the class box. When a compiled class (showing two short
slant lines on its lower right corner) is right clicked, a pop up menu appears.
8. At this point, you may be looking for a "run" button, but there isn't one. Instead,
right-click on the compiled Hello classes box. A menu box will appear showing
an entry for every constructor [starting with new...] (Picture 0.4) and static
method (like void main etc) used in your class. Just click on the void main
(String []) method (Picture 0.4).
A BlueJ: Method Call window will pop up; click OK button. This will run your
Program. A terminal window will pop up to show the output of your program.
Picture 0.4 Pop Up Window for the Hello Class
9. By the same way run your program for a second time. You'll see that the output
from that run will be appended to the output from the first.
To make this program work properly, you'll have to put a copy of the SavitchIn class into
your project folder. From the main BlueJ window, choose Edit | Add Class From
File. Select the java file SavitchIn.java (You can get it from
http://www.cs.queensu.ca/home/cisc124/2001f/SavitchIn.java.). BlueJ will make a copy
of this file and put it into your project folder. Compile both Hello.java and SavitchIn java
files by choosing Tools/Compile from BlueJ main window.
Now you've seen how to create, compile, and run a program using more than one class in
a project. That's enough to start with. BlueJ has other very useful features, which you
will learn gradually.
Java does counting in a sequence of 0,1,2, 3, ...... etc. For that reason, this book
also has started with the chapter 0 that contains all introductory features, facts, figures,
and reports, comments and remarks about Java, JDK and BlueJ.
Although this book is mainly intended for students with no OOP programming
experience, advanced programmers, who are not previously exposed to java language
and BlueJ IDE, will also be benefited.
A beginner is supposed to develop a clear idea about two aspects of Java --- Java
as an Object-Oriented programming language and the beauty of having a support of
Integrated Development Environment (IDE) like BlueJ.
1) Its graphic user interface (GUI) is very simple and easy to use.
2) It supports important teaching tools not available with any other development
environments. BlueJ automatically displays a Universal Modeling Language (UML)
-like representation of classes and relationships between interacting objects involved in
a project. Such Visualization is a great help to both teachers and students in building
foundation concepts.
3) “One of the most important strengths of the BlueJ environment is the user's ability to
create directly objects of any class, and then to interact with their methods. This
creates the opportunity for direct experimentation with objects. Students can almost
'feel' what it means to create an object, call a method, pass a parameter or receive a
return value.”
0.10 The design of this book has made use of “Project-driven approach “
Almost all chapters provide a good number of practical examples with java source
codes. Simple projects have been chosen to illustrate the important concepts discussed
in each chapter. Each concept is revisited in a different context after it is introduced.
The implementation for all the projects is written very carefully, so that many
peripheral issues may be studied by reading the projects' source codes. One cannot deny
the benefits of learning by reading and imitating the sample programming examples.
Chapter 2—Class as the Basis of all Computation opens up class definitions with
encapsulated data and function members. Objects are instances of a class (an
abstraction which can be regarded as a User Defined Data Type). Use of different
primitive data types and operations allowed on them by the java language are also
discussed here citing a good number of programming examples.
Chapter 6 --- Decision Making chapter deals with the conditional decision making
constructs like if-then, if-then-else, switch, etc and their applications in program
control. The special ?: operator is also introduced here.
Chapter 7 --- Iterations chapter shows how to use for loop for a fixed number of
iterations, while loop or do-while loop for any unknown number of iterations. When
to use continue & break statements and how to use nested loops are also discussed.
It is also illustrated how one kind of loop can be converted into another kind, if
required.
Chapter 9 --- Encapsulation, Data Hiding and Inheritance chapter deals with the
private, public scope and visibility rules. Scope of different variables like instance
variables, argument variables, local variables, etc have been discussed with
appropriate examples. Package level access control mechanism has also been
explained. The concept of single and multi-level inheritance has just been introduced
here. The importance of java’s interface has also been explained.
For those who are experienced programmers, two more chapters have been
added with some advanced topics.
Chapter 12 is concerned with On Applets & Windows programming with AWT
and Swing
and
Chapter 13 deals with Object Oriented Analysis & Design using UML.
0.12 Conclusion
This introductory chapter gives a bird’s eye view of the java’s history of evolution and
the importance of BlueJ as an integrated development environment for the beginners. The
importance of java in this age of internet and mobile computing has been clearly spelt
out. Thorough study of the Chapters [1 .... 11] will be essentially required for those
who are studying Java for the first time. Last two chapters are meant for advanced
learners only.
In this chapter some text portions have been marked within “........” to indicate
that those portions have been quoted directly from the official BlueJ site. Please allow
me to thankfully acknowledge the BlueJ open sources. The BlueJ screen shots have been
included here and will be used whereever necessary.
Let me acknowledge, with thanks, the support and encouragement received from one my
ex-students, Dr. Ashok Banerjee.