Colorado State University

Recitation R11 - Java: Truth Tables
Spring 2014

CS160: Foundations in Programming



The purpose of this lab is to incorporate Java with propositional logic.


Write a program that produces the truth table of the statement: (p ^ q) v (¬q ^ r) from scratch in the following phases:

Phase 1

  1. Create a new project in Eclipse called R11, and a new class called R11.java.
  2. Add a scanner to read from the console.
  3. Add a print statement which says: (p^q)v(!q^r) == ?
  4. Add a do-while loop to perform the following:
  5. Test that your program works. The output at this point should be:
    (p^q)v(!q^r) == ?
    Would you like to assign new truth values?: y

    Would you like to assign new truth values?: n
    Goodbye!

Phase 2

  1. Add prompts for the values of p, q, and r
  2. Print out the values of the variables to test that they were set correctly
  3. Test that your program works. The output at this point should be:
    (p^q)v(!q^r) == ?
    Would you like to assign new truth values?: y
    Enter a value for p: hat
    "hat" is not a valid boolean value; p will be set to false by default
    Enter a value for q: f
    Enter a value for r: t
    p is false
    q is false
    r is true

    Would you like to assign new truth values?: y
    Enter a value for p: true
    Enter a value for q: false
    Enter a value for r: t
    p is true
    q is false
    r is true

    Would you like to assign new truth values?: n
    Goodbye!


Phase 3

  1. Replace the print lines stating the truth values (eg. "p is true/false") with print lines for values found in each part of the truth table
  2. If you want to line up your print statements in columns, you can use "\t" in your printed string for a tab
  3. Test that your program works. The output at this point should be:
    (p^q)v(!q^r) == ?
    Would you like to assign new truth values?: y
    Enter a value for p: t
    Enter a value for q: f
    Enter a value for r: t
    p	   q	  r      !q     (p^q)   (!q^r)    (p^q)v(!q^r)
    true   false  true   true   false   true      true

    Would you like to assign new truth values?: n
    Goodbye!

Show your R11.java program to the TA for grading and submit to RamCT to get credit for this lab.

© 2014 CS160 Colorado State University. All Rights Reserved.