An encapsulation violation occurs when private information that should not be changed by external classes is (usually unintentionally) exposed to external modification. Consider the following...


Question 10


An encapsulation violation occurs when private information that should not be changed by external<br>classes is (usually unintentionally) exposed to external modification. Consider the following<br>(complete) Java class that is supposed to be immutable:<br>1. import java.awt.Point;<br>2. import java.math. BigDecimal;<br>3. public final class Encapsulation<br>4. {<br>5. private final Point myPoint;<br>6. private final String[] myStrings;<br>7. private final BigDecimal myNumber;<br>8. public Encapsulation(final Point thePoint,<br>final String[] theStrings,<br>final BigDecimal theNumber)<br>9. {<br>10. myPoint - thePoint;<br>11. myStrings = theStrings;<br>12. myNumber = theNumber;<br>13.}<br>14. public Point getPoint()<br>15. {<br>16. return myPoint;<br>17.}<br>18. public String[) getStrings()<br>19. {<br>20. return (String[])myStrings.clone();<br>21.}<br>22. public BigDecimal getNumber()<br>23. {<br>24. return myNumber;<br>25.}<br>26. }<br>Does this class exhibit any encapsulation violations? If not, answer no, with brief reasons for why<br>encapsulations did not occur. If yes, briefly describe each encapsulation violation and explain how<br>you would fix it. Note that the class does compile as written, so do not look for syntax errors in the<br>code.<br>

Extracted text: An encapsulation violation occurs when private information that should not be changed by external classes is (usually unintentionally) exposed to external modification. Consider the following (complete) Java class that is supposed to be immutable: 1. import java.awt.Point; 2. import java.math. BigDecimal; 3. public final class Encapsulation 4. { 5. private final Point myPoint; 6. private final String[] myStrings; 7. private final BigDecimal myNumber; 8. public Encapsulation(final Point thePoint, final String[] theStrings, final BigDecimal theNumber) 9. { 10. myPoint - thePoint; 11. myStrings = theStrings; 12. myNumber = theNumber; 13.} 14. public Point getPoint() 15. { 16. return myPoint; 17.} 18. public String[) getStrings() 19. { 20. return (String[])myStrings.clone(); 21.} 22. public BigDecimal getNumber() 23. { 24. return myNumber; 25.} 26. } Does this class exhibit any encapsulation violations? If not, answer no, with brief reasons for why encapsulations did not occur. If yes, briefly describe each encapsulation violation and explain how you would fix it. Note that the class does compile as written, so do not look for syntax errors in the code.

Jun 11, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here