Recitation 2

Introduction:

In this recitation, you will have a chance to practice writing classes that use generics. The first few minutes of recitation will review generics, including topics not discussed in lecture (constraining generics). The second and third class below (NumericPoint and Point2) use generics concepts not discussed in lecture.

Point Class

Write a generic Point class. This class should hold two coordinates: x and y. The type of x and y should be generic, that is the programmer using the Point class should specify what type of data they want x and y to be. Use Generics to accomplish this. Both x and y should be the same type. In addition to these two fields, have the following methods:

Demonstrate that your Point class works by:

  1. Create a Point object where its coordinates are strings, with x="A" and y="c".
  2. Create another Point object where its coordinates are integers, with x=13, y=-5.
  3. Print out both objects (either using the accessor methods or writing a toString method and using it).

Point2 Class

The above Point class allows for any data to be used as coordinates, but the coordinates must be of the same (or very similar) types. However, there are some coordinate systems (e.g. alpha-numeric grid) that use different types for each coordinate. Write a Point2 class that allows each coordinate to be a different type. The Point2 class must store a x and y coordinate and provide the same methods as the Point class. Is it possible to create this class by extending the Point class?

Demonstrate that your Point2 class works by:

  1. Create a Point2 object where one coordinate is a String and another is an integer, with x="A" and y=3.
  2. Print out the object (either using the accessor methods or writing a toString method and using it).

Numeric-Only Point Class

If you would like an added challenge, try implementing this class. Eventually, we will talk about constraining generics, but if you are interested in getting ahead, you can try learning it on your own.

The above Point class allows for any data to be used as coordinates. However, since most coordinates are numbers, write a NumericPoint class that restricts the types to only being numeric (e.g. Integer, Float, or Double). You may find it helpful to know that the numeric wrapper classes all descend from the Number class. The NumericPoint class must store a x and y coordinate and provide the same methods as the Point class. You may implement this new class either by trying to extend the Point class above or by writing a new class.

Demonstrate that your NumericPoint class works by:

  1. Create a NumericPoint object where its coordinates are doubles, with x=-5.8 and y=109.47.
  2. Create another NumericPoint object where its coordinates are integers, with x=13, y=-5.
  3. Print out both objects (either using the accessor methods or writing a toString method and using it).
  4. Create a NumericPoint object where its coordinates are strings, with x="A" and y="c". Your program should not compile with this line.

Command-Line Compiling

Since your first assignment requires that your program compile from the command line, this recitation will also give you practice compiling from the command line. Unfortunately, different operating systems (and versions of operating systems) are a little different. The steps below should help get you to compile at the command line. If you have any problems, please ask the TA or the instructor.

Submission and Grading:

This is for your practice. You should be able to complete the first program by the end of recitation, and probably the second program. If you complete a program, call the TA over to look over your program to make sure it is correct. If you do not have time to complete the programs before the end of recitation, you have the option of uploading it to CourseWeb once you are finished to receive feedback on it from the TA. If you upload it after Monday, September 21, please email the TA so he knows to look at it.