CS 261 University of Puget Sound Spring, 2020
 
Computer Science II
Abstract Data Types and their Implementations, Some Basic Algorithms,
Object-oriented Problem Solving, and Efficiency
 

Warning: This course is under development. Although the basic structure of this course is largely established, nothing on this Web site should be considered official or even possibly correct.
DO NOT MAKE PLANS BASED ON THE CONTENTS OF THIS SITE UNTIL JANUARY, 2020.

Working with Java and Eclipse

Summary

This lab outlines the basic steps required to use Ellipse to create and run a simple Java program.

Introduction

Eclipse organizes software into four basic units:

See the reading for this lab for clarification of these organizational elements.

Eclipse Installation

We will be using the Eclipse programming environment throughout this course. Thus, if Eclipse is not installed on your machine, it will need to be installed.

  1. Eclipse Installation: The following instructions target a Mac OS X environment; similar instructions apply for Windows or Linux.

    1. Go to the Eclipse home page: "https://www.eclipse.org/

    2. Click the "Download" button to get to a Download page.

    3. Click on "Download 64 bit". This brings you to another download page.

    4. At Home/Downloads/Eclipse downloads, select a mirror site.

    5. After several such download menus, click on the option to "Open with DiskImageMounter (default)".

    6. When an Eclipse Installer window opens, click on the Eclipse Installer.app.

    7. The Installer will ask what product is desired. Click on "Eclipse IDE for Java Developers".

    8. Accept the license and default options.

    9. A Welcome page provides information on many topics.

    Note: Computing environments on individual machines may vary, so installation on your personal computer may require some adjustments to these instructions.

Setting a Few Defaults in Eclipse

The Java Style Guide for this Course specifies:

Eclipse can support these conventions by setting relevant preferences, as follows:

  1. After the installation of the Eclipse environment, Eclipse will likely be represented by an icon on your desktop. Click on the icon to launch Eclipse.

  2. The first time you run eclipse, the software will want to identify a base directory for all work with Java. Accept the default location, which is the directory eclipse-workspace within your home directory. Accepting this default seems appropriate for most users.

  3. Under the Eclipse menu, click:
    Eclipse > Preferences > Java > Code Style > Formatter

  4. Click the Edit button. On the resulting Profile page:

    1. At the top, choose a new Profile name. (I decided upon the name myDefaultProfile.)
    2. Click on the right triangle for Indentation to expand the indentation options.
      • Under Tab policy, select "Spaces only".
      • Under indentation size, select "3".
      • Under Tab size, select "3".
    3. Click on the right triangle for Line Wrapping
      • Under Maximum line width, type/select "80".
      • Under Default indentation for wrapped lines, select "3"
      • Under Default indentation for array initializers, select "3".
      • On the lower right under Preview line width, select "80".
      • On the lower right, click "OK".
  5. Back on the Formatter menu, click "Apply and close".

Getting Starting with the Person Class

The in-class demonstration developed a simple class Person with these elements:

The first part of this lab uses this class Person to provide a focus for the use of the Eclipse IDE.

To get started programming with Eclipse, follow these steps:

  1. Create a Java Project by going to FileNewJava Project. A form will ask you to name the project; two reasonable generic names are "examples" and "myExamples", but you are free to choose any name you wish. Note: By convention, module names begin with a lowercase letter.

    The form also allows various options for setting up a project. The following [default] settings are recommended.

  2. With this done, you will see examples or myExamples in the left window of Eclipse. Click on the triangle to see what has been concluded (largely the JRE System Library and a source directory (src), which contains the module-info.java file.)

    If for any reason you do not like the module name or other detail of the module, highlight the module name in the left navigation bar, go to the Edit menu, and use the "Delete" option.

  3. Create a package by going to FileNewPackage. As already noted, the in-class example uses package myDirectory, although any valid Java identifier would work. (In the Eclipse form, use the default for the Source folder.) When this step is completed, package myDirectory should appear under the source (src) heading within examples or myExamples

  4. Create a class by going to FileNewClass. Enter Person as the class name, accept the other defaults, and click "Finish" to establish this new class within Eclipse.

  5. With the Person created, implement the fields, constructors, and methods identified in the in-class demonstration for the Person class. (Be sure to use Eclipse capabilities (mostly from the File and Source menus) to do as much of the work as possible.)

  6. Write a separate testing class which exercises the various capabilities in the Person class, to verify that Person works properly.

Defining a Class Course

