For the following program in PYTHON, please add: <command> -> <bool_expr> <bool_
ID: 3829382 • Letter: F
Question
For the following program in PYTHON, please add:
<command> -> <bool_expr>
<bool_expr> -> <bool_term> {OR <bool_term>}
<bool_term> -> <not_factor> {AND <not_factor>}
<not_factor> -> {NOT} <bool_factor>
<bool_factor> -> BOOL | LPAREN <bool_expr> RPAREN | <comparison>
<comparison> -> <arith_expr> [COMP_OP <arith_expr>]
<arith_expr> -> <term> {ADD_OP <term>}
<term> -> <factor> {MULT_OP <factor>}
<factor>-> LPAREN <arith_expr> RPAREN | FLOAT | INT
You will also have to define and accept tokens to represent the boolean values 'True' and 'False', the boolean operators 'and', 'or' and 'not' and the comparison operators '<', '>', '<=', '>=', '==', '!=':
BOOL: 'True' or 'False'
AND: 'and'
OR: 'or'
NOT: 'not'
COMP_OP: '<', '>', '<=', '>=', '==', '!='
CODE:
TEST if everything works:
Pluto 2.0>>>8.5 > 6 and 8 + 3 == 11 and 72 <=100 and 5 != 2+6 and 100 >= 20 or 4 < 2
True
Pluto 2.0>>>not not not True
False
Pluto 2.0>>>9 * 4 + 4 > 17
True
Pluto 2.0>>>not(8/2 > 3.14) or 7 < 10 and 10 < 1
False
Pluto 2.0>>> 8 * 2.0 == 16
True
Pluto 2.0>>>7 + 6 * 2 != 15
True
Pluto 2.0>>>8 + 9 != > 10
Parser error
Expected: NUMBER
Got: >
line 1 position 9
Pluto 2.0>>>True or True and False
True
Pluto 2.0>>>(True or True) and False
False
Pluto 2.0>>>not True or True
True
Pluto 2.0>>>not (True or True)
False
Explanation / Answer
package Largest;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class LargestMain {
public static void main(String[] args) {
int number = 0;
String numStr = "";
int []myArray = new int[10];
int count = 1;
int largest = 0;
boolean valid = false;
while(valid == true); { // Loop to check validity
for(int i = 0; i < myArray.length; i++) {
myArray[i] = i + 1;
numStr = JOptionPane.showInputDialog("Please enter number " + count++ + ":");
number = Integer.parseInt(numStr); // Converts string value to integer
if(number >= largest) {
largest = number;
}
// If-Else if statements checks if values entered are equal to 0-9
if(number >= 0 && number <= 9) {
valid = true;
}
else if ((!(number >= 0 && number <= 9))) {
valid = false;
}
if (valid == false) {
JOptionPane.showMessageDialog(null, "INVALID INPUT...Try Again!!!", "Results", JOptionPane.YES_OPTION);
continue;
}
}
JOptionPane.showMessageDialog(null, "The Largest Number Is: " + largest, "Results", JOptionPane.PLAIN_MESSAGE);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.