Prompt We have learned how to use accessor and mutator methods to access private class member data. It is possible to instead make the class members public, which would allow other programs to directly retrieve and modify the class member data without needing to write the accessor and mutator methods. This is usually considered bad practice. Why do you think that is? What do you think the benefits are to writing accessor and mutator methods instead of just leaving the variables public? Below is the definition for a class called Counter. Define a new method for this class called "findDifference". This method should take another Counter object as an argument and return the difference in the counts between the counter being called and the one passed as an argument. The difference should be given as an absolute value (not returned as a negative). See below the class definition for examples of this method being used. public class Counter { private int count; public Counter() { count = 0; } public int countUp() { count++; return count; } public int getCount() { return count; } } Examples of findDifference: Counter "a" has count 5. Counter "b" has count 10. Counter "myCount" has count 200. Counter "yourCount" has count 0. a.findDifference(b); // This returns 5 b.findDifference(a); // This also returns 5 myCount.findDifference(yourCount); // This returns 200 yourCount.findDifference(b); // This returns 10 b.findDifference(myCount); // This returns 190

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter11: Inheritance And Composition
Section: Chapter Questions
Problem 12PE
icon
Related questions
Question

Prompt

  1. We have learned how to use accessor and mutator methods to access private class member data. It is possible to instead make the class members public, which would allow other programs to directly retrieve and modify the class member data without needing to write the accessor and mutator methods. This is usually considered bad practice. Why do you think that is? What do you think the benefits are to writing accessor and mutator methods instead of just leaving the variables public?
  2. Below is the definition for a class called Counter. Define a new method for this class called "findDifference". This method should take another Counter object as an argument and return the difference in the counts between the counter being called and the one passed as an argument. The difference should be given as an absolute value (not returned as a negative). See below the class definition for examples of this method being used.

public class Counter {
private int count;

public Counter() {
count = 0;
}

public int countUp() {
count++;
return count;
}

public int getCount() {
return count;
}
}

Examples of findDifference:

Counter "a" has count 5. Counter "b" has count 10. Counter "myCount" has count 200. Counter "yourCount" has count 0.

a.findDifference(b); // This returns 5

b.findDifference(a); // This also returns 5

myCount.findDifference(yourCount); // This returns 200

yourCount.findDifference(b); // This returns 10

b.findDifference(myCount); // This returns 190 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Class
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning