Introduction to Java Programming - Lecture Notes | CS 122, Study notes of Computer Science

Material Type: Notes; Class: Computer Programming II; Subject: Computer Science; University: Pace University-New York; Term: Fall 2005;

Typology: Study notes

Pre 2010

Uploaded on 08/09/2009

koofers-user-rnu
koofers-user-rnu 🇺🇸

8 documents

1 / 100

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Pace University
Introduction to Java Programming1
Dr. Lixin Tao
http://csis.pace.edu/~lixin
Computer Science Department
Pace University
October 10, 2005
Table of Contents
1 Computer Systems and Programming Languages .................................................................. 1
1.1 Problem vs. problem instances ....................................................................................... 1
1.2 Structure of a personal computer .................................................................................... 1
1.3 CPU................................................................................................................................. 2
1.4 Memory........................................................................................................................... 3
1.5 Disk file system............................................................................................................... 5
1.6 Display ............................................................................................................................ 7
1.7 Keyboard......................................................................................................................... 8
1.8 Computer Software ......................................................................................................... 8
1.9 Java programming language ........................................................................................... 9
2 Software Installation ............................................................................................................. 12
2.1 Installation of Java J2SE 5 SDK................................................................................... 12
2.2 Customizing properties of Windows Explorer.............................................................. 14
2.3 EditPad installation and customization ........................................................................ 17
2.4 Typical process for creating and running a Java program ............................................ 17
2.5 A basic tutorial on DOS commands.............................................................................. 19
2.6 A note for storing programs on Lab PCs ...................................................................... 20
3 Java Basics............................................................................................................................ 21
3.1 Basic Hello World program.......................................................................................... 21
3.2 Code style and command-line arguments..................................................................... 23
3.3 Java package ................................................................................................................. 25
3.4 Best practice for organizing multiple class projects ..................................................... 28
3.5 Local variables, expressions, and assignment............................................................... 30
3.6 Basic built-in data types and type casting..................................................................... 33
3.7 Command-line arguments and loops ............................................................................ 38
3.8 Calculator with if-else statements................................................................................. 41
3.9 Calculator with switch statement.................................................................................. 44
3.10 Calculator with exception processing ........................................................................... 47
Copyright Dr. Lixin Tao, Pace University, [email protected]. This document should not be copied partially or in
full, or published in any form, without the permission of the author.
This tutorial is the core teaching material for Dr. Lixin Tao’s section of CS121. It has precedence over the
textbook. All course work, including assignments, projects, quizzes, and the final exam, is based on this tutorial.
This tutorial will grow as the class progresses in Fall 2005. Make sure you download its latest version from time
to time from the Course Documents of Pace Blackboard for CS121.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Introduction to Java Programming - Lecture Notes | CS 122 and more Study notes Computer Science in PDF only on Docsity!

Pace University

Introduction to Java Programming

1

Dr. Lixin Tao http://csis.pace.edu/~lixin Computer Science Department Pace University

October 10, 2005

Table of Contents

1 Computer Systems and Programming Languages .................................................................. 1 1.1 Problem vs. problem instances ....................................................................................... 1 1.2 Structure of a personal computer .................................................................................... 1 1.3 CPU................................................................................................................................. 2 1.4 Memory........................................................................................................................... 3 1.5 Disk file system............................................................................................................... 5 1.6 Display ............................................................................................................................ 7 1.7 Keyboard......................................................................................................................... 8 1.8 Computer Software ......................................................................................................... 8 1.9 Java programming language ........................................................................................... 9 2 Software Installation ............................................................................................................. 12 2.1 Installation of Java J2SE 5 SDK................................................................................... 12 2.2 Customizing properties of Windows Explorer .............................................................. 14 2.3 EditPad installation and customization ........................................................................ 17 2.4 Typical process for creating and running a Java program ............................................ 17 2.5 A basic tutorial on DOS commands.............................................................................. 19 2.6 A note for storing programs on Lab PCs ...................................................................... 20 3 Java Basics ............................................................................................................................ 21 3.1 Basic Hello World program .......................................................................................... 21 3.2 Code style and command-line arguments ..................................................................... 23 3.3 Java package ................................................................................................................. 25 3.4 Best practice for organizing multiple class projects ..................................................... 28 3.5 Local variables, expressions, and assignment............................................................... 30 3.6 Basic built-in data types and type casting..................................................................... 33 3.7 Command-line arguments and loops ............................................................................ 38 3.8 Calculator with if-else statements................................................................................. 41 3.9 Calculator with switch statement .................................................................................. 44 3.10 Calculator with exception processing ........................................................................... 47

  • Copyright Dr. Lixin Tao, Pace University, [email protected]. This document should not be copied partially or in full, or published in any form, without the permission of the author.
  • This tutorial is the core teaching material for Dr. Lixin Tao’s section of CS121. It has precedence over the textbook. All course work, including assignments, projects, quizzes, and the final exam, is based on this tutorial.
  • This tutorial will grow as the class progresses in Fall 2005. Make sure you download its latest version from time to time from the Course Documents of Pace Blackboard for CS121.

