AP Notes, Outlines, Study Guides, Vocabulary, Practice Exams and more!

need help with lab 19b

1 post / 0 new
ehere09's picture
Joined: Mar 2008
need help with lab 19b

Assignment Purpose:

The purpose of this program is to use recursion to draw a set of squares,
which become smaller as they reach the other side of the screen.

Write a program which start by drawing a 200 X 200 square at the top-left corner of the monitor. Continue drawing squares that become steadily smaller as they are displayed to right side of each preceding square. The bottom of the square stays at the same level. Each square is 25 percent smaller than the previously drawn square. Start with the student version, shown below.

Provided Student Program for Lab19b

// Lab19bst.java
// The student version of the Lab19b assignment.

import java.awt.*;
import java.awt.event.*;

public class Lab19bst
public static void main(String args[])
Windows win = new Windows();
win.addWindowListener(new WindowAdapter() {public void
windowClosing(WindowEvent e) {System.exit(0);}});

class Windows extends Frame
public void paint(Graphics g)
drawSquare(g, 0, 100, 200);

public void drawSquare(Graphics g, int x, int y, int size)

In the student version you are provided with a completed main method. This method is designed to execute graphics application, which done by completing the Windows class. Make no changes to the main method.

Inside the Windows class, is the paint method that controls the graphics output with an object of the Graphics class. You will need to create a method, called drawSquare, which actually draws the squares and calls itself recursively until the base case is reached.

The recursive process exits when it is true that either the square size becomes smaller than four pixels or the border of the monitor is reached. You must be aware that only full-sized squares must be displayed. Keep this in mind as you determine if you are reaching the border of the monitor.

80 Point Version

The 80 point version draws one set of squares from left to right, starting with a 200X200 square
that is drawn with a top-left corner at coordinate (0,100). Each square is drawn with a 10 pixel space between the squares. Each square is ¾ the size of the previous square.

100 Point Version

The 100 point version draws a second set of squares in the same manner as the 80 point version, but this time the large square start on the right side and successive squares are drawn moving to the left side of the screen.

Need Help?

We hope your visit has been a productive one. If you're having any problems, or would like to give some feedback, we'd love to hear from you.

For general help, questions, and suggestions, try our dedicated support forums.

If you need to contact the Course-Notes.Org web experience team, please use our contact form.

Need Notes?

While we strive to provide the most comprehensive notes for as many high school textbooks as possible, there are certainly going to be some that we miss. Drop us a note and let us know which textbooks you need. Be sure to include which edition of the textbook you are using! If we see enough demand, we'll do whatever we can to get those notes up on the site for you!