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.
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:
Point
object where its coordinates are strings, with x="A" and y="c".Point
object where its coordinates are integers, with x=13, y=-5.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:
Point2
object where one coordinate is a String and another is an integer, with x="A" and y=3.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:
NumericPoint
object where its coordinates are doubles, with x=-5.8 and y=109.47.NumericPoint
object where its coordinates are integers, with x=13, y=-5.NumericPoint
object where its coordinates are strings, with x="A" and y="c". Your program should not compile with this line.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.
cd
command to go to the location of your .java file:
javac
program. After typing in javac
at the terminal, you leave a space, then the name of the file to compile (including the extension), then finally press enter. For example:
java
program. You first type in java, then a space, then the name of the class to run (it must have a main method), then press enter. Do not include .java or .class when entering the name of the class. For example:
cd
command to go to the location of your .java file:
javac
program. After typing in javac
at the terminal, you leave a space, then the name of the file to compile (including the extension), then finally press enter. For example:
java
program. You first type in java, then a space, then the name of the class to run (it must have a main method), then press enter. Do not include .java or .class when entering the name of the class. For example:
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.