Ask Question

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?

+5
Answers (2)
  1. 13 March, 03:50
    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
  2. 13 March, 03:53
    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.
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 & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers