Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
1Announcements
• Please boot up your laptop and start Forte 
when you arrive every Friday
– From the taskbar, go to Start -> Programs -> Forte 
for Java CE -> Forte for Java CE
– It takes a while (about 2 minutes) for Forte to start 
up. Be patient.
– If you’re having trouble starting Forte, please raise 
your hand.
• Please also start Netscape and log into the 
1.00 course Web site every Friday:
– web.mit.edu/1.00/www/
– If you have trouble reaching the Web site, please 
raise your hand.  You’re relying on your laptop’s 
wireless network card for this.
• Optionally bring laptop to Mon, Wed lectures
1.00 Lecture 2
(Lab)
Introduction to Forte for Java
2What’s an IDE?
• An integrated development environment (IDE) is an 
environment in which the user performs the core 
development tasks:
– Naming and creating files to store a program
– Writing code (in Java or another language)
– Compiling code (check correctness, generate executable binary)
– Debugging and testing the code
– And many other tasks: version control, projects, code 
generation, etc.
• Forte for Java Community Edition is a fairly standard 
IDE that we will use in 1.00 for labs, tutorials and 
homeworks
• The alternative to IDEs is a ‘command line’ interface 
which is not visual and in which the tools for each task 
are generally not tightly integrated
Why Use an IDE?
• 1.00 students in past terms have not 
used good development practice 
with command line interfaces
– Neither do many industry programmers
– While you can do the same things with a 
command line interface as an IDE, in 
fact people don’t
3Why Use an IDE?
• What does an IDE facilitate?
– Visual representation of program components
– Ability to browse existing components easily, so 
you can find ones to reuse
– Quick access to help, documentation to use 
existing libraries and tools vs writing your own
– Better feedback and error messages when there 
are errors in your program
– A debugger, which is not primarily used to 
debug, but is used to read and verify code
– Communication between programmers in a 
team, who share a common view of the program
• These are process issues but they are very 
real problems in research and industry
Forte Components
• Please look at your laptop’s Forte window.
• The user interface of the Forte for Java IDE 
consists of the following components: 
Program writingSource editor
Web browser Form editor 
Execution window, etc.Property sheets 
Component inspector Explorer 
Project settings, compile 
and debug options
Main window
• The most important components for the moment 
are the Main Window, Explorer, and the Source 
Editor. 
4Main Window
• The Main window appears first when you launch the 
Forte for Java IDE. It remains open while the IDE is 
running.
• The Main window provides toolbars for the menu 
options. The toolbar provides a fast and easy method 
to execute commands.
• The lower-left corner of the Main window has five 
workspace tabs. Workspaces help you to organize 
windows into logical sets. 
Explorer
• Explorer provides an integrated 
view of all the objects and files in 
the Forte for Java IDE. 
• Explorer is the starting point for 
many programming functions, 
such as creating and executing an 
application. It helps you to work 
with objects, organize your work, 
modify object properties, and 
connect various data sources. 
• Explorer has four tabs: 
Filesystems, Project, Javadoc, and 
Runtime 
5Explorer, p.2
• The Filesystems tab displays the file systems 
(folders/directories and their files) which are 
currently mounted in the IDE. 
– There is a File->Mount command to mount new directories. 
• The Project tab displays the directories and files from 
the current project.
– Projects are used to manage related Java components that 
together make up a program.
• The Javadoc tab contains the directories of API 
documentation in the Javadoc format.
– Javadoc is the online Java documentation.
• The Runtime tab contains a list of currently running 
processes and debugger information
– We won’t use this much in 1.00.
File System
• A filesystem is a file hierarchy such as one stored on 
a local or a network drive, or archive file such as JAR, 
ZIP or WAR.
• To develop applications in the IDE you need to 
determine the filesystems you want to work with and 
mount them in the IDE. Mounted filesystems can be 
viewed on the FileSystems tab in the Explorer.
• Mounting a filesystem makes it accessible to the IDE.
– Advanced: By default, when you mount a filesystem it is 
added to your classpath for compiling running and debugging 
your application in the IDE.
6Source Editor
• You view the Java code and make modifications to the 
code in Source Editor. It is easy to view and debug 
code in this window because Source Editor color-
codes the code according to syntax. For example, it 
highlights all default keywords in blue.
Editor Workspace
• The Forte for Java IDE displays the Editing workspace 
when the IDE starts. The Editing workspace includes 
the following: 
– Explorer 
– Properties Window 
– Source Editor (appears only if a file is open) 
7Using the Editor Workspace
Creating a file:
• Right click on the root 
directory of the 
FileSystems in the 
Explorer view. 
• Select New->Classes-> 
Main (see figure). A 
Template Wizard will open.
• Enter “Welcome” in the 
Textbox and click on 
Finish. This will be the 
filename and the class 
name of the file you are 
creating.
Using the Editor Workspace, p.2
• Once the file is created, the source editor will open.
• We want to write the “Welcome” application so that it 
prints a message on your screen.
• You will need to modify the main method so that it 
prints “Welcome to 1.00” and other stuff.  Your main 
method should look exactly like the following:
public static void main(String[] args) {
System.out.println("Welcome to 1.00");
int students= 240;
int grads= 35;
double pctGrads= (double) grads/students;
System.out.println("Percent grads: " + pctGrads);
System.exit(0);
}
8Editor Features
• Dynamic code completion
– Type first few characters and hit ctrl-spacebar 
to see completions
• Bracket matching
– Place cursor before/after { } ( ) to see matching 
{ } ( )
• Automatic updating of explorer, etc.
• Word matching, keyboard macros, etc.
– Check Forte help or ask your TA
Compiling your Program
• Right-click on the “Welcome”
file in the Explorer view.
• Select “Compile” to compile 
the file. An Output Window will 
appear. 
• If there were no errors in your 
program, you should get a 
“Finished Welcome” message. 
•If you don’t get this message, this means there was an error in 
your program. (Note: Forte offers useful tools to debug errors. 
More on these tools in Tutorial).
•Make sure that you have entered the code correctly. Recompile 
your program. If you’re still getting an error, please raise your 
hand.
9Running Workspace
• You use the Running workspace to run an application. The 
workspace includes the following: 
– Execution window
– Output Window
• The Forte for Java IDE 
automatically switches to the 
Running workspace when you 
execute a program. If there are 
no execution errors, the 
application launches. 
• If there are execution errors, 
the IDE displays them in Output 
Window. 
Running Workspace
• Go back to the Editor 
workspace by selecting the 
“Editor” tab in the main menu.
• Right-click on the Welcome file 
in the Explorer view and Select 
“Execute”.
• If you program has previously compiled successfully, the IDE 
will switch to the Running Workspace and will print out 
“Welcome to 1.00” in the output Window
10
Debugging Workspace
• You use the Debugging workspace to debug an 
application. The workspace includes the following: 
– Debugger Window 
– Output Window 
– Source Editor 
Debugging Workspace
• Go back to the Editor workspace by selecting the “Editor” tab 
in the main menu.
• Select the “Welcome” file in the Explorer view and Select 
Debug -> Step Into.
• The IDE will switch to the Debugging Workspace. The line of 
code that will be executed next is highlighted in blue. For now,
the first line of code should the one highlighted in blue. 
• To execute the code line by line, keep on selecting Debug -> 
Step Into (or use the shortcut key “F7”). In case you get a 
dialog box asking you to “select an action upon Step Into a 
method without a source”, select “Step Out”. 
• The Debugger offers useful tools to watch the value of 
variables and to add breakpoints. More on these tools in 
Tutorial.
11
Browsing Workspace
• The Browsing workspace helps you to view the packages, 
objects, and members in an application. The workspace includes 
the following:
– Object Browser
– Properties Window
Projects in Forte
• The Forte for Java IDE helps you to organize and 
manage files. It uses the concept of a project.
• A project contains all the files that you need to 
execute an application.
• You can easily manage all the files of an application or 
applet because they are in a project. 
• For example, when a project is compiled, all the Java 
language files are included (more on Projects in the 
Tutorial). 
12
GUI Editing Workspace
• The GUI Editing workspace is the main area where you 
develop a graphical user interface. This workspace 
includes the following: 
– Explorer 
– Source Editor 
– Component Inspector 
– Form editor 
• We won’t cover this in
1.00, but you can use
it in PS6-10
IDE Summary
• Forte IDE covers core software development
– Other tools are also used:
• Unified Modeling Language (UML) for requirements, 
overall structure of applications
• Data modeling for databases
• Automated test tools, test drivers
• Etc.
• References
– Sun Web Learning Center (https://tmn.sun.com)
– Forte For Java Community Edition, Release 3.0, 
HELP.
13
Exercise 1
• Using Forte, create a file Lecture2
• Write a main() in class Lecture2 to:
– Declare doubles x and y defining the length 
and width of a rectangle
• Initialize them in your program to positive values
– Compute the perimeter and area of the 
rectangle
• Declare two more doubles to hold these results
– Print out the perimeter and area
• Compile your program
• Review your program
– Step through your program with the debugger
Exercise 2 (Optional)
• Using Forte, create a file Quadratic
• Write a main() in class Quadratic to:
– Declare and initialize a double x 
– Compute the value of the quadratic: 3x2 – x - 2
• Declare a double to hold the result.
– Print out the value of the quadratic. The output 
should be something like:
• At x = 4.0 the value of the quadratic is 42.0
• Compile and run your program
14
Exercise 2 (Advanced)
• Preview on “Data Types”:
– Run the program with several values for x. Use 
values with decimal points, large values, small 
values, negative values, and zero. 
– Advanced: Solve the equation with paper and 
pencil (use the quadratic formula.) The quadratic 
should evaluate to zero at x = 1.0 and at x = -2/3. 
Try these values for x. Are the results exactly 
correct? (More on this topic in the next lecture).