The Program PrintText.java

Download Report

Transcript The Program PrintText.java

Java the UML Way
http://www.tisip.no/JavaTheUMLWay/
Introduction
Preliminaries for reading this book
Familiarize yourself with the book and the web page!
We can contemplate the computer as layered
Two types of Java programs
A small program (an application)
From source code to runnable program
Running an applet
version 2002-04-17
page 2
page 3
page 4
page 5
page 6-7
page 8-9
page 10
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
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 type
commands to navigate the files. An advantage
to know a few such commands.
You know a little about the computer’s
construction:
–
–
microprocessor
internal memory (RAM) and secondary memory
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
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.
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
We Can Contemplate the Computer as ”Layered”
Graphical windows system
Command shell
textual user interface,
many available commands,
each of them corresponding to
several kernel commands
Kernel
fopen,
fclose
a small amount of commands
(”calls)” working on the internals
of the computer
C:\DOS> dir
The distinction between the layers isn’t clear
on all systems. In some systems, however, the
distinction is clear enough to easily switch
windows systems.
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
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.
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
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:
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
The Program PrintText.java
class name
print one
line
file name same as
class name,
with extension java.
/*
* PrintText.java VBH 2001-08-28
* Prints text several times
*
comments
*/
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!");
}
}
do next thing 10 times
printout when running
the program
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/
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!
Chapter 1, page 7
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
class HelloW...
public static ...
Byte code,
HelloWorld.class.
Compiling
#¤¤£@&%#¤#
¤()
Running by an interpreter
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
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
Our first program...
c:\javaprograms> java PrintText
The result is displayed in a window, and looks like this: About to learn java!
About to learn java!
In a tool/editor:
About to learn java!
About to learn java!
– Many editors let you compile and run directly from
About to learn java!
where you type the program.
About to learn java!
About to learn java!
About to learn java!
About to learn java!
About to learn java!
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
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
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
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.
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