Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
ACM Java Tutorial
DrJava
2. Compile
1. Write and save program (File->save)
3. Run
ACM Java Libraries
• Book that uses ACM Java libraries:
Java Compilation Process
Anatomy of a Java Class
Comments
• English-language comment
• Ignored by the compiler
• Two types:
– Enclosed between /* and */. Can expand several lines.
– Starts with // . Only single line.
Imports
• Indicate that the program uses two additional library packages
• A library package:
– A collection of tools written by other programmers that perform specific 
operations
• HelloProgram uses a graphics library and a program library, each of which 
come from a collection of packages produced by the Association for 
Computing Machinery (ACM).
• ACM Java Libraries:
– http://cs.stanford.edu/people/eroberts/jtf/javadoc/student/index.html
• The asterisk at the end of the package name is used to 
indicate that all components from the relevant package 
should be imported.
• Every program in the labs will import at least the 
acm.program package
• Any programs that use graphics will import 
acm.graphics
• i.e. most of your programs will also need to include 
these lines immediately after the program comment. 
• Some programs will use additional packages as well 
and must contain an import line for each one
The Main Class
• The last section of the HelloProgram.java file 
is the HelloProgram class
• Class:
– a template for the creation of individual objects
• Class Definition:
Class Header line
HelloProgram is a 
– Class name: HelloProgram
– extends keyword:
• HelloProgram is a GraphicsProgram and can therefore 
do any of the things that GraphicsProgram can do
– Followed by curly braces
Subclass of GraphicsProgram
• Body of the class definition:
• This is an example of a Java method
• Method name: run
Method Header line
statement
• Method:
– a sequence of program steps that have been collected together 
and given a name
– Method body: steps that the method performs (statements) 
listed between the curly braces and are called.
• Statements:
– End with a semicolon
run() method in ACM Java
• The method run plays a special role in programs that 
use the acm.program package.
• Whenever you run a Java program, the computer 
executes the statements enclosed in the body of the 
run method for the main class
• In HelloProgram, the body of run consists of the 
single statement
• This statement uses two facilities from the library packages. 
The first is the GLabel class, which comes from acm.graphics. 
• The part of the line that reads
creates a new GLabel object containing the text "hello, 
world".
• The rest of the line is:
which takes the new GLabel and adds it to the graphics 
program at a position whose x and y coordinates are 100 and 
75.
Output
Let us try the program in DrJava!
References
• The Art and Science of Java, Eric S. Roberts, 
Stanford University