ii

  • 3.11 Conditionally interrupting loops
  • 3.12 Arrays............................................................................................................................
  • 3.13 Method declaration and invocation...............................................................................
  • 3.14 Java documentation comments
  • 3.15 Scopes of local variables...............................................................................................
  • 3.16 Formatting output..........................................................................................................
  • 3.17 Timing the evaluation of π............................................................................................
  • 3.18 Matrix multiplication
  • 3.19 Interactive command-line data input
  • 3.20 Window-based data input/output
  • 3.21 Text file input/output
  • 3.22 Recursive vs. iterative methods: factorial.....................................................................
  • 3.23 Recursive vs. iterative methods: Fibonacci numbers....................................................
  • 3.24 Algorithm and Towers of Hanoi
  • 3.25 Bubble sort
  • 3.26 Binary search

Computer hardware is typically made up of one CPU (central processing unit) for computing, one main memory for storing programs and data that are processed by the CPU, a hard disk for storing programs and data on permanent basis (surviving power shutdown), a keyboard for getting user input, and a display for displaying computer output. All data processing is conducted by the CPU. When you shutdown your computer, all data in the CPU and main memory will be lost. But programs and data saved on a hard disk will not be affected by power shutdown. While hard disk is safe for storing large amount of programs and data, it is too slow relative to the CPU’s working speed. The main memory functions like a high-speed copy of the active (those currently processed by the CPU) programs and data for improving the CPU’s performance.

Figure 2 Computer organization

The CPU, the (main) memory, the (hard) disk, the keyboard, and the display are all devices connected through a system bus , as shown in Figure 2. At any instance, only one device can broadcast data on the system bus, but every device on the system bus can listen to the data broadcast on the system bus.

1.3 CPU

All computing is conducted by the CPU. CPU is like the brain or control center of a computer.

A CPU is designed to understand a small set of machine-language instructions. The CPU is responsible for reading successive instructions, as well as data needed by the instructions, of a program stored in the main memory and carrying them out. All CPU activities are synchronized by the cycles of a digital clock. This clock repeats CPU cycles, and the number of CPU cycles repeated during each second is called the number of Hz indicating the speed of the CPU.

A CPU uses a program counter ( PC ) to remember the address for the next program instruction in the memory to be executed. Just before the execution of a program, the program counter is set the memory address of the first (in logical sense, not necessarily the physical first instruction; for example, a program may start its execution from instruction 5) instruction of the program to be executed. After the execution of each machine instruction, the value of the program counter is updated for the address of the next instruction to be executed. Since normally the instructions of a program are stored in successive memory locations and are supposed to be executed in that order, the new value of the program counter is typically 1 plus its old value. If a program needs

CPU

PC

(^01) (^23) (^45) (^67) (^89) (^1011) (^1213) (^1415) 16

Program

Data 2

Programs Data

Disk

Keyboard Display

System Bus

M[9]M[10]←← (^21) M[11]←M[9]+M[10]

(^21) 3

to jump out of order (say jumping back by ten instructions) to execute an instruction at a particular memory address (because some condition or event happened), the particular memory address will be set in the program counter for replacing the default successive address.

A CPU uses a few (from 8 to around 100) registers for speeding up its work. A register is a small data storage just large enough to hold a memory word. Registers are really scratch paper for storing the instruction under execution and the intermediate results of instruction execution. For example, to execute instruction “M[11]←M[9]+M[10]”, this instruction will first be copied in an Instruction Register (IR), two general-purpose registers will be used to hold value copies of memory words M[9] and M[10], another general-purpose register will be used to hold the summation of the previous two registers, and resulting value of the last register will be then copied into memory word M[11]. Since the registers are part of the CPU chip, they work at the same speed as the CPU cycle, which is much faster than the memory cycles (the heart-beats used to synchronize memory accesses).

