Question

Write a program that generates a simple three statement calculator. It should display the following menu:...

Write a program that generates a simple three statement calculator. It should display the following menu:

media%2Ffc9%2Ffc9dc3b3-b8da-4d70-9e95-5c
When an option other than (quit) is selected, it should prompt the user to enter the appropriate number of statement letters (1 for negation ; 2 for everything else). And then it should return the resulting truth column.

It should accept only A, B, C, F, T, R, M where A=[11110000], B= [11001100], C=[10101010], F=[00000000], T=[11111111], and R and M are the current and previous resultants respectively which are both initially [00000000].

The program should continue until quit is selected.
0 0
Add a comment Improve this question Transcribed image text
Answer #1
enum Input
{

    A
        {
            @Override
            int[] getArray() {
                return new int[]{1,1,1,1,0,0,0,0};
            }
        },
    B
        {
            @Override
            int[] getArray() {
                return new int[]{1,1,0,0,1,1,0,0};
            }
        },
    C
        {
            @Override
            int[] getArray() {
                return new int[]{1,0,1,0,1,0,1,0};
            }
        },
    F
        {
            @Override
            int[] getArray() {
            return new int[]{0,0,0,0,0,0,0,0};
          }
        },
    T
        {
            @Override
            int[] getArray() {
                return new int[]{1,1,1,1,1,1,1,1};
            }
        };
    abstract int[] getArray();
}
class Propositions
{
    static int[] arr1;
    static int[] arr2;
    static int[] current=new int[]{0,0,0,0,0,0,0,0};
    static int[] prev=new int[]{0,0,0,0,0,0,0,0};
    public static void main(String[] args)
    {
        boolean exitFlag=false;
        Scanner sc=new Scanner(System.in);
        String input1,input2;
        while(!exitFlag)
        {
            System.out.println("Simple propositional calculator\n1.Negation\n2.Conjunction\n3.Disjunction\n4.Implication\n5.Equivalence\n6.Quit");
            int choice=sc.nextInt();
            switch (choice)
            {
                case 1:
                    System.out.println("Enter a number from A,B,C,F,T,R,M:");
                    input1=sc.next();
                    if(input1.equals("R"))
                    {
                        current=negation(current);
                    }
                    else if(input1.equals("M"))
                    {
                        current=negation(prev);
                    }
                    else
                    {
                        current=negation(Input.valueOf(input1).getArray());
                    }
                    prev=current; //for next iteration
                    break;
                case 2:
                    System.out.println("Enter 2 numbers from A,B,C,F,T,R,M:");
                    input1=sc.next();
                    input2=sc.next();
                    assignActualArguments(input1,input2);
                    current=conjunction(arr1,arr2);
                    prev=current; //for next iteration
                    break;
                case 3:
                    System.out.println("Enter 2 numbers from A,B,C,F,T,R,M:");
                    input1=sc.next();
                    input2=sc.next();
                    assignActualArguments(input1,input2);
                    prev=current; //for next iteration
                    break;
                case 4:
                    System.out.println("Enter 2 numbers from A,B,C,F,T,R,M:");
                    input1=sc.next();
                    input2=sc.next();
                    assignActualArguments(input1,input2);
                    prev=current; //for next iteration
                    break;
                case 5:
                    System.out.println("Enter 2 numbers from A,B,C,F,T,R,M:");
                    input1=sc.next();
                    input2=sc.next();
                    assignActualArguments(input1,input2);
                    prev=current; //for next iteration
                    break;
                case 6:
                    exitFlag=true;
                    break;
            }
        }
        sc.close();
    }
    public static int[] negation(int[] arr)
    {
        int[] res=new int[arr.length];
        for(int i=0;i<arr.length;i++)
        {
            res[i]=arr[i]^1;
        }
        System.out.println(Arrays.toString(res));
        return res;
    }
    public static int[] conjunction(int[] arr1,int[] arr2)
    {
        int[] res=new int[arr1.length];
        for(int i=0;i<arr1.length;i++)
        {
            res[i]=arr1[i]|arr2[i]; //bitwise or
        }
        System.out.println(Arrays.toString(res));
        return res;
    }
    public static int[] disjunction(int[] arr1,int[] arr2)
    {
        int[] res=new int[arr1.length];
        for(int i=0;i<arr1.length;i++)
        {
            res[i]=arr1[i]&arr2[i];
        }
        System.out.println(Arrays.toString(res));
        return res;
    }
    public static int[] implication(int[] arr1,int[] arr2)
    {
        int[] res=new int[arr1.length];
        for(int i=0;i<arr1.length;i++)
        {
            res[i]=arr1[i]==1 && arr2[i]==0?0:1; //based on truth table of implication
        }
        System.out.println(Arrays.toString(res));
        return res;
    }
    public static int[] equivalence(int[] arr1,int[] arr2)
    {
        int[] res=new int[arr1.length];
        for(int i=0;i<arr1.length;i++)
        {
            res[i]=(arr1[i]==1 && arr2[i]==1) ||(arr1[i]==0 && arr2[i]==0)?1:0; //based on truth table of equivalence
        }
        System.out.println(Arrays.toString(res));
        return res;
    }
    public static void assignActualArguments(String s1,String s2)
    {
        if(s1.equals("R"))
        {
            arr1=current;
        }
        else if(s1.equals("M"))
        {
            arr1=prev;
        }
        else
        {
            arr1=Input.valueOf(s1).getArray();
        }
        if(s2.equals("R"))
        {
            arr2=current;
        }
        else if(s2.equals("M"))
        {
            arr2=prev;
        }
        else
        {
            arr2=Input.valueOf(s2).getArray();
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
Write a program that generates a simple three statement calculator. It should display the following menu:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (PYTHON) Write a program that displays the following menu:. , Write Algorithm for Code Geometry Calculator...

    (PYTHON) Write a program that displays the following menu:. , Write Algorithm for Code Geometry Calculator 1. Calculate the Area of a Circle 2. Calculate the Area of a Rectangle 3. Calculate the Area of a Triangle 4. Quit Enter your choice (1 - 4): If the user enters 1, the program should ask for the radius of the circle and then display its area. If the user enters 2, the program should ask for the length and width of...

  • extra credit 1 Write a program that will display the following menu and prompt the user...

    extra credit 1 Write a program that will display the following menu and prompt the user for a selection: A) Add two numbers B) Subtract two numbers C) Multiply two numbers D) Divide two numbers X) Exit program The program should: display a hello message before presenting the menu accept both lower-case and upper-case selections for each of the menu choices display an error message if an invalid selection was entered (e.g. user enters E) prompt user for two numbers...

  • C++ programming For this assignment, write a program that will act as a geometry calculator. The...

    C++ programming For this assignment, write a program that will act as a geometry calculator. The program will be menu-driven and should continue to execute as long as the user wants to continue. Basic Program Logic The program should start by displaying a menu similar to the following: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a triangle 3. Quit Enter your choice(1-3): and get the user's choice as an integer. After the choice...

  • Python 3.7 to be used. Just a simple design a program that depends on its own. You should also not need to import anythi...

    Python 3.7 to be used. Just a simple design a program that depends on its own. You should also not need to import anything. No code outside of a function! Median List Traversal and Exception Handling Create a menu-driven program that will accept a collection of non-negative integers from the keyboard, calculate the mean and median values and display those values on the screen. Your menu should have 6 options 50% below 50% above Add a number to the list/array...

  • Java only please Write a program that displays the following menu: Geometry Calculator 1.       Calculate the...

    Java only please Write a program that displays the following menu: Geometry Calculator 1.       Calculate the Area of a Circle 2.       Calculate the Area of a Triangle 3.     Calculate the Area of a Rectangle 4.       Quit Enter your choice (1-4): If the user enters 1, the program should ask for the radius of the circle and then display its area. Use the formula:      area = ∏r2    Use 3.14159 for ∏. If the user enters 2 the program should ask for...

  • Write a C program that does the following: • Displays a menu (similar to what you...

    Write a C program that does the following: • Displays a menu (similar to what you see in a Bank ATM machine) that prompts the user to enter a single character S or D or Q and then prints a shape of Square, Diamond (with selected height and selected symbol), or Quits if user entered Q. Apart from these 2 other shapes, add a new shape of your choice for any related character. • Program then prompts the user to...

  • Write a C++ program that will display the following menu and work accordingly. A menu that...

    Write a C++ program that will display the following menu and work accordingly. A menu that will display the following choices and uses functions to carry out the calculations: 1. Call a function named classInfo() to ask the user the number of students and exams in a course. 2. Call a function getGrade() that will get the information from classInfo() and asks the user to enter the grades for each student. 3. Call a function courseGrade() to display the average...

  • **This is for the C Language. Trigonometric Calculator Specification You are required to write a program...

    **This is for the C Language. Trigonometric Calculator Specification You are required to write a program that calculates and displays values of the trigonometric functions sine, cosine, and tangent for user-supplied angular values. The basic program must comply with the following specification. 1. When the program is run, it is to display a short welcome message. TRIG: the trigonometric calculator 2. The program shall display a message prompting the user to enter input from the keyboard. Please input request (h-help,...

  • Code should be written in C++ 2. [11] You have been hired by Shapes Calculator Menu Maker to create a C++ console a...

    Code should be written in C++ 2. [11] You have been hired by Shapes Calculator Menu Maker to create a C++ console application that prompts the user with a menu and various options. Starting from Lab03, prompt the user for a char value that corresponds to one of the following menu options: a. Sphere=4*pi"radius3 (hint: use a constant value for pi=3.1415) b. Right Circular Cylinder- piradius*height c. Rectangular Parallelepiped length *width height d. Cone-*pi*radius2 height 3 x. Exit menu. Implement...

  • Assignment Write a menu-driven C++ program to manage a class roster of student names that can...

    Assignment Write a menu-driven C++ program to manage a class roster of student names that can grow and shrink dynamically. It should work something like this (user input highlighted in blue): Array size: 0, capacity: 2 MENU A Add a student D Delete a student L List all students Q Quit ...your choice: a[ENTER] Enter the student name to add: Jonas-Gunnar Iversen[ENTER] Array size: 1, capacity: 2 MENU A Add a student D Delete a student L List all students...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT