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

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 = 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
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
