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
- Create a new project in Eclipse called R11, and a new class called R11.java.
- Add a scanner to read from the console.
- Add a print statement which says:
(p^q)v(!q^r) == ?
- Add a do-while loop to perform the following:
- Ask the user if they want to assign new values.
- Print a blank line if the input is "y", otherwise prints "Goodbye!" if the user types "n".
- 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
- Add prompts for the values of p, q, and r
- If the first character of the string is a t, the value is true
- If the first character of the string is a f, the value is false
- Otherwise, tell them that it's not valid, and the default is false
- Print out the values of the variables to test that they were set correctly
- 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
- 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
- You should have: p, q, r, !q, (p^q), (!q^r), (p^q)v(!q^r)
- How do you do "AND", "OR" and "NOT" in Java versus how it is written in propositional logic?
- It might be useful to add variables for functions you need more than once (eg. (!q^r)).
- If you want to line up your print statements in columns, you can use "\t" in your printed string for a tab
- 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.