teaching machines

Half-homework 3 – Trutilities – due October 25

August 9, 2019 by . Filed under cs1, fall 2019, specifications.

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:

Submission

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: