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.
Warning
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?”
Requirements
Complete the classes described below. Place all classes in package hw3
. Make all methods static
.
Main
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.
Trutilities
Write class Trutilities
with the following methods:
- Method
isWorldWarYear
that accepts a year of typeint
. 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 typeString
. 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 typeString
and a needle of typeString
. 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
, butisWithin("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 typeint
. 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:xxxx xxxx xxxx x
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 typeString
. 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:coffee u a r r s l energy
- Method
isBright
that accepts an RGB colorString
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$. UseInteger.parseInt
to convert a single color channel from a hexadecimalString
to anint
. - Method
isUntangled
that accepts two parameters: a circle’s diameter and a square’s side length, both of typedouble
. 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 typeint
. It returns true if and only if the two dates are the same. For example,isSameDay(2001, 12, 25, 2001, 12, 25)
→true
andisSameDay(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 typeint
. 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 typeint
. 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 typeChar
. 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 C D E F G A B isHigher('G', 'D')
→true
andisHigher('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 aString
. 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?
Submission
To check your work and submit it for grading:
- Run the SpecChecker by selecting
hw3 SpecChecker
from the run configurations dropdown in IntelliJ IDEA and clicking the run button. - Fix problems until all tests pass.
- Commit and push your work to your repository.
- 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.