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

Consider the following instance variables and incomplete method that are part of

ID: 3712609 • Letter: C

Question

Consider the following instance variables and incomplete method that are part of a class that represents an item. The variables years and months are used to represent the age of the item, and the value for months is always between 0 and 11, inclusive. Method updateAge is used to update these variables based on the parameter extraMonths that represents the number of months to be added to the age.

private int years;
private int months; // 0 <= months <= 11

//precondition: extraMonths >= 0
public void updateAge(int extraMonths)
{
     /* body of updateAge    */
}

Which of the following code segments could be used to replace /*body of updateAge */ so that the method will work as intended?

I. int yrs = extraMonths % 12;
     int mos = extraMonth / 12;
     years = years + yrs;
      months = months + mos;

II. int totalMonths = years * 12 + months + extraMonths;
        years = totalMonths / 12;
        months = totalMonths % 12;

III. int totalMonths = months + extraMonths;
         years = years + totalMonths / 12;
         months = totalMonths % 12;

A) I Only

B) I, II, and III

C) II and III

D) II Only

E) III Only

Explanation / Answer

let years = 2,months = 6,extramonths = 7

I

yrs = 7%12 = 7

mos = 7/12 = 0

years = 2+ 7 = 9

months = 6+0   

So this is incorrect implementation of the updateAge() method

II

totalmonths = 2*12 +6+7 = 37

years = 37/12 = 3

months = 37%12 = 1

3 years 1 month is correct . So II is correct

III

totalmonths = 6+7 = 13

years = 2+ 13/12 = 2+1 = 3

months = 13%12 = 1

3 years and 1 month is the correct answer . So III is also correct.

The correct option is :

C) II and III?

Do ask if any doubt. Please upvote.

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