Algorithms and OOD (CSC 207 2013F) : Labs

Reading Java


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

Prerequisite Knowledge: 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 can 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 Java formatting conventions.

Exercise 3: Squaring Doubles

Return to the Main class. Insert code to 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 and push your changes.

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 attempt to rewrite it in Java.

Copyright (c) 2013 Samuel A. Rebelsky.

Creative Commons License

This work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.