Lab - Animating Binary Search

The files you need for this assignment can be downloaded here.


Contents:

  • Overview
  • Files you will need
  • Using the Driver
  • Your Tasks
  • Overview of java.awt.Graphics
  • Handing in your completed lab

  • Overview

    This week, we will look at the binary search algorithm on a sorted array. The textbook illustrates the algorithm in Figure 8.8 on page 360. An example of a similar figure is included below. It was generated by our own "solution" to this lab.

     
    Figure: Searching for key '28' in the array generated from seed=0

    Our goal for this lab is to have you create such illustrations using Java's Graphics package (java.awt.Graphics), which is part of their Abstract Windowing Toolkit (AWT).

    Files you will need

    Although you will only need to modify one file (Animate.java), you will need to download several files we provide.

    Using the Driver

    We are providing you with an Applet which contains the full code for binary search. This program also creates a blank palette for your illustrations. Before discussing the details of the graphcs, let's discuss the binary search implementation. This is a recursive procedure for search an array. At each level of the recursion, the driver calls the routine Animate.drawArray, which you must write. All code you provide should be in the file Animate.java.

    The information you have to work with is:

    When you run the applet, you must first enter a variety of parameters, and then press the "run" button. In particular, the algorithm will create a sorted array of N integers, chosen at random (based on the random 'seed'). At this point, it starts the binary search algorithm on the array, looking for the given 'key' value.

    Your Tasks

    In the final section of this handout, we will give more details about using Java's Graphics package. Before doing this however, we want you to have an idea of the types of things you will want to do.

    Mostly, we want you to do your best on this lab. If you do not get pictures which look exactly like the text's, so be it. The TA will use judgement as to whether you have made enough progress in grading your work.

    Please keep in mind that your Animate.drawArray routine should draw a single view of the array as it appears at one level of the recursion. The full illustration, with multiple views of the array, is created as the driver calls your routine multiple times.

    We strongly suggest that you try to tackle as many of the following stages as possible:

    Overview of java.awt.Graphics

    When the user instructs the applet to run binary search, a window is created with height and width specified by the user. Your code contains a variable Graphics gimage in the file Animate.java, which is an instantiation of the java.awt.Graphics class. You will need to use this variable to make most graphics call.

    The coordinate system for the Graphics object is defined so that the origin (0,0) is located at the top-left corner of the screen. Increasing the x-coordinate signifies moving farther to the right; increasing the y-coordinate signifies moving farther downward.

    The Graphics package is designed so that there is always a current pen color, though this choice of color can be changed as needed. At any point, you are allowed to draw basic shapes by specifying the relevant geometry in the coordinate system. When you draw on a graphics, you will be drawing on top of whatever you have previously drawn on that part of the coordinate system. For example, if you want black writing on a blue square, you will need to first draw the blue square and then draw the black text. Doing things in the reverse order will have the effect of painting the blue square overwriting the black text.

    Full documentation for the latest version of the Java.awt.Graphics package can be found online at,

    http://java.sun.com/j2se/1.3/docs/api/java/awt/Graphics.html

    Highlights of java.awt.Graphcis:

    For your convenience (trust us), we have provided an additional method (which we wrote) that displays a string, centered at a particular (x,y) point. The method is, This is a method include in the file Animate.java, so you can call it directly (it is not a method for the gimage object).

    Handing in your completed lab

    You do not have to submit any files electronically. You should instead make a printout of the modified file Animate.java and make sure your name is written clearly at the top of the file. Do this for each of the two weeks, to mark your relative progress.


    Last modified: 15 November 2002