### Recitation R6 - Java: Truth Tables Summer 2016

CS160: Foundations in Programming

#### Preview

The goal of this lab is:
• Take a practice programming quiz for attendence
• Turn in Homework 1
• Write a simple program that prints truth tables
• Work on Hackerrank problems to prepare for the midterm

#### Practice Quiz 2

1. Follow the instructions on the sheet of paper handed to you by your TA
3. Once you complete the quiz, you can start the R6.java!

#### Part 1: Getting started

1. Create a new project in Eclipse called R6, and a new class called R6.java.
2. Cut and paste the following methods into R6:
```    public static boolean and(boolean b0, boolean b1) {
}

public static boolean or(boolean b0, boolean b1) {
}

public static boolean xor(boolean b0, boolean b1) {
}

public static boolean nand(boolean b0, boolean b1) {
}

public static boolean nor(boolean b0, boolean b1) {
}

public static boolean special(boolean b0, boolean b1) {
}

public static void printHeader(String logicalOperatorName) {
}

public static void printRow(boolean b0, boolean b1, boolean result) {
}

```
3. Cut and paste the following code into you main method:
```    // Print truth table (AND)
printRow(true, true, and(true,true));
printRow(true, false, and(true,false));
printRow(false, true, and(false,true));
printRow(false, false, and(false,false));

// add code to test the other methods

```

#### Part 2: Complete the logical operation methods

1. Complete the 5 methods that perform logical operations: AND, OR, XOR, NAND, and NOR.
• It is possible to complete each method with one line of code.
2. Complete the method, special, so it computes the following logical expression:

(b0 XOR b1) AND (NOT(b0) OR b1)

#### Part 3: Complete the methods that help print the truth table

1. Complete the printHeader method so prints exactly the following on one line of output (read carefully):
"b0" followed by a tab, then "b1" followed by a tab, then "b0" followed by a space, then the operator name followed by a space, then "b1".
• the escape character for tab is "\t"
Done correctly, the output should similar to this:
```
b0     b1     b0 AND b1
```
2. Complete the printRow method so it prints the following:

The value of b0 followed by a tab, then the value of b1 followed by a tab, then the value of result.

Done correctly, the output should similar to this:
```
true     false     false
```

#### Part 4: Printing full truth tables and submit your code

Test your completed methods by using calling them inside the main() curly braces. In the code that was provided, you have a full example of printing a correct truth table for the AND method.

When you are satisfied that your all methods work correctly, submit your R6.java program using the Checkin tab on the course website.

#### Practice Problems

In order to receive full credit for this recitation, all methods(and(), or(), nand(), xor(), nor(), special(), printHeader(), and printRow()) must work as specified. We will not be evaluating code you write inside your main().

You must submit your R6.java program using the Checkin tab on the course website, and automated testing will be enabled.