Java Lab 10
Fall 2022
In this lab, you will practice with encapsulation – or the lack thereof.
Create a project named Lab10. Right click on the src icon and create a new Package named DataPackage.
Download Lab10Main.java and copy it into src; download Data.java, DataSnooper.java, and ContainedClass and copy them all into DataPackage. Compile and run the program. Take a look at class DataSnooper.
As you do each problem below, ask yourself, what’s wrong? Where is encapsulation being broken?
1. Add fixes to Data and DataSnooper for the part labeled Problem 1 to enforce encapsulation.
2. Add fixes to those classes for Problem 2.
3. Add fixes for Problem 3.
4. Add fixes for Problem 4.
5. Add fixes for Problem 5. Note that this also involves ContainedClass.
6. Change Data.setCc( ) to do a deep copy: create a new ContainedClass, copy the parameter’s data into it, then set Data’s field.
7. Add code to Data to enforce the following rules. Then add tests at the end of DataSnooper that attempt to violate each rule using object d; print out d to show that the rule holds.
a. iValue must be positive; if not, do not reset it. b. sValue can have a maximum of 10 characters. c. iList can have a maximum of 4 entries.
d. iList’s entries must be between 10 and 20 inclusive.
Deliverable: Zip up all your .java files and upload the zip file to Canvas.