The execution of an instruction by the CPU is conducted in a few phases synchronized by the CPU clock cycles, as described below, and these phases repeat to execute a program.

  1. The program counter’s value is used as a memory address to read out the instruction from the memory location with that address, and the instruction is saved in the CPU’s Instruction Register (IR).
  2. The program counter’s value is set to the memory address for the next instruction to be executed.
  3. The value of IR is decoded, and the data needed for the execution of the instruction is read from memory and copied into some general-purpose registers.
  4. The operation of the instruction is carried out and the result data is saved in a general- purpose register.
  5. The result data is copied from the general-purpose register to its destination memory location.
  6. Go back to step 1.

The speed of a CPU is determined by CPU cycles per second, and a CPU cycle is the basic time unit for the CPU to do some work. Each machine instruction takes one to ten cycles to get executed (to make our examples simpler, we assume each machine instruction takes one CPU cycle to get executed). Today’s PC CPUs normally have a cycle of 1.5 GHz to 3.5 GHz, where a GHz is one giga Hertz, or 2 30 cycles per second.

1.4 Memory

The basic data storage unit of a computer is a bit. A bit can store either 0 or 1, which may be used to represent false and true, or off and on of an electrical switch, respectively.

Eight consecutive bits make a byte. A byte can hold an integer of values between 0 and 255. The value of a byte can represent a key that we type on the keyboard.

A computer word consists of two bytes (16 bits, used by CPUs of 1970s), four bytes (32 bits, typical of today’s PC that will soon be outdated), eight bytes (64 bits, for the coming generation of PCs and for business computing; great effect for dynamic computer games), or sixteen bytes

Figure 3 Memory with program and data

Figure 3 shows an example main memory with 17 words. The first 9 words, at addresses 0 through 8, are storing a program. The last 8 words, at addresses 9 through 16, are storing data. At the very beginning, the program counter PC has value 0, and machine instruction “M[9] ← 2” in memory word M[0] is executed, which stores integer 2 into memory word M[9], the memory word with address 9. Then the program counter PC automatically increases its own value by 1 to become 1 and points to the next machine instruction. In the next CPU cycle, machine instruction “M[10] ← 1” in M[1] is executed, which stores integer 1 in memory word M[10], the memory word with address 10. The program counter PC then automatically increases its own value by 1 to become 2 and points to the next machine instruction. In the next CPU cycle, machine instruction “M[11] ← M[9] + M[10]” in M[2] is executed, which retrieves values in memory words M[9] and M[10], adds them together, and stores the summation result into memory word M[11], the memory word at address 11.

1.5 Disk file system

Hard disk is used for storing programs and data on long-term basis. Programs and data stored on a hard disk can survive power shutdowns. Today’s PC hard disks have capacities from 10 GB to 300 GB, where GB means giga bytes, or 2^30 bytes. Today’s computers normally boot up from its primary hard disk, where a reserved disk space is for storing a special program for loading the core of an operating system into the main memory.

The programs and data are stored on hard disks in the unit of files. A file is a named unit of binary or alphabetic (text) data of any length. The name of a file typically has two parts separated by a period: the file name stem , and the file name extension. For example, a Java source code file may have name “Welcome.java”, where “Welcome” is the file name stem, which is supposed to be a concise name characterizing the purpose of the file; and “java” is the file name extension, which indicates the type of data that is stored in this file. By convention, “java” as a file extension is reserved by Java source code files. The file name extension tells a computer how to interpret the data stored under a particular file name.

0 1 2 3 4 5 6 7 8 9

10 11 12 13 14 15 16

Program

Data

M[9]← 2 M[10]← 1 M[11]←M[9]+M[10]

2 1 3

Since we are encouraged to choose meaningful file names, it is possible that we incidentally create two files with the same name, leading to file name conflicts (the data of the second instance will replace that of the first instance). This leads to the introduction of the concept of file system directories. A file system directory can contain multiple files as well as other directories. A file system directory is a special file containing a table of contents for all files under its control. Two different files can have the same name as long as they belong to two different directories. File system directories are also referred to as file system folders. When a file directory is deleted, all of its contents are deleted too.

Figure 4 A sample file system

