Assignment 5 — Objects you make

Builders that change in place, four predictions checked before they run, and a die you can test because its seed is fixed.

Due Oct 6, 2026

Two parts, both due Tuesday, October 6. Part A needs Wednesday’s class; Part B needs Monday’s.

AI: the course AI policy applies in full. No AI-generated code, and no asking a chatbot to solve these problems before you have.

Part A — builders

In Builders.java, Wednesday’s file, next to Check.java. Every function gets a // line above it saying what it gives back or does, and at least two Check.is lines in main, worked out by hand before the body is written. A check on a builder goes through toString().

  1. mirror, frame and rotate — we started these in class on Wednesday.

    String mirror(String s)
    String frame(String s)
    String rotate(String s, int k)

    mirror("abc") is abccba; frame("abc") is [abc]; rotate("abcde", 2) is cdeab, the first two characters moved to the end.

  2. flip and twice — in place, and nothing comes back.

    void flip(StringBuilder b)
    void twice(StringBuilder b)

    flip turns the builder around; twice doubles it, so ab becomes abab. A check on a void function looks at the builder after the call:

    StringBuilder x = new StringBuilder("abc");
    flip(x);
    Check.is(x.toString(), "cba");
  3. swapEnds — the first and last characters exchanged.

    String swapEnds(String s)

    swapEnds("quokka") is auokkq; swapEnds("ab") is ba.

  4. Four predictions. Put the four fragments below into main, each with its Check.is. Write your prediction in place of every ??? before you run, then run. Where a check failed, correct it, and keep your prediction in a // comment at the end of that line with a few words on why it was wrong. A check that passed needs no comment.

    StringBuilder a = new StringBuilder("ab");
    StringBuilder b = a;
    b.append("c");
    Check.is(a.toString(), ???);
    String s = "ab";
    String t = s;
    t = t + "c";
    Check.is(s, ???);
    StringBuilder c = new StringBuilder("ab");
    StringBuilder d = new StringBuilder("ab");
    Check.is(c == d, ???);
    Check.is(c.toString().equals(d.toString()), ???);
    StringBuilder e = new StringBuilder("ab");
    flip(e);
    flip(e);
    Check.is(e.toString(), ???);

Done when Check.done() prints Looks shipshape with every function checked, and every prediction that missed has its comment.

Part B — a die you can test

Dice.java, a new file next to Check.java. The reading’s die is ThreadLocalRandom.current().nextInt(1, 7), and no check can be written for it: the next roll is anyone’s guess. A Random made with a seed rolls the same sequence every run — the reading says so in 3.3 — and that is what makes it checkable. Random lives in java.util (Coles 3.2.3), so the file starts with

import java.util.Random;
  1. roll — one roll of a six-sided die, from the generator it is given.

    int roll(Random r)

    r.nextInt(1, 7) is the roll. The checks use a seeded Random:

    Check.is(roll(new Random(42)), 3);

    Then a check with a seed of your own: print roll(new Random(7)) once, write down what it printed, and that is your expected value. A seeded die is a die whose answer you can look up.

  2. rollTwo — two rolls, added: the reading’s DiceRoller, as a function.

    int rollTwo(Random r)

    Two calls to roll on the same r, not one call used twice. rollTwo(new Random(42)) is 7, and the second check is a seed of your own again.

  3. report — the two rolls and their sum, as one line of text.

    String report(Random r)

    report(new Random(42)) is 3 + 4 = 7. + joins numbers to text, and Quiz 3’s first question says what happens when the numbers come first.

  4. The real thing. After Check.done(), print report of the reading’s generator, ThreadLocalRandom.current() — it is a Random too, so report takes it as it is — with import java.util.concurrent.ThreadLocalRandom; at the top. Run the program three times.

Done when Check.done() prints Looks shipshape, and three runs print three different last lines — or two, since a die repeats sometimes.

Hand in

Upload Builders.java and Dice.java on Brightspace, under Assignments → A5. The files themselves, not a paste, and not Check.java: your files are run against the course’s own copy of it.

Where they are: in IDEA, IdeaProjects inside your user folder, then cmsc-120-stripe/src/. In the lab that is the machine you sat at, not any other. On OneCompiler, copy each file’s text into a file of the same name on your own computer, and upload those.

If you run out of time

Part A’s three Wednesday functions and the four predictions first: they are Wednesday’s work finished, and the predictions want thinking time and go badly in a hurry. Then roll with its two checks. rollTwo and report can be the last evening’s work.

Grading

Full credit for two files that run and do everything above. These cost points too: a file under any name but Builders.java or Dice.java, a file that does not run as you hand it in, and a Check.is line that still fails. Run both once more after your last edit.

Most of the weight is on three things:

  • The checks. Every function has its // line and at least two Check.is lines, with the answers worked out rather than read off the body; every prediction that missed has its comment.
  • void means void. flip and twice change the builder they are given and give nothing back. A flip that returns a builder is a different function, even when its check passes.
  • roll rolls the Random it is given, whichever one that is. That is what lets one function serve both a seed and the real thing.

Presentation

Two of you will be asked on Wednesday, October 7 to put a file on the projector and walk the class through it. You will not be told in advance.

One of the two will be asked what the seed is for, and what would be lost without it.

What else to do (optional part)

Ungraded. No points, and skipping them costs nothing.

  • isPalindrome, swapEnds written the other way, and dash, from Wednesday’s page.
  • String reportThree(Random r) — three dice, and the sum.
  • void frameInPlace(StringBuilder b) — frame, but on the builder it is given. Then say, in a // line, which of the two frames you would rather call, and why.