Experiments in Java


Problems for Session J3: Building Your Own Classes

Problem J3-A: Shifting points

Add shiftLeft(int amt), shiftRight(int amt), shiftUp(int amt), and shiftDown(int amt) methods to the MyPoint class. Write an appropriate test procedure that demonstrates the use of these methods.

Problem J3-B: Refined points

We designed the MyPoint class so that points have integer values, which means that it is impossible to have a point at positions with fractional components, such as (1.5, 3.2). Update MyPoint to permit such positions.

Problem J3-C: Distance from origin

Add a distance method to the MyPoint class. This method should compute the distance from the origin (traditional distance, involving diagonals). Note that to compute a square root in Java, you should use Math.sqrt(...). Write an appropriate series of tests for this method.

Problem J3-D: A date class

Create a Date class with three fields, year, month, and day. Your class should include at least one constructor as well as methods to extract the year, month, and day from a date. Write an appropriate TestDate class to test the methods and constructors of the Date class.

Problem J3-E: Converting dates to strings

Add a String toString() method to the Date class. Your method should return a string corresponding to the date. For example, if the year were 1998, the month 6, and the day 12, you might return the string 6/12/1998 (for American users). Write appropriate tests for this method.

Problem J3-F: Stick Figures

Create a StickFigure class, similar in purpose to our bordered-square class.

Problem J3-G: House Components

Create a number of classes (Window, etc.) that one might use to aid in drawing houses.

Problem J3-H: Boxed Text

Create a class, BoxedText, whose objects display not only a colored string, but an appropriate rectangle around the string. That is, the rectangle should precisely surround the text. Longer messages should have bigger rectangles.

Warning! You will need the answer from Problem J2-I in order to solve this problem. You will also need to read more about java.awt.FontMetrics.


Copyright (c) 1998 Samuel A. Rebelsky. All rights reserved.

Source text last modified Mon Oct 25 15:25:02 1999.

This page generated on Tue Oct 26 15:37:59 1999 by Siteweaver.

Contact our webmaster at rebelsky@math.grin.edu