Assignment 6 — Decisions

Six small functions that decide: if with return in every branch, ? : in one line, and &&, ||, !, with the examples at the edges.

Due Oct 13, 2026

One file, Decisions.java, next to Check.java — the file from class on Wednesday, if you started it there. Fall break is Monday and Tuesday, so this one is smaller than usual.

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

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. For a function that decides, the examples that matter sit at the edge: the last value one branch takes and the first value the next one takes — for fare below, 2 and 3. Check.done() stays the last line of main.

From class: an if in Java runs a branch; it is not a value. A function hands its answer out with return, and every way through the function has to reach one.

Part A — one answer or the other

  1. fare — the fare for a ride of km kilometres: 5 for two kilometres or less, and 5 plus 2 a kilometre beyond that.

    int fare(int km)

    fare(2) is 5; fare(3) is 11. if and else, with a return in each.

  2. nightFare — the same fare, but 3 a kilometre at night.

    int nightFare(int km, boolean night)

    nightFare(3, true) is 14, nightFare(3, false) is 11, and two kilometres cost 5 at any hour. Three branches: if, else if, else.

  3. bigger — the larger of two numbers.

    int bigger(int a, int b)

    bigger(3, 8) is 8. One line, ? :, no if.

Part B — two questions at once

  1. isWeekend — whether a day, written "Mon" through "Sun", is Saturday or Sunday.

    boolean isWeekend(String day)

    isWeekend("Sat") is true. Two texts are compared with .equals, not ==.

  2. canRun — whether a run happens: it is not raining, and the temperature is at least 50.

    boolean canRun(boolean raining, int temp)

    canRun(false, 50) is true; canRun(true, 70) is false.

  3. passes — whether a student passes: at least one of two scores is over 90, and the lower of the two is over 70.

    boolean passes(int s1, int s2)

    passes(95, 65) is false: the lower score is not over 70. Give the lower score, s1 < s2 ? s1 : s2, a name inside the function, and each of the two questions a name too; then the return line reads as the rule. Java reads && before ||, and a name is the clearest way to say which grouping you mean.

Done when Check.done() prints Looks shipshape with every function checked, and a check at the edge for fare, nightFare and canRun.

If you run out of time

Part A first: fare and nightFare are where the examples at the edge live, and those go badly in a hurry. Part B’s three are each one line once the question is written down in words.

Hand in

Upload Decisions.java on Brightspace, under Assignments → A6. The file itself, not a paste, and not Check.java.

Where it is: 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 the file’s text into a file called Decisions.java on your own computer, and upload that.

Grading

Full credit for a file that runs and does 1 through 6. These cost points too: a file under any name but Decisions.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.

Most of the weight is on three things:

  • The checks, and the ones at the edge in particular: fare(2) and fare(3), nightFare(2, true), canRun(false, 50).
  • return in every branch of fare and nightFare, and bigger as one ? : line.
  • The grouping in passes: the parentheses that make it say the rule.

Presentation

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

One of the two will be asked why fare needs a return in each branch and bigger needs none.

What else to do (optional part)

Ungraded. No points, and skipping it costs nothing. Two follow-ups on A5, then one problem bigger than anything above — in Decisions.java, each function with its // line and two checks. One tool they need, said once: on two whole numbers / drops the remainder and % keeps only it, so 13 / 5 is 2 and 13 % 5 is 3, and year % 4 == 0 says divisible by 4.

Two of A5’s functions, for every input

A5’s rotate(s, k) was only asked for k up to the text’s length; past it, substring stops the program. Its swapEnds(s) was only asked for texts of two characters or more. Today’s if is what makes each one answer every input it can be given.

  1. rotate, for any k — rotate("abcde", 7) is cdeab, the same as rotating by 2, and rotate("", 3) is "". % gives the turn, and a guard for the empty text comes first, since k % 0 stops the program too.
  2. swapEnds, for any text — swapEnds("a") is "a" and swapEnds("") is "": a text with fewer than two characters has no ends to swap and comes back as it is. Say that in the // line, where a reader will look for it.

Which day of the week were you born on?

Five steps that build on each other, in this order; each one is checked before the next is started, so a mistake can only be in the newest one.

  1. isLeap — boolean isLeap(int year). A year is a leap year when it is divisible by 4, except when it is divisible by 100, except when it is divisible by 400. That is three % tests, joined with && and ||, and parentheses to say the grouping, as in passes. The checks: isLeap(2024) is true, isLeap(1900) is false, isLeap(2000) is true — all three, since the rule has two exceptions.

  2. daysIn — int daysIn(int month, int year), with months numbered 1 to 12. Three branches: February, which asks isLeap; the four months of 30, which share one branch through ||; and everything else. daysIn(4, 2026) is 30, daysIn(2, 2024) is 29, daysIn(2, 2026) is 28.

  3. isValidDate — boolean isValidDate(int month, int day, int year). One return line of &&s: the month is at least 1 and at most 12, the day is at least 1 and at most daysIn(month, year). isValidDate(2, 29, 2024) is true, isValidDate(2, 29, 2026) is false, isValidDate(13, 1, 2026) is false.

  4. weekdayNumber — int weekdayNumber(int month, int day, int year), a number from 0 to 6, where 0 is Saturday, 1 Sunday, 2 Monday, and so on to 6 Friday. The rule is Zeller’s, from 1882. Build the body as four names and a return, in this order:

    • m and y, the month and year the rule works with. January and February count as months 13 and 14 of the year before, so when month is below 3, m is month + 12 and y is year - 1; otherwise they are month and year as given. Two ? : lines.

    • k, the year within its century, y % 100; and j, the century, y / 100.

    • The number:

      return (day + 13 * (m + 1) / 5 + k + k / 4 + j / 4 + 5 * j) % 7;

    Two dates worked by hand, every number on the way, so you can check your arithmetic before the % 7:

    10/7/2026 1/1/2000
    m, y 10, 2026 13, 1999
    k, j 26, 20 99, 19
    13 * (m + 1) / 5 28 36
    k / 4, j / 4, 5 * j 6, 5, 100 24, 4, 95
    the sum, before % 7 172 259
    weekdayNumber 4, Wednesday 0, Saturday

    Those two are your first checks; the first is the day this assignment went out, the second is a January, so the shift gets used.

  5. dayOfWeek — two functions, not one. String dayName(int h) turns 0 to 6 into "Saturday" to "Friday": a chain of else if seven long, or ? : seven deep — write the one you can read. Then String dayOfWeek(int month, int day, int year) is one line that calls weekdayNumber and hands the result to dayName. dayOfWeek(10, 7, 2026) is "Wednesday", dayOfWeek(7, 20, 1969) is "Sunday".

Done when the last check is your own birthday, and the calendar on your phone agrees with it.

Three things to try

  1. Leave the braces off an if and indent two lines under it:

    int age = 30;
    if (age < 12)
        IO.println("child");
        IO.println("half price");

    Predict what prints, then run it. Java reads indentation as nothing at all; only the braces say what belongs to the if.

  2. String day = "Saturday".substring(0, 3); and then day == "Sat" — true or false? Then day.equals("Sat"). The first compares two addresses; the second compares two texts. Class 10 had the drawing.

  3. Write nightFare as one line, ? : inside ? :, and read it aloud. It works. Then decide which of the two forms you would rather read in a month, and write your answer in a // line above it.