Introduction to Java

estrapadesherbetΛογισμικό & κατασκευή λογ/κού

18 Νοε 2013 (πριν από 3 χρόνια και 4 μήνες)

62 εμφανίσεις


Introduction to Java





Java can be used to create standalone
programs in the same way as C++ or to
create applets which run within a Web
browser




This course will focus on creating applets




Before a user can run a Java applet, they
must be running a brow
ser that supports
Java (a “Java
-
enabled” browser). Examples
are Netscape Navigator and Microsoft
Internet Explorer



“Java is an object
-
oriented, robust,
secure, multi
-
threaded, interpreted,
platform independent language”



“Object
-
oriented”




Same concept
s as in C++




Main features are encapsulation,
polymorphism and inheritance


Encapsulation




A class could be described as a collection of
data and methods




The methods implement the operations
which can be performed upon the data




The data can only be acces
sed by methods in
the same class




Three main reasons for doing this:


Data hiding


Data security

Faster upgrading




Polymorphism




“having many forms”




Usually refers to identically
-
named methods
that have different behaviour depending
upon the type of ob
ject that they reference
i.e. “method overloading”




Covered in more detail later on in the course



Inheritance




Allows you to re
-
use existing classes by
extending or customising them




Re
-
usability is a fundamental OO concept. It
saves programming time so

you can create
new applications/applets much more quickly




Java has existing super classes e.g. “Applet”



“Robust ”




It’s still possible to write bug
-
ridden
software with Java but certain types of
programming errors are eliminated




More robust than C++
e.g.




More strongly
-
typed


extensive compile
time checking for type mis
-
matches




All accesses to arrays and strings are
checked at run
-
time to ensure that they are
within bounds, eliminating overwriting
memory and corrupting data




No pointers so pointer r
elated bugs are
eliminated




Casts of one type to another are checked at
runtime to ensure that they are legal




Automatic garbage collection




Simpler error handling and recovery with the
exception handling feature





“Secure ”




Downloading programs to a us
ers web site
creates security problems:




The program could introduce a virus onto
the user’s computer




The program could transfer information
stored on the user’s system back to the
server




Java eliminates these problems by not
allowing a Java applet to re
ad from or write
to files on a user’s system


“Multi
-
threaded ”




The instructions that are being executed by a
program can be called the program’s “thread
of execution”




You may want a program to perform more
than one task at the same time. For example,
a
n applet could spin a logo as it flashes the
name of a company on the screen while yet
another image is being downloaded by the
browser. One thread could spin the logo,
another thread could flash the company’s
name and yet another thread could download
th
e image




Of course, the processor can really only
execute one thread at a time but by “time
-
slicing” will give the impression that all
three threads are being executed at the same
time




It is possible to use multi
-
threading with
C++ but Java makes it much
easier





“Interpreted and platform
independent”




A Java program will run on any system type
e.g. PC, Mac, UNIX based i.e. it is platform
independent so the applet programmer only
needs to create one applet




Other language compilers create machine
code wh
ich is processor specific. Java is
different. The Java compiler produces an
intermediate code called “virtual machine
code” (VMC)




VMC is not processor specific so there is
another stage to go through before the
program can be run by a particular
processor
. This is done by the browser when
it downloads the VMC. The browser
interprets the VMC into specific machine
code for the processor which is being used
by the user.




Because of this two
-
stage conversion
process, Java programs will not run as fast
as those

compiled for a specific processor


A First Java Applet


// a first program in Java

import java.applet.Applet; //import Applet class

import java.awt.Graphics; //import Graphics class


public class HelloWorld extends Applet

{

public void paint(Graphics g
)


{


g.drawString(“Hello World”,5,25);


}

}


(a)
//

indicates a programmers comment (as in
C++)


(b)
import statements




Import the necessary classes into your program.
These lines tell the compiler that class Applet is
to be found in package java.applet a
nd that class
Graphics is found in package java.awt




Importing the Applet class allows you to build
your own applet




Importing the Graphics class allows your applet
to draw graphics on the screen


(c)

public class HelloWorld extends Applet




This is a class

definition




The
class
keyword introduces the definition




HelloWorld

is the name of the class. When you
save an applet in a file the file name must be the
same as the class name with a .java extension so
the example file name would be
HelloWorld.java. It
is case sensitive.




extends Applet

