Chapter 2 Simple Java Applications - People
[Pages:36]Chapter 2
Simple Java Applications
2.1 An Application and its Architecture 2.2 How to Build and Execute an Application
2.2.1 Using an IDE 2.2.2 Using the JDK 2.3 How the Application Works 2.3.1 An Execution Trace of the Application 2.4 How One Object Constructs Another 2.5 Repairing Compiler Error Messages 2.6 Summary 2.7 Programming Exercises 2.8 Beyond the Basics
This chapter applies concepts from Chapter 1 to building simple programs in the Java language. Its objectives are to
? present the standard format of a Java program and illustrate the instances of class, method, and object, as they appear in Java.
? explain the steps one takes to type a program, check its spelling and grammar, and execute it
? show how an object can send messages to other objects and even create other objects as it executes.
2.1 An Application and its Architecture
If you were asked to built a robot, what would you do? First, you would draw a picture of the robot; next you would write the detailed instructions for its assembly. Finally, you would use the detailed instructions to build the physical robot. The final product, a robot "object," has buttons on its chest, that when pushed, cause the robot to talk, walk, sit, and so forth. The buttons trigger the robot's "methods"--the activities the robot can perform.
2.1. AN APPLICATION AND ITS ARCHITECTURE
21
Figure 2.1: architecture of initial application
Hello main
[Note: what we will write]
System.out println
[Note: Java's name for the command window]
As we learned in the previous chapter, program construction follows the same methodology: We draw an initial design, the class diagram, and for each component (class) in the diagram, we write detailed instructions in Java, which are saved in a file in secondary storage. When we start the program, the class is copied into primary storage and becomes an executing object.
The Java designers use the term application to describe a program that is started by a human user. After an application's object is constructed in primary storage, a message is automatically sent to the application's main method, causing its instructions to execute. Therefore, every Java application must possess a method named main.
Begin Footnote: Not surprisingly, the precise explanation of how an application starts execution is more complex than what is stated here, but we nonetheless stick with our slogan that "classes are copied into primary storage and become executing objects." End Footnote
To illustrate these ideas, we construct a small Java application that contains just the one method, main, which makes these two lines of text appear in the command window:
Hello to you! 49
To make the text appear, our application sends messages to a pre-existing object, named System.out, which is Java's name for the command window. The application we build is named Hello; it interacts with System.out in the pattern portrayed in the class diagram of Figure 1.
The class diagram shows that Hello has a main method; a message from the "outside world" to main starts the application. The other component, System.out, has its name underlined to indicate that it is a pre-existing object--a Java object that is already connected to the command window. The object has a method, println (read this as "printline"), which knows how to display a line of text in the command window. The Java designers have ensured that a System.out object is always ready and waiting to communicate with the applications you write.
22
The arrow in the diagram indicates that Hello sends messages to System.out. That is, it uses System.out by communicating with (or "connecting to") its println method.
Using terminology from Chapter 1, we say that Figure 1 presents an architecture where Hello is the controller (it controls what the program does) and System.out is the output view (it displays the "view" of the program to the human who uses it).
We write and place the instructions for class Hello in a file named Hello.java. Java instructions look like "technical English," and the contents of Hello.java will look something like this:
public class Hello { public static void main(String[] args)
{ ... to be supplied momentarily ...
} }
Java is a wordy programming language, and we must tolerate distracting words like public, static, void, which will be explained in due course. For now, keep in mind that we are writing a class that has the name Hello and contains a method named main. The set braces act as "punctuation," showing exactly where a method and a class begin and end. The ellipses indicate where the instructions will be that send messages to System.out.
For our example, main must contain instructions that display two full lines of text. The algorithm for this task goes:
1. Send a message to System.out to print "Hello to you!" on a line of its own in the command window.
2. Send a message to System.out to print the number 49 on a line of its own in the command window.
We must convert the above algorithm into Java instructions and insert them where the ellipses appeared above. Step 1 of the algorithm is written like this in Java:
System.out.println("Hello to you!");
This instruction sends to System.out the message to print-line (println) the text, "Hello to you!". In similar fashion, Step 2 is written
System.out.println(49);
The technical details behind these two instructions will be presented momentarily; for now, see Figure 2 for the completely assembled program.
Before we dissect Figure 2 word by word, we demonstrate first how one types the program into the computer, checks the program's spelling and grammar, and starts it.
2.2. HOW TO BUILD AND EXECUTE AN APPLICATION
23
Figure 2.2: sample application
/** Hello prints two lines in the command window */ public class Hello { public static void main(String[] args)
{ System.out.println("Hello to you!"); System.out.println(49);
} }
2.2 How to Build and Execute an Application
We take several steps to make the application in Figure 2 print its text in the command window:
1. class Hello must be typed and saved in the file, Hello.java.
2. The program's spelling and grammar must be checked by the Java compiler; that is, the program must be compiled.
3. The program must be started (executed).
To perform these steps, you use either your computer's (i) integrated development environment (IDE) for the Java language, or (ii) text editor and the Java Development Kit (JDK).
We briefly examine both options, and you should obtain help with selecting the one you will use. If you are not interested in this selection at this moment in time, you may skip either or both of the two subsections that follow.
2.2.1 Using an IDE
There are many IDEs available for Java, and we cannot consider them all. Fortunately, IDEs operate similarly, so we present a hypothetical example. Be certain to read the manual for your IDE before you attempt the following experiment.
When an IDE is started, it will present a window into which you can type a Java application (or "project," as the IDE might call it). Select the IDE's menu item or button named New Project to create a new project, and when the IDE asks, type the name of the class you wish to write. For the example in Figure 2, use the name,
24 Hello, for the project:
2.2. HOW TO BUILD AND EXECUTE AN APPLICATION
25
Next, type the class into the window and save it by using the IDE's Save button:
If done properly, class Hello will be saved in a file named Hello.java. Next, the program must be compiled. Compile by selecting the IDE's Compile or
26 Build button:
If there are any spelling or grammar errors, they will be listed in a small window of their own; otherwise, a message will announce that the compile has completed successfully. In the former case, you repair the errors and try again; in the latter case, you will see that the compiler has created the translated version of the application and placed it in the file, Hello.class.
Finally, start the program by selecting the button named Run or Launch (or Start, etc.) This starts the Java interpreter, which helps the processor read and execute
2.2. HOW TO BUILD AND EXECUTE AN APPLICATION
27
the byte-code instructions in Hello.class. In the case of the Hello application, its execution causes two full lines of text to appear in the command window. The IDE shows the command window when needed:
(Footnote: Unfortunately, your IDE might show and remove the command window before you can read the text that appeared! If the command window disappears too quickly, insert these lines into your Java program immediately after the last
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- a guide to programming in java mr barrett s class
- file new project new project categories java
- using netbeans to compile and run java programs
- java for beginners
- final project presentation edu
- mini project report northwestern university
- introduction to the java programming language
- a step by step guide to using jdbc with eclipse step 1
- chapter 2 simple java applications people
Related searches
- simple java code
- simple java hello world program
- simple java jdbc example
- simple java program using array
- simple java projects
- java applications with source code
- simple java program for beginners
- simple java programming code
- simple java hello world
- minecraft 1 12 2 download java edition
- simple java exercises
- simple java codes for beginners