Algorithms and OOD (CSC 207 2014F) : Labs

Reading Java


Summary: In this laboratory, you will read, attempt to understand, run, and occasionally correct some Java code.

Prerequisite Knowledge: Git. Using Eclipse. Java basics.

Preparation

Start Eclipse.

Fork the GitHub repository for this lab, available at https://github.com/Grinnell-CSC207/lab-code-reading.

Clone your fork to the local machine.

Import the clone into Eclipse. Note: This clone is already configured as an Eclipse project, so you should be able to select Import Existing Projects rather than Use the New Project Wizard.

Exercises

Exercise 1: A Quick Overview

Look at the Main class and predict what the program is supposed to do.

Confirm your hypothesis experimentally.

Exercise 2: Reformatting Code

Look at the MathUtils class. You may note that it's badly formatted. Find a quick way to reformat it according to the class code-formatting conventions.

Exercise 3: Squaring Doubles

Return to the Main class. Insert code instructions print the squares of the real numbers 0.1, 0.2, ...., 0.9. When typing, type slowly and take advantage of the popup menus that appear.

Exercise 4: Correcting Errors

There's a small error in square(double). Correct that error.

Exercise 5: Commit and Push

Commit your changes and push the result back to GitHub. Then revisit the GitHub page to see if they are incorporated.

Exercise 6: Introduce Errors

Introduce some syntax errors into your Java code and see how Eclipse reacts. In each case, you should see a red x at the left of the pane. Click on the x to see what options are available. Here are some suggestions:

  • Remove the int from the declaration of i in main.
  • Refer to i outside of the for loop.
  • Remove one or more right braces.
  • Replace one of the instances of java.io.PrintWriter with just PrintWriter.
  • Change the name of the class (but not the file) to Mine.
  • Call square on a String.
  • Remove the keyword return from the body of one of the versions of square.

For Those With Extra Time

Extra 1: Translate C

Pick a simple C program you've written previously and rewrite it in Java.