The file directories of a PC constitute a forest of directory trees, one tree for each hard disk partition (a disk partition is a logical disk; a hard disk can contain multiple disk partitions). In computer science, the root of a tree is drawn at the top, and its sons and descendants below the root. All the entities in a tree are called nodes. In our case, a node is either a file or a directory. There is a line connecting a father node and a son node in the trees. Figure 4 is a simplified file system for a particular PC. We assume that the PC has two hard disk partitions “C:\” and “D:\”, and they are the roots of their own directory trees respectively. Those nodes of the trees with names in Italic font are files, and the others are directories. The absolute path of a file or directory is the concatenation of all the node names on the path (connected lines) from the tree root to the file or directory, separated by “\” in Windows and “/” in Unix/Linux. For example, the absolute path of file “Welcome.java” in our example is “C:\cs121\basics\project\Welcome.java”. When we work in a Command Prompt window, the Command Prompt window will work in a particular file system directory called the current working directory , for each directory tree. Inside a working directory, you can use relative path to denote a file or directory under the sub- tree rooted at the current working directory by concatenating all the node names on the path from the current working directory to that specific file or directory, excluding the current working directory, separated by “\” in Windows and “/” in Unix/Linux. As an example, if the Command Prompt window is currently working in directory “C:\cs121\basics”, then the relative path of file “Welcome.java” from the current working directory is “project\Welcome.java”; and if the Command Prompt window is currently working in directory “C:\cs121\basics\project”, then the relative path of file “Welcome.java” from the current working directory is “Welcome.java”. Inside a Command Prompt window, we can change the working directory by using the “cd” (change directory) command. There are two special symbols used for representing two relative

C:\

cs

basics

project

homework

Program Files

Java

jdk1.5.0_

classes

project

Welcome.class

D:\

cs

classes projects

bin lib jre

javac.exe

java.exe

java.exe bin^ lib

Welcome.java

Figure 5 Display coordinate system with a pixel at (3, 2)

A typical VGA display can support an array (matrix) of 640 x 480 pixels (640 rows, each row having 480 pixels), where each pixel can display various colors. Most of today’s displays have much higher screen resolution , which means the number of pixels per unit length in each row or column of the screen.

1.7 Keyboard

A keyboard is used to enter input data to a program running on a computer. A computer may run several programs at the same time, but only one program is the active program for receiving input data from the keyboard. Some programs run in windows. To make the program behind a window active, just click any where in the window.

The keyboard is used to enter various characters including alphabetic characters like “a, b, A, B”; numeric characters like “1, 2, 3”; punctuation keys like “. , :”; and special function characters like PageUp, PageDown, Ctrl (Control), Alt (Alternative), and Esc (Escape). While most key strikes will generate visible characters, some will not, as Ctrl and Alt.

The space key (the long key at the bottom middle of the keyboard), the Enter key ( new line key), and the Tab key can be used to generate white spaces. A single space key strike will insert a single space character at the current cursor location of the display. A single Enter key strike will insert a new line character. A single Tab key strike will insert a single Tab character, or a predefined number of space characters. We say that the space key, the Enter key, and the Tab key all generate white spaces.

1.8 Computer Software

Computer software is computer programs. There are two categories of software: system software and application software. System software is for managing computer system hardware and software for their more efficient usage, and providing proper abstraction of the computer system for easing the work of application developers and end users. The most important example for system software is operating systems like Windows, Unix, and Linux. Java compiler and Java virtual machine, as well as C/C++ compilers, are also system software. Application software is

x

y

software for solving problems in a particular application domain, like word processing, games, engineering computation, and bank transaction processing.

Computer software is implemented with programming languages. The computer hardware can only understand very primitive machine instructions like adding two integers. While in theory we can implement all software in machine instructions, in practice it will be too complex and we may not be able to write correct programs when they are large. High-level programming languages like C, C++, and Java allow programmers to write readable programs with powerful statements (similar to instructions or commands), and let a compiler or an interpreter to convert the programmer-friendly programs into machine instructions before they are executed (run). Java is one of the most important high-level programming languages.

1.9 Java programming language

Java is a high-level programming language designed for problem solving. Unlike natural languages that use very flexible set of vocabulary and flexible syntax, a programming language uses a small set of key or reserved words and arranges these reserved words in very strict patterns to compose statements.

The computer hardware can only understand a set of low-level instructions of the so-called machine language. Different computer hardware architectures define their unique machine languages. Instructions at this level can only deal with basic operations like reading a key strike on the keyboard, saving a number at a particular memory address, reading value from a particular memory address, and adding two numbers. To solve a problem, a programmer needs to write a very long sequence of such machine language instructions called a machine-language program. Given the program and input data, a computer can compute the output.

