Experiments in Java


Problems for Session J2: Objects and Methods

Problem J2-A: Shifting points

Add a shiftAndPrint method to PointPrinter that moves a point right 3, up 3, left 6, down 6, right six, up 3, and left 3, printing the result at each step.

Problem J2-B: Shifting points, revisited

Add a shiftAndPlot method to PointPrinter that moves a point right 3, up 3, left 6, down 6, right six, up 3, and left 3, plotting the result at each step. Write a program that reads in the point and uses the helper to plot the point at each position.

Problem J2-C: Drawing a grid

Write a program, Grid.java, that prints out a 3 by 3 grid of points on the plane. Make the spacing in the grid one unit.

Problem J2-D: Drawing a grid, revisited

Write a program, Grid.java, that prints out a 3 by 3 grid of points on the plane. Input the spacing in the grid and the center point in the grid.

Problem J2-E: Happiness

Write a program, Smiley.java that draws a smiley face by plotting points appropriately.

Problem J2-F: Initials

Write a program, Initials.java, that draws your initials by plotting points appropriately.

Problem J2-G: Rainbow Text

Using the techniques from Experiment J2.8, draw your name in a rainbow of colors. (Your name will appear in each color.)

Problem J2-H: Other Drawing Methods

In this session, you learned about six methods provided by java.awt.Graphics for drawing basic shapes: drawString, drawLine, drawRect, drawOval, fillRect, and fillOval. Are these all that's available? Consult the Java documentation and write short summaries of the other drawing methods.

Problem J2-I: Determining Text Width

Instead of the rainbow-drawing technique from Problem J2-G, you might want to draw each letter in a different color. However, this makes it necessary for you not only to change the color before drawing each letter, but also to determine the correct place to draw each letter. Does Java give you the tools for doing so? Yes. There is a FontMetrics class that helps you determine the width of strings. Read the documentation on that class and write instructions that a classmate could use to write his or her name with each letter in a different color.


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

Source text last modified Tue Oct 26 12:55:42 1999.

This page generated on Tue Oct 26 15:38:00 1999 by Siteweaver.

Contact our webmaster at rebelsky@math.grin.edu