13.8 Review Excercises 13


Exercise 13.8

  1. Assuming that g is a Graphics object, state the error in each statement.

    (a) g.setColor(Color.Red);
    (b) g.setFont("Serif");

  2. Write a program that will display a window that is 500 pixels wide and 400 pixels high. The window should contain the following, drawn as indicated:

    • In the upper left quadrant, the name Joanne, in a monospaced font, inside a solid blue circle
    • In the lower left quadrant, the name Man-Kwan, in a serif font, inside an outlined red square
    • In the upper right quadrant, the name John, in a sans serif font, inside a solid magenta rectangle
    • In the lower right quadrant, the name Kevin, in an italic font, inside an outlined cyan ellipse
    • In the centre, the name Vaughn, in a bold italic font, inside a solid yellow isosceles triangle

  3. Write a program that allows the user to click on two points within a blank window and then draws the rectangle that has those points as the endpoints of one of its diagonals. If the points are aligned vertically or horizontally, the program should simply join the two points with a line segment.

  4. Write a program that allows a user to click on three non-collinear points in a window and then draws the circle that passes through those points. If the user selects three collinear points, the program should draw a message that indicates this.
  5. Projects

  6. Write a program that asks the user to submit the time in hours and minutes in two text fields. The program should then draw a representation of the given time as it would appear on an analog clock. The "clock" should consist only of a circular region containing two line segments - a short one for the hour hand and a longer one for the minute hand.

  7. Write a program that first presents the user with a window that contains a blank square area with labelled text at the bottom. The label should direct the user to enter an integer value n in the range 2 <= n <= 100. After the user enters the number and presses < enter >, the program should draw n filled circles, each of radius four pixels. These circles should be arranged at equal intervals around the perimeter of a large circle. The diameter of the large circle should be determined by the size of the region; it should have a diameter equal to 90% of the lesser of the width and height of the region.

  8. Although we can draw a filled circle fairly easily using the fillOval method, this question asks you to draw such a figure the hard way, one pixel at a time. Specifically, you are to draw the graph of the relation x^2 + y^2 <= 4 (a circular disk with radius 2 and centre at the origin). In making your drawing, position the point (0,0) at the centre of the display area and try using a scale of one unit = 200 pixels. Set the scale by defining a constant:
     final int SCALE = 200; // pixels per unit
    This should produce a drawing that will fit nicely on the screen of your computer but, if it does not, write the program so that you can change the size of the image easily by changing the value of the constant.  To draw the disk, for each pixel corresponding to a point in the square region in which -2 <= x <= 2 and -2 <= y <= 2, test whether the sum of the squares of the coordinates of the point is less than or equal to 4. If so, the point lies in the disk. If the point lies in the disk, fill in the pixel by invoking the method fillOval with a width and height of 1.

  9. Modify the program of the previous question to draw the graph of the Mandelbrot set. This is a set of points on an Argand diagram, a coordinate system in which the complex number z = a + ib is represented by the point (a, b). To determine whether or not a complex number c is in the Mandelbrot set, we examine the terms of the following sequence.

                 z1 = c
                 zk = zk - 1^2 + c, if k > 1

    The value c is in the Mandelbrot set if and only if the terms of the sequence never get "large". To determine whether or not this sequence will ever get large, look at up to thirty terms, testing each one to see whether or not |Zk| > 2. (In geometric terms, this checks to see if the point lies outside the circle of radius 2, centred at the origin.) If any term of the sequence ever satisfies this condition, the sequence is guaranteed to get large. (What we mean by this is that, as k gets large, the value of Zk is guaranteed to eventually become larger than any given value you care to name. As an example, the sequence 1,2,4, ... gets large in this sense.) If the sequence has not produced a point that is more than two units from the origin after thirty iterations, you can depend on it never growing large. In this case, the point c is part of the graph of the Mandelbrot set on an Argand diagram. If a point is in the set, use the drawOval method to add it to the set, as in the previous question. The domain of your graph should be from -2 to 1 (along the real axis) while the range should be from -1 to 1 (along the imaginary axis).

  10. Write a program that allows two people to play tic-tac-toe against each other. The program should present a square area that is divided into nine smaller squares by vertical and horizontal lines. If a user clicks the mouse in an empty region, the program should place a large X or a large O (on alternate turns) into the middle of that area. The program should not permit any move after a player has won the game. The window should also have a "Restart" button at the bottom to allow users to start a new game at any time.

  11. Modify the program in the previous question so that a user can play against the computer. Try to make the computer's moves reasonably intelligent.

  12. The game called Life was invented in 1970 by the British mathematician John Conway. It is played by one person on a (theoretically) unbounded grid of squares. Each cell on the grid represents an organism which can, at any time, be either alive or dead. Which organisms are alive changes from generation to generation according to the number of neighbouring eels that are alive. Births and deaths take place according to the following rules:

    • The neighbours of a given cell are the eight cells that are adjacent to it vertically, horizontally, or diagonally.
    • If a cell is alive but has only zero or one neighbours that are alive, then in the next generation the cell dies of loneliness.
    • If a cell is alive and has four or more neighbours that are alive, then in the next generation the cell dies of overcrowding.
    • A living cell with either two or three living neighbours remains alive in the next generation.
    • If a cell is dead, then in the next generation it will become alive if it has exactly three neighbours that are already alive in this generation.
    • All births and deaths take place at exactly the same time so that dying cells can help give birth to other cells but cannot prevent the deaths of others by reducing overcrowding. Similarly, cells about to be born can neither preserve nor kill cells living in the current generation. In order to follow this rule, one must determine all births and deaths before creating or destroying any cells.

    Write a program that will play the game of Life on a bounded grid. The program should allow the user to specify a starting pattern and should then continue to create new generations periodically until the user stops the action. The user should be able to control the time between generations. Java's Timer and TimerTask classes in the package java.util provide methods for controlling time between events.

        Despite the simplicity of the rules of the game, many quite interesting patterns and dynamics are possible. Some of the "creatures" composed of collections of living cells can blink, glide, and even produce offspring. For some of the possibilities, take a look at some of the many web sites devoted to this subject or see the book Wheels, Life and Other Mathematical Amusements by Martin Gardner.