METHODOLOGY. In this program, the approach that I used

Added on - 21 Sep 2019

  • 3

    pages

  • 1530

    words

  • 59

    views

  • 0

    downloads

Showing pages 1 of 3
METHODOLOGYIn this program, the approach that I used was to get towards the program goal step by step. Theprogram required three main goals or outputs to be achieved –Print the list of all independent variables in the given expressionPrint all the possible logical expressions and sub-expressionsPrint the truth table for the input expression.In our program, the approach that we used is simple as it includes completing the above tasks one afteranother. The code is made fully commented so that our approach becomes easy to understand for anyperson who reads those comments.Very first, we declared the two main required variables in the program as expressionVariables andmidExpressions, both ArrayList which will store independent variables and logical expression in theprogram. We have made a simple and clear main method by minimizing the number of statements,added three function call statements in the main method. The first function called isformulateExpression(), which is tasked to formulate and process the given expression. The secondfunction call made is makeTruthTable(), which will create truth table based on the expressionVariablesand midExpressions parameters. The third function called is printTruthTable(), which will print the truthtable passed as input to console.Now, we will discuss in depth what actually our formulateExpression function is performing –The function will get input expression from user by creating a scanner class object getExpress,this input expression is stored in actualExpression variable. Before getting input from user, theuser is given some instructions about how input expression should look like, this is done so as tominimize the error in input. Finally getExpression object is closed after input is taken.Next, first expression length is checked if its equal to 0 i.e. empty string then an exception isthrown. Otherwise we will continue with the expression processing. charactersOfExpression is achar array that is made from input expression by converting input string into char array for easein further processing.We will now traverse through each and every character of the expression so as to classify theminto operator or variable and subsequently throwing exceptions if the expression has wrongsyntax. Adding of the variable to expressionVariable list is done only after ensuring that thevariable is already not present in list. For checking, one for loop is added before adding variableto list.If the character is not found to be a variable, then the character is checked to be equal to eitheropening or closing parenthesis. If it is ‘(‘ then firstly syntax error in expression is checked and anexception is thrown if it is found. The bracket count is incremented by 1. The same is done for‘)’ character i.e. first exception is thrown if syntax is incorrect then bracketCount isdecremented.Next the bracketCount is checked each time we encounter any ‘)’ character. This count ischecked to be less than 0 that means incorrect syntax else if this count is zero or greater then it