Groundwork Groundwork

Practice: a film club flyer

The Riverside Film Club needs a flyer for Friday's showing, and the person who used to make them has left. You are going to print one, a piece at a time, until the whole thing comes out in one go.

Everything here uses only what Chapter 1 covered: IO.println, text in quotes, joining with +, arithmetic, and a comment. No variables—those arrive in Chapter 2.

This is what you are working toward:

======================================
         RIVERSIDE FILM CLUB
======================================

Feature:  The Quiet Harbor  (1998)
When:     Friday, 7:30 pm
Where:    Room 14

Seats available: 96
Snack bar total: 17.25

======================================

Two of those numbers are not typed in. The club has 8 rows of 12 seats, and Friday's snack order is 3 boxes of popcorn at 3.75 each and 2 drinks at 3.00 each. Let Java do that arithmetic—if the price changes, you want to edit one number, not recount.

Steps

Step 1. Start with the banner. Print the rule, the club name, and the rule again—three lines, exactly as they appear above. The rule is 38 equals signs, and the club name is indented by nine spaces.

Show a solution
void main() {
    IO.println("======================================");
    IO.println("         RIVERSIDE FILM CLUB");
    IO.println("======================================");
}

The rule is typed out in full. "=".repeat(38) would be shorter, but that is Chapter 3.

Step 2. Print the feature line: Feature: The Quiet Harbor (1998), with two spaces after the colon and two before the year, as in the flyer. Use a single IO.println with one string.

Show a solution
void main() {
    IO.println("Feature:  The Quiet Harbor  (1998)");
}

One string, spaces and all, exactly as it should appear.

Step 3. Now a variation on that line, built from two pieces joined with +: "Feature:" and "The Quiet Harbor (1998)". Run the box as it is first, and notice that + puts nothing at all between the pieces. Then join a space, " ", in between them, so the line reads Feature: The Quiet Harbor (1998): one space after the colon this time (the flyer keeps two), and two before the year, as before.

Show a solution
void main() {
    IO.println("Feature:" + " " + "The Quiet Harbor  (1998)");
}

+ puts nothing between the pieces it joins, so the space after the colon has to be a string of its own, " ". When the spacing matters, put it inside a string.

Step 4. Print the two booking lines together, When: and Where:, as two separate IO.println lines, lining the values up under each other exactly as in the flyer.

Show a solution
void main() {
    IO.println("When:     Friday, 7:30 pm");
    IO.println("Where:    Room 14");
}

Two lines, and the values line up because the spaces are inside the strings.

Step 5. Print the seat count as Seats available: 96—but work out the 96 with a calculation from 8 rows of 12 seats, not by typing it.

Show a solution
void main() {
    IO.println("Seats available: " + (8 * 12));
}

8 * 12 rather than 96: change the row count and the flyer stays right. The parentheses are the chapter's habit; a product is worked out first even without them, but a sum, as in step 6, needs them.

Step 6. Print the snack total as Snack bar total: 17.25, again calculated: 3 boxes of popcorn at 3.75, plus 2 drinks at 3.00.

Show a solution
void main() {
    IO.println("Snack bar total: " + (3 * 3.75 + 2 * 3.00));
}

The parentheses make Java finish the arithmetic before it joins the answer onto the text. The space after the colon is inside the string, since + adds none of its own.

Step 7. Put the whole flyer together in one program, blank lines and all, with a comment on the first line saying what it prints. Every line of the target output, in order.

Show a solution
// Prints the flyer for Friday's Riverside Film Club showing.

void main() {
    IO.println("======================================");
    IO.println("         RIVERSIDE FILM CLUB");
    IO.println("======================================");
    IO.println();
    IO.println("Feature:  The Quiet Harbor  (1998)");
    IO.println("When:     Friday, 7:30 pm");
    IO.println("Where:    Room 14");
    IO.println();
    IO.println("Seats available: " + (8 * 12));
    IO.println("Snack bar total: " + (3 * 3.75 + 2 * 3.00));
    IO.println();
    IO.println("======================================");
}

The comment says what the program is for, not what each line does. Blank lines come from IO.println() with nothing in it.