Defining Your Own Classes

Part 2:
Instantiable Classes and Constructors

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 2 of DefiningYourOwnClasses?
  4. Question: What time is it now?
  5. Instantiable Classes and Constructors

  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. Comment out the constructor for your Complex class.
  8. Question: Does the program still compile and run correctly?
  9. Question: How does Java handle the situation in which a class has no constructors?
  10. Add a new constructor that accepts two double parameters and uses them as the real and imaginary components of the object.
  11. Question: What happens if you now try to create a new Complex object without providing the real and imaginary components?
  12. Modify your main program to provide the real and imaginary components that your new constructor requires.
  13. Question: How does Java handle the situation in which a class has at least one constructor but no default constructor?
  14. Question: What would happen if you tried to add a new constructor that accepts two double parameters and uses them as the magnitude and argument of the object?
  15. Uncomment the default constructor and verify that you can now create objects either with or without specifying the real and imaginary components.
  16. Question: How does Java handle the situation in which a class has more than one constructor?
  17. Add another new constructor that accepts a String parameter and parses it to obtain the real and imaginary components of the object. You should assume that the parameter has the form <double>+<double>i. You will need to use the indexOf method of the String class.
  18. Modify your main program so that it prompts the user for two complex numbers in the form expected by your constructor, and displays their sum, difference, product, and quotient.
  19. Submitting the Project

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

  25. Question: How much time did you spend on Part 2 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!