While a machine language is native to the computer hardware and therefore can be executed most efficiently, the complexity of machine-language programs is overwhelming. This motivated the creation of various high-level programming languages. Java is one of the latest high-level programming languages. In a high-level language, the instructions are called statements. Each high-level statement is the abstraction of a sequence of machine instructions. As a result, a high- level program is much shorter than its machine-instruction counter-part, and it is much easier to write and maintain (understand and modify).

But a computer cannot understand a high-level language program directly. A high-level language program is called source code , which needs be translated into machine instructions, also called executable code , before computer hardware can understand and execute it.

A compiler is a computer program that transforms a high-level language source code into machine-language executable code , and saves the executable code in a computer file ( executable file ) for later execution. The translation process takes time. For the same source code, the compiler needs only to translate it once. Because the compiler can carefully analyze the complete source code, a compiler can generate very efficient machine-language code in the sense that the latter is shorter and executes faster on the computer hardware. Given a new set of input data, we only need to run (execute) the machine-language code on it (feed the data to the executable code) to get computation results. Languages aiming at high execution efficiency, including C, C++, COBOL, FORTRAN, and Pascal, usually use this compilation approach.

purposes. Identifiers “class”, “public”, “static”, “void”, and “package” are examples of reserved words. Reserved words are all in lower-case. Identifiers can be of any length.

At a higher level, a Java program is made up of one or more classes, and each class contains one or more methods. A method is a sequence of operations to be executed by the CPU. A class is a container for holding related methods. Each class has a name with the first letter capitalized. Each method has a name with the first letter in lower-case. The classes of a program may be distributed in a few Java source code files. Java also uses variables for holding intermediate values. A variable’s name is an identifier with the first letter in lower-case.

2 Software Installation

