| 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.
This lab outlines the basic steps required to use Ellipse to create and run a simple Java program.
Eclipse organizes software into four basic units:
See the reading for this lab for clarification of these organizational elements.
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.
Eclipse Installation: The following instructions target a Mac OS X environment; similar instructions apply for Windows or Linux.
Go to the Eclipse home page: "https://www.eclipse.org/
Click the "Download" button to get to a Download page.
Click on "Download 64 bit". This brings you to another download page.
At Home/Downloads/Eclipse downloads, select a mirror site.
After several such download menus, click on the option to "Open with DiskImageMounter (default)".
When an Eclipse Installer window opens, click on the Eclipse Installer.app.
The Installer will ask what product is desired. Click on "Eclipse IDE for Java Developers".
Accept the license and default options.
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.
The Java Style Guide for this Course specifies:
Line length: Ensure that no lines of code are longer than the maximum number of characters; in the case of CS 261, this limit is 80 characters due to Java's inherent verbosity. (Note: Oracle suggests 80 characters; Google's limit is 100 characters. Due to past issues with printing, 80 characters seems appropriate here.)
Indentation using spaces: Do not indent with
tab characters (\t). Any block of code enclosed in
curly braces should be indented one level deeper than the
surrounding code. Choose a reasonable and consistent convention
for indentation; generally 2 to 4 spaces is acceptable. We
recommend 3 spaces for indentation. Make sure set the "Tab
policy" for all projects
to "Spaces
only".
Eclipse can support these conventions by setting relevant preferences, as follows:
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.
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.
Once you have started Eclipse, note that a "Welcome" panel appears on the right hand side of the environment. Working through this tutorial will show you many of the basic elements of Eclipse, largely paralleling what follows.
Under the Eclipse menu, click:
Eclipse > Preferences > Java > Code Style >
Formatter
Click the Edit button. On the resulting Profile page:
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:
Create a Java Project by going to File ⇒ New ⇒ Java 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.
Click the box for "use default location"
Click the radio button for "Use an execution environment JRE." JavaSE-9 is fine, but any version after 9 will be acceptable for this course. (Do NOT use JavaSE-8 or earlier versions!!)
Under Project layout, click the radio button for "Create separate folders for sources and class files".
With the above options identified and the name of the project entered, click "Finish" to create the new project.
As you click "Finish", Eclipse will ask you if you wish to "Create a new module-info.java file." The name of your module likely will be pre-filled. After checking the module name, check the box "Generate comments" and click "Create".
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.
Create a package by going to File ⇒ New ⇒ Package. 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
Create a class by going to File ⇒ New ⇒ Class. Enter Person as the class name, accept the other defaults, and click "Finish" to establish this new class within Eclipse.
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.)
Write a separate testing class which exercises the various capabilities in the Person class, to verify that Person works properly.
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.
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!
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.
Review Rational.java.
Create a new number package within the examples or myExamples project, and establish a new Rational class within this numbers package.
Implement new methods for subtraction, multiplication, and division. (Consider an appropriate result, in the case that a divisor is zero during division.)
Extend testing in the main method to check all arithmetic operations thoroughly.
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.
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.
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.
[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
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).
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:
Improve the Rational constructors, as needed, to guarantee the following conditions for every rational number:
|
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 |
|
| For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |
|
Copyright © 2011-2019
by Henry M. Walker.
|