Defining Your Own Classes

Part 3:
Information Hiding and Visibility Modifiers

CSSE 120
Fundamentals of Software Development I

Rose-Hulman Institute of Technology
Computer Science & Software Engineering

Winter term, 2003-2004

Learning objectives:

After completing this project, you should be able to:
  • Define an instantiable class with multiple methods and constructors.
  • Differentiate the local and instance variables.
  • Define and use value-returning methods.
  • Distinguish private and public methods.
  • Distinguish private and public data members.
  • Describe how the result is returned from a method.
  • Define an instantiable main class.

Items in red are learning objectives for this part of the project.

Instructions:

Group work

DefiningYourOwnClasses report

  • When you see a question prefaced by
        Question: blah blah blah ...
    put your answer in your DefiningYourOwnClassesReport.txt report (more on this shortly).

Time limit

  • Most students complete this exercise in ? to ? minutes after seeing their instructor demonstrate the ideas.
  • If you find yourself spending much more time than that on this exercise, please see your instructor.

    Preliminaries

  1. Open your DefiningYourOwnClasses project and report.
  2. Skim this document. Then return to this point.
  3. Question: About how many minutes do you think it will take you to complete Part 3 of DefiningYourOwnClasses?
  4. Question: What time is it now?
  5. Local Variables, Return Values, and Parameter Passing

  6. Unless otherwise specified, use JOptionPane for all required input and output (I/O) in this project (you are free to use whatever method you prefer for other I/O).
  7. If any methods in your Compex class other than setReal and setImaginary assign values to the real and imaginary fields, replace the assignments with invocations of the setReal and setImaginary mutators.
  8. Question: What effect should replacing assignments to fields with invocations of accessors have on the behavior of the program? State whether or not you actually had to make any changes, and if you did then describe the tests that you performed to verify that the program behaves as expected.
  9. Add two new fields to your Complex class, as follows (note that making the fields public is a bad idea, but we're going to do it anyway for now):
          public double magnitude = 0.0;
    	  public double argument = 0.0;
  10. Modify your mutators (setReal, setImaginary, setMagnitude, and setArgument) so that they also assign the appropriate values to the new fields. Do not invoke the accessors (getReal, getImaginary, getMagnitude, and getArgument). Hint: consider reusing some of the code in getMagnitude and getArgument.
  11. Question: What effect should adding new fields and assignments to those new fields but not adding references to the new fields have on the behavior of the program? Describe the tests that you performed to verify that the program behaves as expected.
  12. Rewrite the getMagnitude and getArgument methods so that they return the values of the appropriate fields.
  13. Question: What effect should rewriting the getMagnitude and getArgument methods so that they return the values of the appropriate fields have on the behavior of the program? Describe the tests that you performed to verify that the program behaves as expected.
  14. Add the following statements to your main method:
          Complex part3 = new Complex();
    	  part3.setReal( 3.0 );
    	  part3.setImaginary( 4.0 );
    	  System.out.println( 
    	     "The complex number " + part3.getReal() + " + " + part3.getImaginary() + "i \n" +
    		 "has magnitude " + part3.getMagnitude() + " and argument " + part3.getArgument() + "."
    		 );
          part3.magnitude = -1.0;
    	  part3.argument = Math.PI * 2.0;
    	  System.out.println( 
    	     "But thanks to public access, we can make it look like \n" +
    	     "the complex number " + part3.getReal() + " + " + part3.getImaginary() + "i \n" +
    		 "has magnitude " + part3.getMagnitude() + " and argument " + part3.getArgument() + "."
    		 );
    		 
  15. Question: Explain why giving the magnitude and argument fields public visibility was a bad idea.
  16. Change the visibility of the magnitude and argument fields to private.
  17. Question: Ignoring the effects of roundoff, is there any way for a user of your Complex class to tell which fields exist within the class? Can they tell how the methods are implemented? This is called information hiding, and we say that the Complex class encapsulates the implementation details.

    Submitting the Project

  18. Make any final changes that you want to your project.
  19. Generate the HTML documentation for your project.
  20. Commit your changes to your CVS module (be sure to include your source file, your report, and your HTML documentation).
  21. Tag your CVS module DefiningYourOwnClasses3.
  22. Summary

  23. Question: How much time did you spend on Part 3 of DefiningYourOwnClasses? Compare your answer to how much time you had estimated that you would spend. You receive full credit no matter how far off your estimate is!