Ask Question
14 August, 00:41

Iven the code fragment below with nested if statements, rewrite the code to combine all the tests into one if statement with multiple conditionals. Paste (or type) the rewritten code into your answers document.

int feb = 28;

if ((year % 4) = = 0) / / assume that year is an integer with a valid year value

{

if ((year % 100) ! = 0)

{

System. out. println ("This is a leap year");

feb = 29;

}

}

If the value of year is 2020, what will the value of feb be after this code runs?

+1
Answers (2)
  1. 14 August, 00:52
    0
    public class Main {

    public static void main (String [] args) {

    int year = 2016;

    int feb = 28;

    if ((year % 4) = = 0 && (year % 100) ! = 0)

    {

    System. out. println ("This is a leap year");

    feb = 29;

    }

    }

    }

    Explanation:

    We can use the and logical operator, &&, to join the nested if statements into one single statement. The and operator will first evaluate the condition year % 4 is equal to zero and then followed with checking if year % 100 is not equal to 100. If only both conditions are met, it will evaluated to true and then go in to the if block to print the message "This is a leap year" and set 29 to variable feb.
  2. 14 August, 04:02
    0
    See Explanation Below

    Explanation:

    The new code segment is

    int feb = 28;

    if ((year % 4) = = 0 && (year % 100) ! = 0) / / assume that year is an integer with a valid year value

    {

    System. out. println ("This is a leap year");

    feb = 29;

    }

    Assume year = 2020, the assigned value of feb is 29;

    Reason below;

    At line 2 of the new code segment, two conditions are tested both of which must be satisfied.

    1. year % 4 = = 0

    2020 % 4 = 0

    0 = 0 (True)

    2. year % 100! = 0

    2020 % 100! = 0

    20! = 0 (True)

    Since both conditions are true, the value assigned to feb will be 29 and

    "This is a leap year" will be printed without the quotes
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Iven the code fragment below with nested if statements, rewrite the code to combine all the tests into one if statement with multiple ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers