Assignment 8 — The flags again, in Java

A1’s flags with Img: stripes that scale from one number, a record and an enum that say which flag, and the reading’s own colors.

Due Oct 27, 2026

One file, Flags.java, in the graphics project from class on Wednesday, cmsc-120-graphics, in src/ beside Img.java and Check.java. Part A needs Wednesday’s class, Part B the records of week 8, and Part C this week’s reading, [CU] 3.7 — Graphics.

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

No project? In IDEA: the menu button (☰, top left) → File → New → Project from Version Control… → paste https://github.com/cu-cs-labs/cmsc-120-graphics → Clone, and say yes when it asks to load the Maven project. Its README says what each file is.

Every function

A // line above it with a purpose statement, and at least two Check.is lines in main, worked out before the body is written. A picture is checked by its size, Img.width and Img.height, at two different units:

Check.is(Img.width(vertical(10, "green", "white", "orange")), 30);

Every size in a flag comes from unit, as in A1: change the unit, and the whole flag grows, with nothing left behind at the old size. Check.done() comes after the checks, and then main shows every flag, so that you see each one too:

Check.done();
Img.show(japan(30));

The program ends when the last window is closed.

Part A — stripes and a disc

  1. vertical — three stripes side by side, each unit wide and 2 * unit high, the first on the left. France is vertical(30, "blue", "white", "red"), and Ireland vertical(30, "green", "white", "orange").

    Img vertical(int unit, String first, String second, String third)
  2. horizontal — three stripes one above another, each 5 * unit wide and unit high, the first on top. Germany is horizontal(18, "black", "red", "gold").

    Img horizontal(int unit, String first, String second, String third)
  3. disc and japan — Japan’s flag is white, 3 * unit wide and 2 * unit high, with a red disc in the middle whose diameter is three fifths of the height. disc is the red disc alone; japan puts it on the white, with Img.overlay.

    Img disc(int unit)
    Img japan(int unit)

    Work out the disc’s radius in unit on paper first: half of three fifths of 2 * unit. Then put a check on Img.width(disc(30)) in main, and one at a unit that is not a multiple of 5, with the number worked out by hand. 3 / 5 is 0 in whole numbers; the order you multiply and divide in decides what comes out. japan keeps the white flag’s size whatever the disc does, so its own checks are on that.

Part B — which flag, as a record

A tricolor is three colors and which way its stripes run. At the top of the file, under the import line if you have one:

enum Stripes { VERTICAL, HORIZONTAL }

record Tricolor(Stripes stripes, String first, String second, String third) {}

In main: Tricolor france = new Tricolor(Stripes.VERTICAL, "blue", "white", "red"); and Germany the same way.

  1. draw — the flag a Tricolor describes, at that unit: a switch on its stripes, with a case for each and no default, handing its colors to vertical or horizontal.

    Img draw(Tricolor t, int unit)

    Img.width(draw(france, 10)) is 30; Img.width(draw(germany, 10)) is 50.

  2. turned — the same three colors in the same order, with the stripes running the other way.

    Tricolor turned(Tricolor t)

    Check.is takes a record, and compares it part by part:

    Check.is(turned(france), new Tricolor(Stripes.HORIZONTAL, "blue", "white", "red"));

    A second check: what does turning a flag twice give back?

Part C — the reading’s colors

The reading’s RandomTricolorFlag picks its two outer colors at random and makes the middle one halfway between them, with interpolate. Here the two are given. Every Img shape takes the reading’s Color as well as a color’s name, and the file needs one line at its very top for it:

import javafx.scene.paint.Color;
  1. blend — three vertical stripes, sized as in vertical, the middle one halfway between the other two: first.interpolate(third, 0.5), as in the reading.

    Img blend(Color first, Color third, int unit)

    blend(Color.RED, Color.BLUE, 30) has a purple stripe in the middle.

Done when Check.done() prints Looks shipshape with every function checked at two units, including the disc at a unit that is not a multiple of 5, and main shows every flag.

Hand in

Upload Flags.java on Brightspace, under Assignments → A8. The file itself, not a paste, and not Img.java or Check.java.

Where it is: in IDEA, IdeaProjects inside your user folder, then cmsc-120-graphics/src/. In the lab that is the machine you sat at, not any other.

Grading

Full credit for a file that runs and does 1 through 6. These cost points too: a file under any name but Flags.java, a file that does not run as you hand it in, and a Check.is line that still fails. Run it once more after your last edit.

Updated October 25: the layout. One statement to a line, and a blank line between functions; IDEA’s Code → Reformat Code lays a file out that way in one keystroke. A file crammed onto a few long lines cannot be read, so it cannot be marked: it gets 0.

Most of the weight is on three things:

  • Every size from unit: no bare number for a width or a height inside a flag.
  • The disc’s radius, checked at a unit that is not a multiple of 5.
  • A switch with a case for each kind of stripes, and no default, in draw and in turned.

Presentation

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

What else to do (optional part)

Ungraded. No points, and skipping it costs nothing. One problem bigger than anything above, in Flags.java, each function with its // line and checks.

The Nordic crosses

Sweden’s flag is blue with a yellow cross whose upright is left of the middle. In one unit it is 16 wide and 10 high: across, 5 of blue, 2 of the cross, 9 of blue; down, 4, 2 and 4. Img has no way to put a shape at a place, so the flag is built from bands, the way Pyret built it.

  1. row(int unit, int height, String field, String cross) — one band across the flag, height high: the field 5 units wide, the cross 2, the field 9. At unit 10 and height 40 it is 160 by 40.
  2. nordic(int unit, String field, String cross) — a band, the bar of the cross across the whole flag, 16 * unit wide and 2 * unit high, and the same band again: Img.above, twice. At unit 10 it is 160 by 100.
  3. Three flags from one function. Sweden is nordic(u, "blue", "gold"); with the same proportions, Denmark is red and white, and Finland white and blue. The real Danish and Finnish crosses sit a little differently; one function for all three is the point.
  4. Norway, the hard one: a blue cross with a white border, on red. In one unit it is 22 wide and 16 high: across, 6 of red, 1 of white, 2 of blue, 1 of white, 12 of red; down, the same 6, 1, 2, 1 and then 6. It has five bands. The two thin white bands look different from the two thick red ones, but they are the same shape: 6, 1, 2, 1 and 12 across, with white where the red was. One function, norwayRow(int unit, int height, String outer, String border, String cross), makes all four, and the blue bar is a rectangle. At unit 10: each red band is 220 by 60, each white band 220 by 10, the bar 220 by 20, and the flag 220 by 160.