• Java 2 Platform Standard Edition 5.0 API specification
• OpenJDK source code repository for library classes
• Source code from Data structures and problem-solving using Java, third edition
Our goal for today is to create a new package using Eclipse and to populate it with a new class.
/opt/eclipse/eclipse &to launch Eclipse.
csc152 project in the Package Explorer
sidebar. If the small triangle to the left of the word csc152 is
pointing to the right, click on it to reveal the contents of the project.New option from
the File menu, and the Package option from the secondary menu
that appears. Eclipse displays a new window titled New Java
Package.New Java Package window, fill in the text field
labelled Name: with the name of the package you want to create.
Let's call it vend.Finish button at the bottom of the New Java
Package window. The window will disappear.vend packagePackage
Explorer sidebar, then select the New option from the File
menu, and the Class option from the secondary menu that appears.
Eclipse displays a new window titled New Java Class.New Java Class window, fill in the text field
labelled Name: with the name of the class you want to create.
Let's call it CoinBox.Finish button at the bottom of the New Java
Package window. The window will disappear, and Eclipse will automatically
generate some basic "scaffolding" code for the CoinBox.java file and
load it into the editing subwindow in the middle of the main Eclipse
window. Note, in particular, that the package vend; declaration is
included automatically.CoinBox class
The Eclipse editor is, by default, configured to be extremely supportive,
almost to the point of being meddlesome. As you use it, you'll quickly
notice that it helps out in lots of little ways; for instance, when you
type a left parenthesis or a left brace, the editor inserts both
parentheses or both braces, appropriately positioned. It also helps
out in global ways; for instance, as you add each field and each method,
its name will appear in the right sidebar, the one named Outline,
which gives an overview of your progress.
The CoinBox class will model the coin box inside a vending machine.
The coin box contains some number of quarters, some number of dimes, and
some number of nickels. Use the Eclipse editor to add some fields and
methods to the basic framework that it constructed for you.
int that will keep track of how many coins of that kind are in the
coin box.depositQuarter method that adds 1 to the number of
quarters in the coin box.depositDime and depositNickel
methods.cashValue method that determines the total value of
all the coins currently in the coin box.
At any point, you can save the current version of the file by clicking on
the diskette icon (second from the left in the toolbar) or by selecting
the Save option from the file menu.
The CoinBox.java file is saved within the folder that you named as your
Eclipse workspace when setting up Eclipse. If that folder is called, say,
/home/spelvin/workspace, then the full pathname to the file will be
/home/spelvin/workspace/csc152/vend/CoinBox.java.
CoinBox.java file.mail -s "CoinBox.java" partner@grinnell.edu < /home/spelvin/workspace/csc152/vend/CoinBox.javasubstituting your partner's name for
partner and the pathname to the
file for /home/spelvin/workspace/csc152/vend/CoinBox.java.
If your program is now syntactically correct, and has a main method,
and you have saved it, you can run it as a Java application. From the
Run menu, select the Run As option, and select Java
Application from the secondary menu that appears.
If you have forgotten to save your work, Eclipse will remind you to do so
by popping up a "Save Resources" window, listing the file or files that you
need to save. Click on the OK button at the bottom of the window.
There is also a "run" icon on the toolbar just below the menu bar, at the top of the Eclipse window. It is a white triangle, pointing to the right, in a green circle. Once you have established that you want to run your class as a Java application, clicking on that button has the same effect as the menu selection.
If Eclipse reports that no console is available, it means that your program wants to do interactive input or output and that Eclipse failed to anticipate this and made no provision for it. Click the "run" icon to repeat the process of running the program.
If Eclipse isn't sure what it is that you're trying to execute, it will pop
up a "Run" window that invites you to "create, manage, and run
configurations." A "configuration" is an Eclipse launching mechanism for
starting up a program in precisely the way you want. A configuration
usually has the same name as the class containing the main method
that you're trying to invoke. The "Run" window provides a place to put the
name, the project (csc152) to which the class belongs, and the fully
qualified class name (in this case, [vend.CoinBox]). If the Run
window doesn't seem to supply an appropirate configuration, you can create
one by clicking on the New button in the lower left-hand corner,
which will give you the opportunity to create one.
In the Run window, clicking on the tab labelled Arguments
brings up an interface in which you can supply command-line arguments (here
called "Program arguments") that will be assembled into an array and bound
to the parameter of your main method.
main method for the CoinBox class that
constructs an empty CoinBox object, deposits a few coins in it, and
displays the amount of money that has accumulated in it.main method so that it accepts any number of
command-line arguments, each of which must be one of the three strings
Q, D, or N, and deposits the corresponding coins in a
second constructed CoinBox object, then reports the accumulated
total.