Lab exercise #4: Using Eclipse

Course links

External links

The objective

Our goal for today is to create a new package using Eclipse and to populate it with a new class.

Part 1: Creating a package

  1. Open a terminal window. At the prompt, type
    /opt/eclipse/eclipse &
    
    to launch Eclipse.
  2. Find the 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.
  3. Select the 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.
  4. In the 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.
  5. Click on the Finish button at the bottom of the New Java Package window. The window will disappear.

Part 2: Creating a class in the vend package

  1. To start editing a definition for a class that will belong to this new page, click to highlight the entry for the package in the Package 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.
  2. In the 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.
  3. Click on the 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.

Part 3: Writing the 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.

  1. For each of these three kinds of coin, add a private field of type int that will keep track of how many coins of that kind are in the coin box.
  2. Write a depositQuarter method that adds 1 to the number of quarters in the coin box.
  3. Similarly, write depositDime and depositNickel methods.
  4. Write a cashValue method that determines the total value of all the coins currently in the coin box.

Saving your work

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.

  1. Save your CoinBox.java file.
  2. Mail a copy of it to your lab partner by opening a terminal window and typing
    mail -s "CoinBox.java" partner@grinnell.edu < /home/spelvin/workspace/csc152/vend/CoinBox.java
    
    substituting your partner's name for partner and the pathname to the file for /home/spelvin/workspace/csc152/vend/CoinBox.java.

Running the class as a Java application

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.

  1. Create a 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.
  2. Run the program as a Java application and check that the output is correct.
  3. Revise the 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.