2.1 Installation of Java J2SE 5 SDK

  1. Visit “Start|Settings|Control Panel|Add/Remove Programs” to remove any of existing installations of Java SDK or JRE (Java runtime environment). Make sure that you reboot the system before you install any new software. After rebooting, delete the installation directories for those removed installations. Let a PC have multiple Java installations may lead to confusion.
  2. Visit http://java.sun.com/j2se/1.5.0/download.jsp with a Web browser. Click on hyperlink “Download JDK 5.0 Update 5” to get to the J2SE 5 SDK download page. Check the checkbox for accepting license agreement. In the section for “Windows Platform – J2SE (TM) Development Kit 5.0 Update 5” section, click on the hyperlink “Windows Offline Installation, Multi-language” to download the installation file “jdk-1_5_05-windows- i586-p.exe” to your PC. The file has 57.49 MB.
  3. In a Windows Explorer , click on the downloaded file “jdk-1_5_05-windows-i586-p.exe” to start its installation. Accept the license agreement, and take all default values. By default, your J2SE 5 installation directory will be “C:\Program Files\Java\jdk1.5.0_05”. The installation of J2SE 5 will take about 450 MB of disk space. The installation of JDK 1.5 includes the installation of an embedded JRE (Java Runtime Environment) 1.5. After the JDK 1.5 is installed, you will be prompt for installing an external version of JRE into “C:\Program Files\Java\jre1.5.0_05”. This external JRE’s installation is important to you if you need to enable Java support in your Web browsers.
  4. To run Java compiler javac and interpreter java on the command-line, you need to add the bin directory of your J2SE 5 in the value of the environment variable PATH. An environment variable is a memory location where the operating system keeps some values for programs to access. When you type a command “javac” and then Enter in a Command Prompt window, for example, the operating system will try to find whether there is an executable file named “javac.exe”, “java.bat”, or “javac.com” with the value of your PATH. The value of PATH is a sequence of directories separated by semicolon “;” in Windows and colon “:” in Unix/Linux. Period (.) is used to represent the current working directory (the directory in which the Command Prompt window is working). The operating system will sequentially try to find an executable file for “javac” in the directories listed in the value of PATH, from left to right. The first executable file with file stem name “javac” will be executed. If the operating system completed the search in all directories on the PATH (specified by the PATH’s value) and none of the executable files for “javac” could be found, the operating system will declare that “`javac’ is not recognized as an internal or external command, operable program or batch file .”

Right-click on “My Computer” and choose “Properties” from the popup menu. In the “System Properties” window, click on the “Advanced” tab. Click on the “Environment Variables” button. In the bottom “System variables” area of the “Environment Variables” pane, click on button New to launch the “New System Variable” pane. Enter value “JAVA_HOME” for variable name, and value “C:\Program Files\Java\jdk1.5.0_05” for “Variable value”, as shown below. (If you use a Java SDK installation at another directory, you should update the variable value accordingly.)

Click on the OK button to compete the definition. Click on the OK button of the “Environment Variables” pane to shut it down. Click on the OK button of the “System Properties” window to shut it down.

  1. To test whether you have done correctly, click on “Start|Run…”, type “cmd” in the Open box, and click on the OK button to start a Command Prompt window. Type “javac” and the Enter key in the window. If you see usage information of “javac”, you have completed this step successfully. Otherwise you need to carefully redo this step.

2.2 Customizing properties of Windows Explorer

For software developers, Windows Explorer should show detailed view of files, and file extensions of known types should be displayed all the time. It will also be convenient if we can easily launch a new Command Prompt window from a Windows Explorer and change directory automatically to the directory that we choose in the Windows Explorer. This section introduces some useful customizations to your Windows Explorer for better supporting Java programming.

  1. Click on “Start|All Programs|Accessories|Windows Explorer” to launch an instance of Windows Explorer.
  2. Click on “View|Details” to turn on the detailed view. In the right pane, each file or directory will be represented by a line specifying its name, size, type, and date modified.
  3. Click on “Tools|Folder Options…” to launch the Folder Options pane. In the Folders Options pane, click on tab “View”. Make sure that you have checked on options “Display the full path in the address bar”, and unchecked options “Hide extensions for known file types” and “Remember each folder’s view settings”, as shown in the following screen capture:

Click on button “Apply to All Folders” so all Window Explorer instances will have the same folder options as this one.

  1. Click on tab “File Types”. In the “Registered file types” list, click on the line for “(NONE) Folder”, as shown below (if you are using Windows 2000, “(NONE)” is replaced with “(N/A)”):

2.3 EditPad installation and customization

Visit http://www.editpadpro.com/editpadlite.html to download and install the free Editpad Lite text editor.

Now we are going to customize EditPad so it is more convenient for program development. We will define a smaller tab size, and replace each tab character with spaces. The Java source code developed on such customized EditPad can present themselves well in other text editors.

Start EditPad. Click on “Options|Preferences…” Click on “File Types” in the new Preferences window. In the left upper scroll pane, scroll and choose “Java source code” (if you cannot find “Java source code”, you can click on the “New” button under the file type list to create a new file type for Java associated with file name pattern .java: you enter “Java source code” in the Description textbox, and enter “.java” and Enter in the Extensions textbox). In the lower right side of the window, set Tab size to 2, Block indent to 2, and check the checkbox for “Pressing Tab inserts spaces instead of tab characters”. Then click on the bottom Apply button.

2.4 Typical process for creating and running a Java program

  1. Use EditPad to create a Java source file named “Hello.java” in directory “C:\cs121”. If you don’t have directory “C:\cs121” yet, use a Windows Explorer to create it. Start EditPad. If EditPad is already open for editing some other files, click on “File|New” or the first New icon to start an empty Untitled file. Click on “File|Save As…” to launch the “Save File” window. Use your mouse to click in the top half of the “Save File” window to navigate in your file system so that the “Save in:” box shows the destination directory “cs121” under “C:\”. Type file name “Hello.java” in the “File name:” textbox. Then click on the Save button to save the new empty file into a new file “C:\cs121\Hello.java”. The following figure is the screen capture for the “Save File” window.

In EditPad , enter the following contents into the new file “Hello.java”:

Finally click on “File|Save” or the Save icon to save the new contents into file “Hello.java”.

  1. Open a Command Prompt window and change directory to “C:\cs121” (see the next section for some tutorial on using DOS commands). If you have followed my customization of the Windows Explorer , you could achieve this by right-clicking on folder “C:\cs121” in the left pane of a Window Explorer , and then click on the Command Prompt item of the popup menu.
  2. In the Command Prompt window, type javac -d C:\classes Hello.java to compile the source file “Hello.java” into a Java class file “Hello.class” in folder “C:\classes”. The command-line switch “-d” of “javac” command is used to specify the

public class Hello { public static void main(String[] args) { System.out.println("Hello, World!"); } }