Tutorial 1, CPSC 331, Fall 2008

home page -  news -  syllabus -  schedule -  assignments -  tutorials -  tests -  java -  references -  Mike Jacobson


Tutorial 1 -  Tutorial 2 -  Tutorial 3 -  Tutorial 4 -  Tutorial 5 -  Tutorial 6 -  Tutorial 7 -  Tutorial 8 -  Tutorial 9 -  Tutorial 10 -  Tutorial 11 -  Tutorial 12 -  Tutorial 13 -  Tutorial 14 -  Tutorial 15 -  Tutorial 16

 Review of Java Programming Fundamentals

About This Exercise

Goals of this exercise are as follows.

Students should work through this exercise as soon as possible. It is a “do-it-yourself” exercise: Teaching assistants will be available to answer questions about this exercise during the tutorials on Wednesday, September 10 and to assist you in setting up your Java environment.

You may also send email to the course instructor or ask questions about this during the instructor’s office hours if you need help to complete this exercise.

Problems To Be Solved

  1. Read Appendix A and Chapter 3 of the Textbook. These chapters are about Java fundamentals and object-oriented programming, and should be review material for students who have successfully completed Computer Science 233. Familiarity with this material will be assumed, almost immediately!

  2. Set up your Java Environment. Choose a subdirectory of your home directory for storage of Java source files as well as a completely separate subdirectory of your home directory for storage of the class files that you compile from your source files: This type of file organization is useful when are developing software for other people to use, so it is a good idea for you to get into the habit of using it now.

    Once you have chosen and created these subdirectories, make any changes to your computing environment that are needed to ensure the following.

    • The javac utility automatically searches for source files in your chosen source directory and automatically stores Java class files in (an appropriate subdirectory of) your chosen subdirectory for compiled class files.

    • The java utility automatically searches for Java class files in (an appropriate subdirectory of) the directory you have chosen for storage of these files.

    You might find that online documentation for the javac and java are helpful as you set up your environment. You will probably need to edit a file in your home directory whose name is something like either .cshrc or .profile as part of this.

  3. Get Ready To Work With the “Shapes” Hierarchy. Store each of the files Shape.java, Circle.java, Rectangle.java, ThreeD.java, and RectangularPrism.java in (a subdirectory of) your directory for Java source files. Use the javac utility to create class files corresponding to each.

  4. Carry Out One More Test: Write a short Java program that uses your “Shapes hierarchy,” in which you

    • create a Rectangle, “a,” with length 2 and height 3,
    • create a RectangularPrism, “b,” with length 2, height 3, and depth 4,
    • check to see whether a is equal to b (by calling a method of a with argument b), and
    • check to see whether b is equal to a (by calling a method of b with argument a).

    Use whichever input-output format you like (but note that input-output requirements for assignments will be more rigid).

    Then carry out the test that you have written. You should find that the results are different than they should be!

    Examine the files Rectangle.java and RectangularPrism.java carefully. Do you see why surprising output is generated by your test?

  5. Correct the Problem You Have Noticed: Information about the the getClass method is available as part of Sun’s online description of a Java object, and this will probably be helpful. How is getClass different from the instanceof method that is used in the current version of Rectangle.java and RectangularPrism.java? Why is getClass more useful than instanceof in this situation?

    Try to describe a different situation in which instanceof would be more useful than getClass instead.

  6. Implement RectangularPrism in a Different Way: The decision that RectangularPrism “is a” (kind of) Rectangle is questionable: It would also be reasonable to decide that a RectangularPrism “has a” Rectangle as one of its faces instead, so that the RectangularPrism class would not “extend” the Rectangle class and, indeed, these would not be related through inheritance at all.

    Write a different version of RectangularPrism.java that is consistent with the decision that a RectangularPrism “has a” Rectangle. How is your code different from the original version of RectangularPrism.java?

  7. Solve Additional Problems, As Time Permits: Appendix A and Chapter 3 of the textbook contain numerous additional problems that will help you to review Java programming fundamentals. Source code for the material in these chapters is available on the student companion site.


This page last modified:
http://www.cpsc.ucalgary.ca/~jacobs/cpsc331/F08/tutorials/tutorial.html