Question

In: Computer Science

Create a Java program which prints a truth table for any 2 variables propositional function. Assuming...

Create a Java program which prints a truth table for any 2 variables propositional function.
Assuming variables p and q. Instead of the symbols for "and" and "or", just used the words. For "not", use the tilde "~"

Prompt the user for 3 questions
1: "p and q"? Or "p or q"?
2: Do you want to negate p?
3: Do you want to negate q?

For example, suppose in a menu system the user chose "p or q", and to negate q. The output should look similar to this:

p | q | (p or ~q)
------------------
T | T | T
T | F | T
F | T | F
F | F | T

Solutions

Expert Solution

Explanation::

  • Code in JAVA is given below
  • Screenshot of OUTPUT is given at the end of the code


Code in JAVA::

import java.util.Scanner;

public class TruthTable {

   public static void main(String[] args) {
       /**
       * We will use Scanner class object named sc to take input from the user
       * */
       Scanner sc=new Scanner(System.in);
      
       /**
       * In here we will ask user three questions.
       * */
       int choice,negateP,negateQ;
       System.out.print("\"p and q\"? or \"p or q\"? Enter 1 for first option or 2 for second: ");
       choice=sc.nextInt();
      
       System.out.print("Do you want to negate p? Enter 1 for yes or 2 for no : ");
       negateP=sc.nextInt();
      
       System.out.print("Do you want to negate q? Enter 1 for yes or 2 for no : ");
       negateQ=sc.nextInt();
      
       System.out.print("\n p | q | (");
       if(negateP==2) {
           System.out.print("p ");
       }else {
           System.out.print("~p ");
       }
       if(choice==1) {
           System.out.print("and ");
       }else {
           System.out.print("or ");
       }
       if(negateQ==2) {
           System.out.println("q)");
       }else {
           System.out.println("~q)");
       }
       System.out.println("-------------------------------");
      
       System.out.print(" T | T | ");
       System.out.println(value(choice,negateP,negateQ,true,true));
      
       System.out.print(" T | F | ");
       System.out.println(value(choice,negateP,negateQ,true,false));
      
       System.out.print(" F | T | ");
       System.out.println(value(choice,negateP,negateQ,false,true));
      
       System.out.print(" F | F | ");
       System.out.println(value(choice,negateP,negateQ,false,false));
      
   }
   public static char value(int choice,int negateP, int negateQ,boolean a, boolean b) {
       if(negateP==1) {
           if(a) {
               a=false;
           }else {
               a=true;
           }
       }
       if(negateQ==1) {
           if(b) {
               b=false;
           }else {
               b=true;
           }
       }
       if(choice==1) {
           //and case
           if(a==true && b==true) {
               return 'T';
           }else {
               return 'F';
           }
       }else {
           //or case
           if(a==true || b==true) {
               return 'T';
           }else {
               return 'F';
           }
       }
   }

}

OUTPUT::
TEST CASE 1:


TEST CASE 2:


Please provide the feedback!!   

Thank You!!


Related Solutions

java program Create a program that creates and prints a random phone number using the following...
java program Create a program that creates and prints a random phone number using the following format: XXX-XXX-XXXX. Make sure your output include the dashes.  Do not let the first three digits contain an 8 or 9 (HINT: do not be more restrictive than that) and make sure that the second set of three digits is not greater than 773. Helpful Hint:   Think though the easiest way to construct the phone number. Each digit does do not have to be determined...
Write a C program which prints the environment variables to a file "sample.txt" . All variables...
Write a C program which prints the environment variables to a file "sample.txt" . All variables are followed by a newline character. example: example: inputs: envp/environ = {"E1=5","E2=9",NULL} outputs: env.txt as a string would be "E1=5\nE2=9\n".
Create and submit a Python program (in a module) that contains a main function that prints...
Create and submit a Python program (in a module) that contains a main function that prints 17 lines of the following text containing your name: Welcome to third week of classes at College, <your name here>! can someone please show me how to do this step by step? I'm just confused and keep getting errors in idle.
Description: You are to develop a Java program that prints out the multiplication or addition table...
Description: You are to develop a Java program that prints out the multiplication or addition table given the users start and end range and type of table. This time several classes will be used. You are free to add more methods as you see fit – but all the methods listed below must be used in your solution. For the class Table the following methods are required: - Protected Constructor – stores the start and size of table, creates the...
create a VHDL program for the Truth Table below. Please make sure to create your code...
create a VHDL program for the Truth Table below. Please make sure to create your code for the simplified circuit. A B C Z 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 0 1 0 0 1 1 0 1 1 1 1 0 1 1 1 1 0
Construct the truth-table for the following propositional formulas. In each case, explain whether the formula is...
Construct the truth-table for the following propositional formulas. In each case, explain whether the formula is a tautology, a contradiction, or neither. (Explain how you arrive at this conclusion.) (a) ¬((p → ¬p) → ¬p) (b) (p → (q ∧ r)) → (¬r → ¬p) (c) (p → ¬q) → ¬(¬p → q)
Python please! Create one program to include all 10 elements: Create a function that prints a...
Python please! Create one program to include all 10 elements: Create a function that prints a sentence passed to the function as parameter. On the next line, print the same sentence without the 1st and the last character, use string slicing. Use the following sentence: This is the sentence. Given a string: This string was copied in an article. Replace the relevant words to read as follows: This string was discovered in the article xyz. Given a string: This string...
Multithreaded programming Write a multithreaded program (JAVA) that prints messages with thread IDs. 1. Create at...
Multithreaded programming Write a multithreaded program (JAVA) that prints messages with thread IDs. 1. Create at least three user-threads. 2. Each thread needs to be terminated after printing each thread ID.
1)  Recall, a truth table for a proposition involving propositional symbols p and q uses four rows...
1)  Recall, a truth table for a proposition involving propositional symbols p and q uses four rows for the cases p true, q true, p true, q false, p false, q true and p false, q false (in that order). For example  the outcome for p v ¬q  is  T, T, F, T  since the expression is only false when q is true but p is false. Of course, we have the same outcome for any logically equivalent proposition including ¬(¬p ∧ q), (¬p ∧...
Q2) (a) construct a truth table for the following function: ?(?, ?, ?) = (?? +...
Q2) (a) construct a truth table for the following function: ?(?, ?, ?) = (?? + ?̅?) ̅̅̅ + ?? (b) Use the truth table of (a) to write the function F in sum of minterms form. (c) Expand the function ?(?, ?, ?) = ? + ?̅? to product of Maxterms form. (d) Simplify the following function using K-Map. ?(?, , ?, ?, ?) = ?̅? + ?? + ?̅? + ??̅?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT