teaching machines

Half-homework 2 – Methods – due October 7

August 5, 2019 by . Filed under cs1, fall 2019, specifications.

Your objective in this homework is to make code self-contained and reusable using methods. You will do this in the context of solving several disconnected problems that have no overarching story. Sorry.

Breaking code up into methods has several benefits: it enables large problems to be decomposed into smaller, mind-sized bytes; methods with a distinct purpose are easier to test than gangly spans of code; and because of the scope boundaries of methods, data becomes more insulated and less likely to get accidentally overwritten or inappropriately referenced. Additionally, methods jive with a basic human desire to make things that have lasting value. Like a family recipe, methods stand the test of time and get passed around.

Warning

When one is first learning about methods, there’s a great temptation to acquire the parameters from the user and print out the method’s return value inside the method, like this:

public void triple(int n) {
  Scanner in = new Scanner(System.in);
  System.out.println("What is n? ");
  n = in.nextInt();

  System.out.println(n + n + n);
}

Don’t do this. Methods generally should not perform input and output. Where the input comes from and where the output goes to should be decided by the caller of the method—not the method itself. Our method triple is most reusable and testable when we write it like this:

public int triple(int n) {
  return n + n + n;
}

Written properly, we can feed it data from many different sources:

triple(scanner.nextInt());
triple(generator.nextInt());
triple(17);

Additionally, we can embed the return value in many different contexts:

int thrice = triple(17);
System.out.println(triple(2));
int b = a + triple(5);

Requirements

Complete the five classes described below. Place all classes in package hw2. Make all methods static.

Main

Write class Main with a main method, which you are encouraged to use to test your code. Nothing in particular is required of it, but it must exist.

StringUtilities

Write class StringUtilities with the following methods:

MathUtilities

Write class MathUtilities with the following methods:

HyperUtilities

Write class HyperUtilities to help calculate various properties of n-dimensional boxes. A box in 1D is a line segment, a box in 2D is a square, a box in 3D is a cube, a box in 4D is a hypercube, and so on. This class has the following methods:

FileUtilities

Write class FileUtilities with the following methods:

Submission

To check your work and submit it for grading:

  1. Run the SpecChecker by selecting hw2 SpecChecker from the run configurations dropdown in IntelliJ IDEA and clicking the run button.
  2. Fix problems until all tests pass.
  3. Commit and push your work to your repository.
  4. Verify on Gitlab that your submission uploaded successfully by adding the comment test hw2 to any commit. You will receive an email of the SpecChecker results.

A passing SpecChecker does not guarantee you credit. Your grade is conditioned on a few things: