teaching machines

Half-homework 3 – Trutilities – due October 25

Your objective in this homework is to reason about data using logical and relational operators. Expressions built out of these operators yield true/false or yes/no answers, which can ultimately be used to steer our code one way or another. You will use operators to solve several disconnected problems that have no overarching story. Sorry again.


Some of you who have programmed before may be familiar with if statements. You might be tempted to solve some of these problems with code like this:

public static boolean meetsSomeCriteria(String name) {
  if (someCondition) {
    return true;
  } else {
    return false;

This is bad form. When the condition is true, you return true. When it’s false, you return false. Why not just return the value of the condition directly?

public static boolean meetsSomeCriteria(String name) {
  return someCondition;

It’s true that boolean expressions do usually end up embedded in an if statement (or a loop), but you won’t need them for this assignment, which focuses only on boolean operators. Do not use if statements in this homework.

Another Warning

You might be tempted to see if a boolean value is true by writing something like this:

return isImportant == true;

There’s something awkward here. Let’s break down this expression into a truth table:

isImportant isImportant == true
false false
true true

The second column is identical to the first. Do you see that comparing isImportant to true doesn’t give us any information we didn’t already have with just isImportant? == true is a lot like + 0 or * 1 It is the identity operation of boolean logic, only giving back what you put in. Dispense with comparing to true:

return isImportant;

Okay, okay, you say. But what about these two constructs?

return isImportant == false;
return isImportant != true;

Let’s again check out the truth table:

isImportant isImportant == false isImportant != true
false true true
true false false

Notice how these operations just flip the value of isImportant. Equality with false and inequality with true are better expressed using the ! operator:

return !isImportant;

This construction is much more readable. Which would you rather say: a) “Is important is false?”, b) “Is important is not true?”, or c) “Is not important?”


Complete the classes described below. Place all classes in package hw3. Make all methods static.


Write class Main with a main method, which you are encouraged to use to test your code. Nothing in particular is required of it, but it must exist.


Write class Trutilities with the following methods:

  • Method isWorldWarYear that accepts a year of type int. It returns true if and only if a world war took place during that year. World War I spanned 1914-1918, and World War II spanned 1939-1945.
  • Method isDwarf that accepts a name of type String. It returns true if and only if the name refers to one of Snow White’s seven dwarf friends. Observe Disney canon; the seven dwarfs are Doc, Grumpy, Happy, Sleepy, Bashful, Sneezy, and Dopey. Allow any capitalization of the name, e.g., isDwarf("dOpEy")true.
  • Method isWithin that accepts two parameters: a haystack of type String and a needle of type String. It returns true if and only the needle is contained strictly within the haystack, but does not appear at its very start or very end. For example, isWithin("I learned it by watching you", "it")true, but isWithin("I learned it by watching you", "you")false.
  • Method hasStragglers that accepts two parameters: a number of total items and a number of columns in which to lay out the items, both of type int. It returns true if and only if the bottom row of the items is less than half full. For example, if there are 13 items grouped in 4 columns, we’d see this arrangement:
    The bottom row is less than half full, so we report true. To determine the number of items in the bottom row, consider that you are grouping the items and seeing what’s left over.
  • Method isWordRectangle that accepts four parameters, a north word, an east word, a south word, and a west word, all of type String. It returns true if the words can be arranged crossword-style, end to end, to form a rectangle. The north and south words must flow top to bottom, and the east and west words must flow left to right. For example, isWordRectangle("coffee", "early", "energy", "curse")true because they form this rectangle:
    u    a
    r    r
    s    l
  • Method isBright that accepts an RGB color String of the form #RRGGBB—which is the hexadecimal representation of color used in many applications. It returns true if the color’s luminosity is greater than 118, the luminosity of middle gray. Compute luminosity according to this formula: $0.21 \times red + 0.72 \times green + 0.07 \times blue$. Use Integer.parseInt to convert a single color channel from a hexadecimal String to an int.
  • Method isUntangled that accepts two parameters: a circle’s diameter and a square’s side length, both of type double. It returns true if and only if the circle and square do not intersect when their centers are aligned, as shown in the following figure: For example, isUntangled(8, 10)true because the circle fits cleanly inside the square.
  • Method isSameDay that accepts six parameters: a year, a month, and a day for a first date, and another year, month, and day for a second date. All parameters are of type int. It returns true if and only if the two dates are the same. For example, isSameDay(2001, 12, 25, 2001, 12, 25)true and isSameDay(2001, 12, 25, 2000, 12, 25)false.
  • Method isAfterDay that accepts six parameters: a year, a month, and a day for a first date, and another year, month, and day for a second date. All parameters are of type int. It returns true if and only if the first date happens strictly after the second. For example, isAfterDay(2001, 10, 31, 2002, 10, 15)false. Write the logic yourself; do not use any of Java’s builtin date classes.
  • Method isThisWinter that accepts three parameters: a year, a month, and a day, all of type int. It returns true if and only if the date falls in the span of this coming winter, which starts on December 21, 2019, and ends on March 19, 2020. Call upon other methods you’ve written to do much of the logic.
  • Method isHigher that accepts two musical note names of type Char. Assume each note name is a capital letter A, B, C, D, E, F, or G. The note names can be ordered in several ways. You are familiar with their alphabetic order by now:
    A B C D E F G
    Musically, however, notes have the following order, from low to high:
    C D E F G A B
    A is higher than C. This method returns true if and only if the first named note is strictly higher than the second. For example isHigher('G', 'D')true and isHigher('G', 'B')false. One of the several ways to solve this problem is to decompose the logic into three possible situations: when the first note is A, when the first note is B, and when the first note is any of the remaining notes. For each case, consider what must be true of the other note for the first to be higher.
  • Method isBlackSquare that accepts a chess position as a String. Positions are identified by their alphabetic rank (row) and numeric file (column). The official arrangement of positions is shown in the following figure: This method returns true if and only if the given position is a black square. For example, isBlackSquare("a1")true. Consider converting the rank to a number, similar to the file. What, then, is true of the numeric addresses of all the black squares?


To check your work and submit it for grading:

  1. Run the SpecChecker by selecting hw3 SpecChecker from the run configurations dropdown in IntelliJ IDEA and clicking the run button.
  2. Fix problems until all tests pass.
  3. Commit and push your work to your repository.
  4. Verify on Gitlab that your submission uploaded successfully by adding the comment test hw3 to any commit. You will receive an email of the SpecChecker results.

A passing SpecChecker does not guarantee you credit. Your grade is conditioned on a few things:

  • You must meet the requirements described above. The SpecChecker checks some of them, but not all.
  • You must not plagiarize. Write your own code. Talk about code with your classmates. Ask questions of your instructor or TA. Do not look at others’ code. Do not ask questions specific to your homework anywhere online but Piazza. Your instructor employs a vast repertoire of tools to sniff out academic dishonesty, including: drones, CS 1 moles, and a piece of software called MOSS that rigorously compares your code to every other submission. You don’t want to live in a world serviced by those who achieved their credentials by questionable means. For your future self, career, and family, do your own work.
  • Your code must be submitted correctly and on time. Machine and project issues are common—anticipate them. Commit early and often to Git. Extensions will not be granted. If you need more time to make things work, start earlier.


Leave a Reply

Your email address will not be published. Required fields are marked *