indicates that the new class will
be derived from a super (base) class named
Applet.
The Applet class provides the basic
functionality that every Java applet needs.





The
public
keyword allows the browser to create
an ins
tance (an object) of the class HelloWorld.
Java also has
private

and
protected

keywords
and these are used much in the same way as
C++. These are covered in more detail in later
lectures.


(d)

{ }




Begins and ends the body of a class or method
definition


(e)
public void paint(Graphics g)




Introduces a method into the HelloWorld applet



public

allows the browser to use it



void
indicates that this method will not return
any results



paint
is the name of the method. The paint
method’s task is to draw graphics

on the screen.
This method is different to ‘normal’ methods as
it does not have to be called by the applet. It is
spontaneously called by the browser when it
starts up the applet and again if the applet
becomes covered by another window and then
re
-
expos
ed. The paint method can be called by
an applet but it is not recommended. If different
information is to be displayed on the screen, for
example after an input from the user, then the
repaint

method should be used instead. This
method erases any informa
tion that was
previously displayed on the screen and then calls
paint

again




(Graphics g)

The paint method needs a
Graphics object to be able to execute it’s task and
this is passed as a parameter named
g.
Again, the
applet does not have to explicitly pa
ss this
information to the method because the browser
will do this automatically when it uses the
method


(f)
g.drawString(“Hello World”,5,25);




This is the only programming statement in the
applet




The Graphics object
g

calls its
drawString

method. The
call has three parameters: the
character string to be displayed and the two
screen co
-
ordinates




As you can see, unlike paint ,drawString is not
called automatically


Another Java applet


import java.applet.Applet; //import Applet class

import java.awt.G
raphics; //import Graphics class

public class HelloWorldWithFont extends Applet

{

public void paint(Graphics g)


{


Font font = new Font(“Arial”,16);


g.setFont(font);


g.drawString(“Hello World”,5,25);


}

}




As you can probably work out, this time “Hello

World” will be displayed in 16 point Arial font




The paint method creates a
Font

object named
font
with the specified values. The Graphics
class has another method named
setFont

and so
the Graphics object
g
can call this method.




Clearly, the setFont met
hod needs a Font object
to be able to execute its task and so font is passed
to it when the method is called






An applet which uses variables


import java.applet.Applet; //import Applet class

import java.awt.Graphics; //import Graphics class

public c
lass AddTwoNumbers extends Applet

{

int number1= 2;

int number2 = 3;

int sum;

sum = number1 + number2;

public void paint(Graphics g)


{


Font font = new Font(“Arial”,16);


g.setFont(font);


g.drawString(“The sum is ”,+ sum,5,25);



}


}




As you can see, u
sing variables in Java is very
much like using variables in C++




You can use a combination of letters, numbers
and the under
-
score for a variable name. You
cannot begin a variable name with a number.
Java is case
-
sensitive.




Notice the use of the + sign b
efore the name of
the variable
sum
. This is necessary because the
method drawString does what it says


it draws a
string of characters. The value of sum is
implicitly converted to a string and this is then
concatenated to the string “The sum is ”.


Creat
ing an HTML file




A Java applet runs within a browser so you will
need to create an HTML file to load the applet
into the browser




The following HTML file will load the first
example program into the browser:


<HTML>

<TITLE> HelloWorld Applet </TITLE>

<APP
LET CODE = “HelloWorld.class” WIDTH =






275

HEIGHT = 55>

</APPLET>

</HTML>




Lines 1 and 5 indicate the beginning and end of
the html file




Line 2 indicates the title of the html file




Lines 3 and 4 are special tags for Java applets.
They tell the browse
r to load a specific applet
and define the size of the applets display area in
the browser. generally, an applet should be less
than 640 pixels wide and 480 pixels tall




The first piece of the applet tag
CODE =
HelloWorld.class
indicates that the file
Hel
loWorld.class contains the compiled applet
class. When you compile a Java program each
class is put into a separate file that has the same
name as the class with a .class extension




The html file does not have to be given the same
name as the applet but it

makes sense to give
these files meaningful names.




The JDK provides a special program called the
appletviewer
with which you can test your
applets. This is like a browser that only knows
how to interpret Java applets




The appletviewer is invoked for the H
elloWorld
class from the computers command line as
follows:(assuming the html file had been saved
as HelloWorld.html)

appletviewer HelloWorld.html




Further instructions on html files will be given, if
you need them, in labs