Advanced Programming - CS239

Department of Computer Science

LAB 11: EXPERIMENTING WITH ACCESSIBILITY/VISIBILITY

 

Getting Ready: Before going any further you should:

1. Make a directory on your N: drive for this lab.

2. Setup your development environment.

3. Download the file Document.java from this directory

4. Download  the file FormattedDocument.java from this directory

5. Download the  file Driver.java from this directory

 

Follow the lab instructions in the lab_accessibility.pdf.  You will turn in this document at the conclusion of doing lab work, no later than Monday at the start of your lab period.  You do not need to submit any code.  Be sure you do try each experiment, do the prediction on paper, then see if your prediction is true by carrying out the code.  Be sure you understand each step as you finish it.

 

Lab 11 – Accessibility and Visibility Deliverable 

(to be printed and turned in by Monday at the beginning of lab)

  Name ________________________________________________________

 

Do steps 1..5 and put the files on your  your flash drives or floppies in addition to your N drive  then PAUSE and STUDY the classes you have just created.  As part of your studying, annotate the classes with comments that tell what you think is significant about each method. Continue to do this as you make the changes described in this lab.

 

Answer the following questions:

1.What is the relationship between FormattedDocument and Document?

 

2.Who has access to the attributes of Document?

 

3.What is happening in FormattedDocument in the line:  super(text);

 

 

Part 1:

1. What output do you expect will occur when you compile all the classes and execute the driver?  (answer this BEFORE compiling and running the driver).

 

 

What output did you get?

 

Does this match your answer above?   yes  or no

If not, why not?

 

2. Don't change the declaration of the variable named doc but change the line containing

doc = new Document(text); to doc = new String(text);. Re-compile the driver.

What error was generated?

 

Why?

 

3. Don't change the declaration of the variable named doc but change the line that now

contains doc = new String(text); to doc = new FormattedDocument(text,

20);.

 

4. Re-compile the driver. Why did it compile even though there appear to be

incompatible types?

 

 

 

 

5. Re-execute the driver. Show the output below.

 

 

 

Did it output what you expected?

 

 

If so, why? If not, why not?

 

 

 

6. The getText() method in the FormattedDocument class contains the line temp =

super.getText();. Explain this line of code.

 

 

 

 

7. Replace the line temp = super.getText(); with the line temp = getText(). Recompile this class and re-execute the driver. What happened and why?

 

 

 

 

 

Part II: In this part of the lab you will experiment with changing the  accessibility/visibility of attributes and methods.

 

1. Make the accessibility of the getDelimiters() method in the Document class

private. Re-compile only the Document class. What happens and why?

 

 

 

2. Re-compile only the FormattedDocument class. What happens and why?

 

 

 

3. Now make the getDelimiters() method protected and re-compile all of the

classes. What happens and why?

 

 

 

4. What's the difference between the public version and the protected version?

 

 

 

Which is better?

 

 

 

Why?

 

 

 

5. Perform the same experiments with the getWordCount()method.

Which version is better?

 

 

 

 Why?

 

 

 

 

6. Change the accessibility of the delimiters and text attributes in the Document class to protected. What changes can you now make to the FormattedDocument class?

(Hint: Think about how the FormattedDocument class accesses these attributes.)

 

 

 

 

7. Do you like these changes? Why or why not?

 

 

 

8. Now that the delimiters attribute is protected, do you still need the

getDelimiters() method?

 

 

 

9. Should either the getDescription() or getText() mehods in the Document class be protected?

 

 

 

Why or why not?