### Recitation R7 - Java: Truth Tables Spring 2015

CS160: Foundations in Programming

It this lab, you will:
• Write code that performs logical operations
• Learn how to write simple methods
• Write a program that prints truth tables

#### Part 1: Getting started

1. Create a new project in Eclipse called R7, and a new class called R7.java.
2. Cut and paste the following code inside the curly braces for R7:
```public static boolean and(boolean b0, boolean b1) {
// add code here
}

public static boolean or(boolean b0, boolean b1) {
// add code here
}

public static boolean xor(boolean b0, boolean b1) {
// add code here
}

public static boolean nand(boolean b0, boolean b1) {
// add code here
}

public static boolean nor(boolean b0, boolean b1) {
// add code here
}

public static boolean special(boolean b0, boolean b1) {
// add code here
}

public static void printHeader(String logicalOperatorName) {
// add code here
}

public static void printRow(boolean b0, boolean b1, boolean result) {
// add code here
}

public static void main(String[] args) {

// 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 here

}

```

#### 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:

NOT(b0 AND b1) XOR (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".

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 R7.java program using the Checkin tab on the course website.

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 R7.java program using the Checkin tab on the course website, and automated testing will be enabled.