11
version 2002-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 1 Java the UML Way http://www.tisip.no/JavaTheUMLWay/ Introduction Preliminaries for reading this book page 2 Familiarize yourself with the book and the web page! page 3 We can contemplate the computer as layered page 4 Two types of Java programs page 5 A small program (an application) page 6-7 From source code to runnable program page 8-9 Running an applet page 10

Java the UML Way version 2002-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and

Embed Size (px)

Citation preview

Page 1: Java the UML Way  version 2002-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and

version 2002-04-17Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal.

ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/

Chapter 1

Java the UML Wayhttp://www.tisip.no/JavaTheUMLWay/

Introduction

Preliminaries for reading this book page 2Familiarize yourself with the book and the web page! page 3 We can contemplate the computer as layered page 4Two types of Java programs page 5A small program (an application) page 6-7From source code to runnable program page 8-9Running an applet page 10

Page 2: Java the UML Way  version 2002-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal.ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/

Chapter 1, page 2

Preliminaries for Reading This Book

• You have used a computer for a while. You know– the use of Windows incl. Windows Explorer, cut and paste, etc.

– the terms file, directory and sub-directory

• You know a little about the significance of a file’s extension, examples:– Windows program files have the extension exe.

– Files with the extension doc are data files - input for the application Word.

– Note the difference between program files and data files.

• You have some experience in the use of the Internet.

• Windows has a graphical user interface.

• MS-DOS-prompt is an example of a textual user interface, where we typecommands to navigate the files. An advantageto know a few such commands.

• You know a little about the computer’sconstruction:

– microprocessor

– internal memory (RAM) and secondary memory

Page 3: Java the UML Way  version 2002-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal.ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/

Chapter 1, page 3

Familiarize Yourself with the Book and the Web Page!

• The web page: http://www.tisip.no/JavaTheUMLWay/

• Every chapter has the following elements:– Introduction with learning-goals

– The chapter text• Most sections end with small problems. The most important ones are included

in this series of slides, connected to the relevant text. Solutions to the problems are on the web page.

• All numbered example programs are on the web page, for downloading and running.

– At the end of every chapter • Review problems, covering the most important parts of the chapter.

• Programming problems. Solutions for many of these are on the web page.

• List with explanations of new concepts.

• And remember: It is not possible to become a good programmer by reading. You have to program a lot.

Page 4: Java the UML Way  version 2002-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal.ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/

Chapter 1, page 4

We Can Contemplate the Computer as ”Layered”

Graphical windows system

Command shell

Kernel

fopen, fclose

C:\DOS> dir

a small amount of commands(”calls)” working on the internals

of the computer

textual user interface, many available commands,

each of them corresponding toseveral kernel commands

The distinction between the layers isn’t clearon all systems. In some systems, however, the distinction is clear enough to easily switch windows systems.

Page 5: Java the UML Way  version 2002-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal.ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/

Chapter 1, page 5

Two Types of Java Programs

• Java applets– When you fetch a page on the web, the page is transferred from a server

machine (where the page is stored) to a client machine (your computer).– You read the web page by means of a program called a browser (i.e.

Netscape or Microsoft Explorer).– The page may refer to a (a bit special) type of program file stored on the

server machine.– The program file is transferred to the client machine, and the browser runs

the program.– Such a program is an applet, and is stored on a file with the extension

class.

• Java applications– These are ”ordinary programs”, not only for the Internet.– Has the extension class, not exe.– Run by a program called java.– Not necessarily connected to the Internet at all.

Page 6: Java the UML Way  version 2002-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal.ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/

Chapter 1, page 6

A Small Program (an Application)

• We type in the program as plain text.

• We can’t add formatting such as italics or different fonts.

• Therefore we don’t use a word processor, but a text editor, when typing in programs. Notepad, WinEdit and TextPad are examples of text editors. TextPad:

Page 7: Java the UML Way  version 2002-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal.ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/

Chapter 1, page 7

The Program PrintText.java

/* * PrintText.java VBH 2001-08-28 * Prints text several times**/class PrintText { public static void main(String[] args) { System.out.println("Our first program..."); for (int i = 0 ; i<10 ; i++) System.out.println("About to learn Java!"); }}

class name comments

file name same asclass name,

with extension java.

print oneline

do next thing 10 times

Our first program...About to learn java!About to learn java!About to learn java!About to learn java!About to learn java!About to learn java!About to learn java!About to learn java!About to learn java!About to learn java!

printout when runningthe program

Page 8: Java the UML Way  version 2002-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal.ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/

Chapter 1, page 8

From Source Code to Runnable Program

• The program code we type in is called source code.• It is a good habit to put the source code on a file with the same name as the class.

Many tools enforce this. The file name must have the extension java. Here: PrintText.java. Mind the upper and lower case.

• Java programs must be translated into byte code. • The byte code can be run on several types of computers.

– Necessary when applets are downloaded from the Internet.

• The Java interpreter translates into the relevant machine code.• Programs written in other programming languages are often translated directly into

machine code (the language which the microprocessor uses).

Source code, HelloWorld.java

Byte code, HelloWorld.class.

class HelloW... public static ...

#¤¤£@&%#¤#¤()Compiling Running by an interpreter

Page 9: Java the UML Way  version 2002-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal.ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/

Chapter 1, page 9

How to Run the Program (Application)

• We need– A program which translates from source code to byte code: javac.exe– A program which takes the byte code and runs the program: java.exe - (The Java

interpreter).

• In an MS-DOS window:– Assume that the file PrintText.java is in the directory c:\javaprograms.– Go to this directory:

• c:\windows>cd c:\javaprograms• c:\javaprograms> javac PrintText.java• c:\javaprograms> java PrintText• The result is displayed in a window, and looks like this:

• In a tool/editor:– Many editors let you compile and run directly from

where you type the program.

Our first program...About to learn java!About to learn java!About to learn java!About to learn java!About to learn java!About to learn java!About to learn java!About to learn java!About to learn java!About to learn java!

Page 10: Java the UML Way  version 2002-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal.ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/

Chapter 1, page 10

Running an Applet

• An HTML file must contain a reference to the applet:

– <APPLET CODE=”SimpleApplet.class" CODEBASE="." WIDTH=400 HEIGHT=300></APPLET>

• Let’s name the HTML file SimpleApplet.html. It can be run in more than one way:

– With the program appletviewer:• From the MS-DOS prompt: >appletviewer SimpleApplet.html

• From a tool: You have the applet’s source code in a window. You press for compiling and running of it. Often, a tiny HTML file is created on the fly, and this is passed to appletviewer.

• appletviewer only shows the applet, not the other parts of the web page (HTML file).

– The file can be opened in a web browser:• file:///J|/USER/JavaBook/ExCh4/SimpleApplet.html

– The file can be retrieved from a web server:• http://www.aitel.hist.no/~else/SimpleApplet.html

Page 11: Java the UML Way  version 2002-04-17 Only to be used in connection with the book "Java the UML Way", by Else Lervik and

Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal.ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/

Chapter 1, page 11

Before continuing you have to familiarize yourselves

with the tools you are going to create your Java programs with.

Do the downloading and installation, if not already done. See Appendix A.

Make the program on page 10 work, so that the printout becomes like the one on page 11.

If you use the SDK from Sun, try to use the procedure on pages 13-14.

Find the book’s web page, and find your way around. Download the solutions for the small problems.