Colorado State University

Recitation R6 - Java: Truth Tables
Summer 2016

CS160: Foundations in Programming


Preview

The goal of this lab is:

Practice Quiz 2

  1. Follow the instructions on the sheet of paper handed to you by your TA
  2. Download QuizInterface.java into the src folder for Q2
  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) {
            // 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
        }
    
    
  3. Cut and paste the following code into you main method:
        // Print truth table (AND)
        printHeader("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.
  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". 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.

© 2016 CS160 Colorado State University. All Rights Reserved.