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
fare— the fare for a ride ofkmkilometres: 5 for two kilometres or less, and 5 plus 2 a kilometre beyond that.fare(2)is5;fare(3)is11.ifandelse, with areturnin each.nightFare— the same fare, but 3 a kilometre at night.nightFare(3, true)is14,nightFare(3, false)is11, and two kilometres cost 5 at any hour. Three branches:if,else if,else.bigger— the larger of two numbers.bigger(3, 8)is8. One line,? :, noif.
Part B — two questions at once
isWeekend— whether a day, written"Mon"through"Sun", is Saturday or Sunday.isWeekend("Sat")istrue. Two texts are compared with.equals, not==.canRun— whether a run happens: it is not raining, and the temperature is at least 50.canRun(false, 50)istrue;canRun(true, 70)isfalse.passes— whether a student passes: at least one of two scores is over 90, and the lower of the two is over 70.passes(95, 65)isfalse: 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 thereturnline 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.
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)andfare(3),nightFare(2, true),canRun(false, 50). returnin every branch offareandnightFare, andbiggeras 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.
rotate, for anyk—rotate("abcde", 7)iscdeab, the same as rotating by 2, androtate("", 3)is"".%gives the turn, and a guard for the empty text comes first, sincek % 0stops the program too.swapEnds, for any text —swapEnds("a")is"a"andswapEnds("")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.
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 inpasses. The checks:isLeap(2024)istrue,isLeap(1900)isfalse,isLeap(2000)istrue— all three, since the rule has two exceptions.daysIn—int daysIn(int month, int year), with months numbered 1 to 12. Three branches: February, which asksisLeap; the four months of 30, which share one branch through||; and everything else.daysIn(4, 2026)is30,daysIn(2, 2024)is29,daysIn(2, 2026)is28.isValidDate—boolean isValidDate(int month, int day, int year). Onereturnline of&&s: the month is at least 1 and at most 12, the day is at least 1 and at mostdaysIn(month, year).isValidDate(2, 29, 2024)istrue,isValidDate(2, 29, 2026)isfalse,isValidDate(13, 1, 2026)isfalse.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 areturn, in this order:mandy, the month and year the rule works with. January and February count as months 13 and 14 of the year before, so whenmonthis below 3,mismonth + 12andyisyear - 1; otherwise they aremonthandyearas given. Two? :lines.k, the year within its century,y % 100; andj, the century,y / 100.The number:
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,y10, 2026 13, 1999 k,j26, 20 99, 19 13 * (m + 1) / 528 36 k / 4,j / 4,5 * j6, 5, 100 24, 4, 95 the sum, before % 7172 259 weekdayNumber4, 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.
dayOfWeek— two functions, not one.String dayName(int h)turns 0 to 6 into"Saturday"to"Friday": a chain ofelse ifseven long, or? :seven deep — write the one you can read. ThenString dayOfWeek(int month, int day, int year)is one line that callsweekdayNumberand hands the result todayName.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
Leave the braces off an
ifand indent two lines under it:Predict what prints, then run it. Java reads indentation as nothing at all; only the braces say what belongs to the
if.String day = "Saturday".substring(0, 3);and thenday == "Sat"—trueorfalse? Thenday.equals("Sat"). The first compares two addresses; the second compares two texts. Class 10 had the drawing.Write
nightFareas 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.