Defining Your Own Classes

Part 1:
Defining Instantiable Classes

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.


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.


  1. Create a new project using the Basic Console Application template and the project name DefiningYourOwnClasses.
  2. Create a file called DefiningYourOwnClassesReport.txt, placing it in the docs subfolder of your DefiningYourOwnClasses project.
  3. Open your DefiningYourOwnClassesReport.txt file. You will use it to record your answers to the questions in all of the parts of this exercise. Start the report with an appropriate title, your names, the date, and a heading to indicate the beginning of Part 1.
  4. Skim this document. Then return to this point.
  5. Question: About how many minutes do you think it will take you to complete Part 1 of DefiningYourOwnClasses?
  6. Question: What time is it now?
  7. Creating the CVS Module

  8. Create a new module for your DefiningYourOwnClasses project, calling the module DefiningYourOwnClasses, spelled just like that.
  9. Add your report and the initial contents of your DefiningYourOwnClasses project to your new CVS module.
  10. Documenting the Project

  11. Write JavaDoc comments based on the planned behavior of the project. Include all of the required tags. Ensure that you can generate the HTML documentation correctly.
  12. Defining Instantiable Classes

  13. 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)..
  14. Add an introductory message to the main method. When it is finished, the program will prompt the user for two complex numbers and display the results of various operations on those numbers.
  15. Ensure that the program exits gracefully when it finishes.
  16. Modify the main method so that it declares and creates a Complex object.
  17. Question: Explain why the project won't compile at this point.
  18. Using the New Class command from the Project menu, create the Complex class.
  19. Question: Explain why the project won't compile at this point.
  20. Add return statements to the get methods (accessors) to return the values of the appropriate fields. At this point your program should compile.
  21. Modify the set methods (mutators) so that they change the values of the appropriate fields. Add statements to your main method to test your class.
  22. Question: Describe the tests that you used to convince yourself that your class is working.
  23. Add methods double getMagnitude and double getArgument that return the values suggested by their names. Do not add any new fields to your class.
  24. Question: Describe the tests that you used to convince yourself that your getMagnitude and getArgument methods are working.
  25. Add methods void setMagnitude( double theMagnitude ) and void setArgument( double theArgument ) that change the real and imaginary components of the Complex object so that the indicated property is set, but the other property is not changed.
  26. Question: If you execute x.setMagnitude( someValue ) and then immediately execute x.getMagnitude(), you could get back something slightly different from someValue. Find such a value and explain why this happens.
  27. Add methods void add( Complex addend ) and void subtract( Complex subtrahend ) that change the real and imaginary components in the way suggested by their names.
  28. Question: Suppose that you wanted to be able to execute a statement like x = x.add( y ), where x and y are Complex objects. What would the return type of the add method have to be?
  29. Add methods void multiply( Complex multiplicand ) and void divide( Complex divisor ) that change the real and imaginary components in the way suggested by their names.
  30. Submitting the Project

  31. Make any final changes that you want to your project.
  32. Generate the HTML documentation for your project.
  33. Commit your changes to your CVS module (be sure to include your source file, your report, and your HTML documentation).
  34. Tag your CVS module DefiningYourOwnClasses1.
  35. Summary

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