The next part of this lab provides additional practice with the basics of the Eclipse Environment, by creating a Course class within the examples or myExamples package.

Consider a Class Course which records basic information for a course (e.g., information that might be in a course directory). Such information might include the subject (e.g., CSCI or MATH), course number (e.g., 261), number of credits (e.g., 1.0 or 0.5), and course name (e.g., Computer Science II).

File Course.java provides a basic framework for this class (based on courses at Grinnell College. As this file suggests, class Course should contain these elements:

The goal of this part of the lab is to create this new Person class using as little effort as possible.

  1. Use Eclipse to create a new Person class within the examples or myExamples package.

    Be sure to run your program to test whether all is working properly!

Writing/Expanding a Rational.java Class

As another example, consider the abbreviated class Rational.java. In mathematics, a rational number is a fraction, where both numerator and denominator are integers, with the denominator non-zero.

  1. Review Rational.java.

    1. What options are available to create a new Rational object?
    2. To what extent does this class assure that the denominator of a fraction is non-zero?
    3. What arithmetic operation(s) is (are) implemented, and why is the implementation correct?
  2. Create a new number package within the examples or myExamples project, and establish a new Rational class within this numbers package.

    1. The class will be in the same examples project that was created in step 8.
    2. Create a new numbers package.
    3. Create a new Rational class, and copy the abbreviated Rational.java code.
    4. Compile and run Rational.java, and check that it works properly.
  3. Implement new methods for subtraction, multiplication, and division. (Consider an appropriate result, in the case that a divisor is zero during division.)

  4. Extend testing in the main method to check all arithmetic operations thoroughly.

  5. To illustrate how addition works, use the add to add 1/4 + 1/4 + 1/4 + 1/4. Of course, the result should be 1. What is actually computed? Is the result correct? Is the result what one might want? Explain.

Reducing Fractions

Although the Rational constructors and the addition method work, Step 18 shows that simple operations can yield large numerators and denominators quickly. One helpful approach can be to reduce fractions to lowest terms in the constructor.

  1. With the code given for the add method, why would reducing a fraction to lowest terms in a constructor reduce the result of addition to lowest terms as well?

A common approach to reduce fractions is to identify a greatest common divisor for both the numerator and denominator, and then remove that common factor. This approach is called the Euclidean Algorithm.

Computing the Greatest Common Divisor (Euclidean Algorithm):

[The following is an edited and abridged version of Section 4.1 from Problems for Computer Solutions Using FORTRAN by Henry M. Walker, Winthrop Publishers, 1980 and is used with permission of the copyright holder.]

Let N and M be two positive integers. The greatest common divisor of N and M, denoted gcd (M, N), is defined to be the positive integer D such that

  1. D divides N and M, and
  2. D is the largest integer dividing both N and M; i.e., every integer E that divides both N and M also divides D.

For example, 2 = gcd (6, 8); 4 = gcd (4, 12); 1 = gcd (8, 9); 6 = gcd (66, 24).

Algorithm: The algorithm proceeds by long division—keeping track of subsequent remainders:

This process continues until we find a remainder Ri+1 which is 0. Then Ri = gcd (M, N).

Notes: Proof that the Euclidean Algorithm works typically provides two nice examples of mathematical induction. See me, if you have questions or would like to talk about the proof.

Example: Find the Greatest Common Divisor of 66 and 24


Step 1: Divide 66 by 24: remainder is 18.
Step 2: Divide 24 by 18: remainder is 6
Step 3: Divide 18 by 6: remainder is 0, so we can stop.

Thus, 6 = gcd (66, 24).


  1. Within the Rational class, implement a private gcd method that has two integers as parameters and uses the Euclidean Algorithm to compute and return their greatest common divisor.

    Notes:

  2. Improve the Rational constructors, as needed, to guarantee the following conditions for every rational number:

    1. the denominator is always non-negative. (For now, ignore the case where the denominator is 0—we'll handle that situation shortly in another lab.)
    2. the fraction is always reduced to lowest terms.

created 17 January 2012
revised 20 January 2012
revised 1 September 2014
revised 30 August 2018
revised 15 September 2019
revised 13-18 January 2020
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.
ccbyncsa.png

Copyright © 2011-2019 by Henry M. Walker.
Selected materials copyright by Samuel A. Rebelsky, John David Stone, and Henry Walker and used by permission.
This page and other materials developed for this course are under development.
This and all laboratory exercises for this course are licensed under a Creative Commons Attribution-NonCommercial-Share Alike 4.0 International License.