//LogicalOperatorsTest.java /* logical operators examples. if sunny OR weekend then go outside if sunny AND weekend then go outside if NOT rainy then go outside two truth values ||'ed together are True if either or both are True. Ex. booleanExpr1 || booleanExpr2 Is True if booleanExpr1 is True or if booleanExpr2 is True (or if both are True). Is False only if both are False. two truth values &&'ed together are True only if both are True. Ex. booleanExpr1 && booleanExpr2 Is True if booleanExpr1 is True and booleanExpr2 is True. Is False if either or both are False. a truth value !'ed reverses its truth value. Ex. !booleanExpr Is True if booleanExpr is False Is False if booleanExpr is True */ import javax.swing.*; public class LogicalOperatorsTest extends JApplet { public void init () { String input; int monthNum; int days=0; int num, b, d; //test if a variable is either of two values input = JOptionPane.showInputDialog("Enter a 2 or a 5"); num = Integer.parseInt(input); if (num==2 || num==5) JOptionPane.showMessageDialog(null, "good number: is 2 or 5" + num); else JOptionPane.showMessageDialog(null, "bad number: " + num); //***** String language; language = JOptionPane.showInputDialog("Enter language (english or spanish)"); if (language.equals("english") || language.equals("spanish")) JOptionPane.showMessageDialog(null, "good language: " + language); else JOptionPane.showMessageDialog(null, "bad language: " + language); //***** input = JOptionPane.showInputDialog("Enter month number"); monthNum = Integer.parseInt(input); // 2 or more truth values ||'ed together is True if one or more are True. if (monthNum==1 || monthNum==3 || monthNum==5 || monthNum==7 || monthNum==8 || monthNum==10 || monthNum==12) days = 31; else if (monthNum==4 || monthNum==6 || monthNum==9 || monthNum==11) days = 30; else if (monthNum == 2) days = 28; else JOptionPane.showMessageDialog(null, "invalid month number"); JOptionPane.showMessageDialog(null, "days= " + days); //***** //test if number in a range of values input = JOptionPane.showInputDialog("Enter a number between 5 and 10"); num = Integer.parseInt(input); if (num>=5 && num<=10) JOptionPane.showMessageDialog(null, "in range: " + num); else JOptionPane.showMessageDialog(null, "out of range: " + num); //test if number not in a range of values if (num<5 || num>10) //logical reverse of above condition JOptionPane.showMessageDialog(null, "out of range: " + num); else JOptionPane.showMessageDialog(null, "in range: " + num); //we say "num not 2 or it's not 5", but that means in logic: //num not 2 AND it's not 5. We don't speak logically correctly. input = JOptionPane.showInputDialog("Enter a 2 or a 5"); num = Integer.parseInt(input); if (num!=2 && num!=5) //logical reverse of above condition JOptionPane.showMessageDialog(null, "bad number: is not 2 nor 5 " + num); else JOptionPane.showMessageDialog(null, "good number: " + num); language = JOptionPane.showInputDialog("Enter language (english or spanish)"); // ! is logical NOT, reverses truth/boolean value if (!language.equals("english") && !language.equals("spanish")) JOptionPane.showMessageDialog(null, "bad language: not English nor Spanish" + language); else JOptionPane.showMessageDialog(null, "good language: " + language); //enter two numbers for denominators, input = JOptionPane.showInputDialog("Enter a number"); b = Integer.parseInt(input); input = JOptionPane.showInputDialog("Enter a number"); d = Integer.parseInt(input); //check if either is zero: if (b==0 || d==0) JOptionPane.showMessageDialog(null, "bad denominator(s)"); else JOptionPane.showMessageDialog(null, "good denominators"); //check that neither is zero: if (b!=0 && d!=0) //reverse of above. swap if and else parts JOptionPane.showMessageDialog(null, "good denominators"); else JOptionPane.showMessageDialog(null, "bad denominator(s)"); } } /* Wrong wrong wrong: num==2 || 5 num==2 || ==5 num==1 || 2 || 3 num!=2 && 5 num!=2 && !=5 num<5 || 10 num<5 || >10 5<=num<=10 num!=2 || num!=5 always True, so is useless and wrong num==2 && num==5 always False, so is useless and wrong */