1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Vladimir [108]
3 years ago
12

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
Computers and Technology
1 answer:
velikii [3]3 years ago
3 0

Answer:

e. II and III

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

<em />years = 6<em> and </em>months = 8<em />

<em>updateAge(5) will update years to 7 and month 1</em>

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

<u>Code I</u>

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

<u>Code II</u>

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

<u>Rough Calculation</u>

years = 3; months = 6; extraMonths = 15

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

Conclusion: Code II is correct

<u>Code III</u>

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

<u>Rough Calculation</u>

years = 3; months = 6; extraMonths = 15

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

Conclusion: Code III is correct

You might be interested in
Which editing options are available when making a custom show?
IRISSAK [1]

adding slide, removing slide, and changing order of slides


8 0
3 years ago
A + B (AC (B +C’) D) = A + BD
zmey [24]
Yo what is that i’m confused
5 0
4 years ago
Read 2 more answers
All users at your site are using the Bash shell. You want to set a variable that will apply to every user and always have the sa
podryga [215]

Explanation:

I assume you are on a single host.

To modify the .bashrc for all NEW users, you can edit/modify (with su privilege) the file

/etc/skel/.bashrc

save the file before creating new users.

Be sure to know what you're doing, and test the file before you use it host-wide.

For existing users, you can have them do the modifications/additions individually in case they have already modified their own version.

4 0
4 years ago
quarters are about 1 inch in diameter. how many quarters would be required to stretch from one end of a football field to the ot
dedylja [7]

so there are about 240 quarters in a foot

a football field is 360 feet long if you times these together you would get 86,400

4 0
4 years ago
When planning the structure of a spreadsheet, columns are for _______ items and rows are for _______ items.
Ivan
C: single and single

I hope it helps :)
5 0
3 years ago
Other questions:
  • Alex is nearing graduation. His counselor showed him this information. Alex's College Costs &amp; Payment Options per Year Costs
    5·2 answers
  • The sequence of folders to a file or folder is called a(n) ________
    8·1 answer
  • The second step when using the problem-solving process is to
    7·1 answer
  • Why coaxil cable called coxial cable ?
    15·2 answers
  • In c++ 11, the ________ tells the compiler to determine the variable's data type from the initialization value.
    6·1 answer
  • What would be used by a business to assess how the business is working within its organization goals? O A. Information systems B
    8·1 answer
  • Which of the following are logical reasons for using a pie chart to show how employees spend their time at ABC organization? Cho
    14·1 answer
  • Write a small program that takes in two numbers from the user. Using an if statement and an else statement, compare them and tel
    12·1 answer
  • With a(n) ____ you can arrive at a scene, acquire the data you need, and return to the lab as quickly as possible.
    14·1 answer
  • Suppose your team is finishing a project and the supervisor stresses ensuring the material cannot be erased. To what memory leve
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!