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:
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:
The program ends when the last window is closed.
Part A — stripes and a disc
vertical— three stripes side by side, eachunitwide and2 * unithigh, the first on the left. France isvertical(30, "blue", "white", "red"), and Irelandvertical(30, "green", "white", "orange").horizontal— three stripes one above another, each5 * unitwide andunithigh, the first on top. Germany ishorizontal(18, "black", "red", "gold").discandjapan— Japan’s flag is white,3 * unitwide and2 * unithigh, with a red disc in the middle whose diameter is three fifths of the height.discis the red disc alone;japanputs it on the white, withImg.overlay.Work out the disc’s radius in
uniton paper first: half of three fifths of2 * unit. Then put a check onImg.width(disc(30))inmain, and one at a unit that is not a multiple of 5, with the number worked out by hand.3 / 5is0in whole numbers; the order you multiply and divide in decides what comes out.japankeeps 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.
draw— the flag aTricolordescribes, at that unit: aswitchon its stripes, with a case for each and nodefault, handing its colors toverticalorhorizontal.Img.width(draw(france, 10))is30;Img.width(draw(germany, 10))is50.turned— the same three colors in the same order, with the stripes running the other way.Check.istakes a record, and compares it part by part: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:
blend— three vertical stripes, sized as invertical, the middle one halfway between the other two:first.interpolate(third, 0.5), as in the reading.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
switchwith a case for each kind of stripes, and nodefault, indrawand inturned.
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.
row(int unit, int height, String field, String cross)— one band across the flag,heighthigh: the field 5 units wide, the cross 2, the field 9. At unit 10 and height 40 it is 160 by 40.nordic(int unit, String field, String cross)— a band, the bar of the cross across the whole flag,16 * unitwide and2 * unithigh, and the same band again:Img.above, twice. At unit 10 it is 160 by 100.- 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. - 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.