The template code provided is intended to take two inputs, x and y, from the user and print "pass" if one or more of the following is true:
x is not less than 4
y is not greater than 5 and x + y is less than 7
However, when using De Morgan's law to simplify this code, the programmer has made some mistakes. Can you correct the errors so the code functions as intended?
/* Lesson 6 Coding Activity Question 2 */
import java.util.Scanner;
public class U3_L6_Activity_Two{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
int y = scan.nextInt();
if(!((x 5) || x+y > 7))
System.out.println("pass");
}
}

Respuesta :

import java.util.Scanner;

public class U3_L6_Activity_Two{

   public static void main(String[] args){

       Scanner scan = new Scanner(System.in);

       int x = scan.nextInt();

       int y = scan.nextInt();

       if(x>=4|| ((y < 5) && ((x+y) < 7))){

           System.out.println("pass");

       }

   }

}

I'm pretty sure this is what you're looking for. Best of luck.

For formatting and readability sake, the given code is re-written as follows:

/* Lesson 6 Coding Activity Question 2 */

import java.util.Scanner;        // 1

public class U3_L6_Activity_Two{ // 2

   public static void main(String[] args){     // 3

      Scanner scan = new Scanner(System.in);   // 4

      int x = scan.nextInt();     // 5

      int y = scan.nextInt();    // 6

      if(!((x 5) || x+y > 7))         // 7

      System.out.println("pass");    // 8

    }

}

The line that needs to be fixed is the if statement in line 7.

As given in the instruction, the code prints "pass" if one or more of the following is true:

i. x is not less than 4. This can be re-written as;

 ! ( x < 4 )

ii. y is not greater than 5 and x + y is less than 7. This can be re-written as;

! (y > 5) && (x + y < 7)

To have the right operand of ii to have a negation, we can re-write as follows;

! (y > 5) && ! (x + y > 7)  (i.e changing the less than sign to greater than and then putting the negation sign (!)). This is great so that the De Morgan's law will be applicable on both sides.

Note:

a. ! is the Java's equivalent way of writing the logical operator NOT

b. && is the Java's equivalent way of writing the logical operator  AND

Since one or more of the conditions in (i) and (ii) need to be true before the "pass" is printed, we can combine (i) and (ii) using the OR operator ( || ) as follows;

! ( x < 4 )  ||  ( ! (y > 5) && ! (x + y > 7) )           ----------------(***)

The De Morgan's laws are very great for making deductions, equivalence and valid proofs when using logical operators.

The laws are as follows;

i. ∼ (P ∧ Q) = ∼ P ∨ ∼ Q

This can be written as;

! (P AND Q) = ! P OR !Q

or in Java like syntax as

! (P && Q) = ! P || !Q

ii. ∼ (P ∨ Q) = ∼ P ∧ ∼ Q

This can be written as;

! (P OR Q) = ! P AND !Q

or in Java like syntax as

! (P || Q) = ! P && !Q

Using these laws, we can simplify our expression in (***) as follows;

! ( x < 4 )  ||  ( ! (y > 5) &&  ! (x + y > 7) )

a. Start with the right side i.e ( ! (y > 5) && ! (x + y > 7) )

The De Morgan's law (stated in (ii) above) will remove the negation from both terms in the right side term, change the && to || and then put a single negation to apply to both terms in the bracket to give the following;

! ( x < 4 )  ||  ! ( y > 5  || x + y > 7 )

b. Now, there are two main terms - ! ( x < 4 ) and ! ( y > 5  || x + y > 7 ) separated by ||

Apply the De Morgan's law (stated in (i) above) to remove the negation from both terms, then change the || to && and then put a single negation to apply to both terms.

! ( x < 4  && (y > 5  || x + y > 7))

The result in b is the simplified version of the condition in the if statement.

The complete and corrected code is therefore written as follows;

/* Lesson 6 Coding Activity Question 2 */

import java.util.Scanner;        // 1

public class U3_L6_Activity_Two{ // 2

   public static void main(String[] args){     // 3

      Scanner scan = new Scanner(System.in);   // 4

      int x = scan.nextInt();     // 5

      int y = scan.nextInt();    // 6

      if(! ( x < 4  && (y > 5 || x + y > 7) ) )         // 7

      System.out.println("pass");    // 8

    }

}

Read more about De Morgan's law at: https://brainly.com/question/13317840