CMPU 102 - Lab 1

For this lab we will write a class that represents a pocketful of change.  The goal is for you to get re-acquainted with Java and learn how to use the Eclipse Java development environment.

Part 0: Logging in

From the login screen, choose "Options->Session->OpenWindows Desktop".  Type your username and password to log in.  After logging in, type "passwd" and return in a terminal window.  Follow the prompts to choose a new password for your account.

Part 1: Download the lab files

Start eclipse by typing "eclipse &" in a terminal window and hitting return.  Wait for the main window to open.  If necessary, click on "Workbench".

Start a web browser by typing "firefox &" in a terminal window and hitting return.  Go to the class web page: http://www.cs.vassar.edu/~cs102/.  Go to the table in the Labs section.  Right click on the link to the file "lab1.zip" in this week's lab (Lab 1) and choose "Save Link Target As...".  Save the file in your home directory.

In Eclipse, choose "File->Import->Existing Projects into Workspace" from the menu.  Click on "Select archive file", and then the "Browse" button next to this option.  In the file chooser, select the file "lab1.zip" from your home directory.  Click "Finish".  You should now see a project called "lab1" in the Package Explorer.

Part 2: Implement Coins class

Implement the class called Coins, which is in the file called Coins.java.  This file can be found by expanding the "lab1" project, then the "src" folder, and then the "edu.vassar.cs.cs102" package.  Double click on the file to open it in an editor window.

The class should have instance variables for each coin type listed below:

    pennies, nickels, dimes, and quarters

The class should provide:

  1. a zero-parameter constructor that calls the 4-parameter constructor, setting all instance variables to 0.  Hint: the first line of the zero-parameter constructor should be
    this(args...);
    
    where args... is the list of arguments you want to pass to the 4-parameter constructor.

  2. a 4-parameter constructor that initilializes each data field.

  3. findCentsValue calculates and returns the total value of the coins in cents, as an integer.

  4. findDollars calculates and returns the value in dollars, as an integer. Any fractional part of a dollar should be discarded: for example, if the coins total $1.37, then this method should return the value 1.

  5. findChange calculates and returns the value of the leftover change (after the dollars are removed) as an integer. For example, if the coins total $1.37, then this method should return the value 37.

  6. toString to report the state of the Coins object.

  7. equals to compare two Coins. This method should return true only if the two objects are equal in total value.

Part 3: Testing

After you have finished the Coins class, you should run the main method in the class called CoinsTester.  The main method for this class takes a single command line parameter, which determines the upper bound of how many coins of each type to randomly create.  For example, if you pass the value "5" to CoinsTester's main method, it will randomly create from 0 to 4 of each kind of coin in the Coins objects it creates.

To execute the CoinsTester main method from within Eclipse:

  1. First, click on "CoinsTester.java" in the Package Explorer so it is highlighted.  Choose "Run->Run..." from the menu.

  2. Under "Configurations:", Click on "Java Application", and then click "New". You should see the name of the class ("CoinsTester") in the "Name:" field at the top of the dialog.

  3. Click on the tab labeled "Arguments".

  4. In the "Program arguments" box, enter the number "10".

  5. Finally, click the "Run" button at the bottom of the dialog.

You will see the output of the program in the Console window.  Try varying the value you pass to the main method in order to find the largest value you can pass in order to have two randomly-created Coins objects compare as equal.  To change the argument:

  1. Select "Run->Run..." from the menu, click "CoinsTester" under "Configurations:"

  2. Choose the "Arguments" tab

  3. Change the value you entered for "Program arguments"

  4. Then the click the "Run" button again

Note that you can re-run the last program you ran by clicking on the green toolbar button that looks like this:

Part 4: Submitting your project

From a terminal window, type the following commands:

cd
cd eclipse-workspace
submit102 lab1

Part 5: Log out

When you are done, close Eclipse, and then click on the "VCCS" menu in the lower left of the screen.  Choose "Logout..." and then click "Yes" when prompted.  Always remember to log out when you are done using the system, to ensure that no one else uses your account.