Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Check the code. Would it compile? If not, make logical change (smallest one poss

ID: 3604896 • Letter: C

Question

Check the code. Would it compile?

If not, make logical change (smallest one possible) such that it would work. Explain what was the problem.

public static int getStrippedNumber(int num) {

    int numberOfDecSets;

    if ((numberOfDecSets = num % 10) == 0) {

        return num - numberOfDecSets;

    } else {

        num= num - (numberOfDecSets + 1 );

    }

}

What would be printed (before and after changes) if the method would be invoked from main as

System.out.println(getStrippedNumber(100)); // prints

System.out.println(getStrippedNumber(55)); // prints

System.out.println(getStrippedNumber(152)); // prints

Explanation / Answer

public static int getStrippedNumber(int num) {

int numberOfDecSets;

if ((numberOfDecSets = num % 10) == 0) {

return num - numberOfDecSets;

} else {

return num - (numberOfDecSets + 1 ); //we should return value to the called function.

}

}

System.out.println(getStrippedNumber(100)); // prints 100

System.out.println(getStrippedNumber(55)); // prints 49

System.out.println(getStrippedNumber(152)); // prints 149

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote