204k views
5 votes
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
public void updateAge(int extraMonths)
{
/* body of updateAge */
}
Which of the following code segments shown below could be used to replace /* body of updateAge */ so that the method will work as intended?
I int yrs = extraMonths % 12;
int mos = extraMonths / 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. II only
c. III only
d. I and II only
e. II and III

1 Answer

6 votes

Answer:

e. II and III

Step-by-step explanation:

Given

The above code segment

Required

Which can complete the updateAge() method

From the program we understand that the updateAge() will update the year and month based on the extraMonths passed to it.

For instance


years = 6 and
months = 8

updateAge(5) will update years to 7 and month 1

Having established that, next we analyze options I, II and III

Code I

1. This divides extraMonths by 12 and saves the remainder in yrs.

For instance: 15 months = 1 year and 3 months. So:


yrs = 3

2. This divides extraMonths by 12 and saves the whole part in mos

For instance: 15 months = 1 year and 3 months. So:


mos = 1

3. This updates the value of years by the result of 1 (i.e. the remaining months)

4. This updates the value of years by the result of 2 (i.e. the remaining years)


months = months + mos;

Conclusion:, (I) is incorrect because years and months were updated with the wrong values

Code II

1. This converts years to months, then add the result of the conversion to extraMonths + month

For instance:
years = 3; months = 6; extraMonths = 15


totalMonths = 3 * 12 + 6 + 15 = 57\ months

2. This calculates the number of years in totalMonths


years = totalMonths / 12;

i.e
years = 57/12 = 4

3. This divides totalMonths by 12 and saves the remainder in months


months = totalMonths \% 12;

i.e.
months = 57\%12 = 9

Rough Calculation


years = 3; months = 6; extraMonths = 15


3\ years + 6\ months + 15\ months = 4\ years\ 9\ months

Conclusion: Code II is correct

Code III

1. This calculates the total months

For instance:
years = 3; months = 6; extraMonths = 15


totalMonths = 6 + 15 = 21\ months

2. This calculates the number of years in totalMonths, then add the result ot years


years = years + totalMonths / 12;

i.e.
years = 3 + 21/12 = 3 + 1 = 4

3. This divides totalMonths by 12 and saves the remainder in months


months = totalMonths \% 12;

i.e.
months = 21\%12 = 9

Rough Calculation


years = 3; months = 6; extraMonths = 15


3\ years + 6\ months + 15\ months = 4\ years\ 9\ months

Conclusion: Code III is correct

User Moris Kramer
by
3.9k points