CPSC 124, Fall 1998

Quiz Number 6


This is the sixth quiz given in CPSC 124: Introductory Programming, Fall 1998. See the information page for that course for more information.

The answers given here are sample answers that would receive full credit. However, they are not necessarily the only correct answers. In some cases, I give more detailed answers here than would be necessary to get full credit.


Question 1: The definition of an applet named MyApplet would begin with the line

class MyApplet extends Applet {

What is going on here? What is "Applet"? What is the word "extends" doing there?

Answer: Applet is the name of a class that is a standard part of Java. Here, MyApplet is a new class that is being defined as a subclass of Applet. The word "extends" is used to create subclasses in this way. The class MyApplet inherits everything that is in the class Applet. The rest of the definition of MyApplet will consist of additions and modifications to the stuff that is inherited from Applet.

Question 2: What is the function of a LayoutManager in Java?

Answer: A LayoutManager implements some policy for laying out all the visual components that have been added to a container, such as an applet or panel. That it, it sets the sizes and positions of the components. Different types of layout managers have different rules about how components are to be arranged.

Question 3: Explain carefully what the standard repaint() does.

Answer: The repaint() method is called to notify the system that the applet (or other component for which it is called) needs to be redrawn. It does not itself do any drawing (neither directly nor by calling the paint() routine). You should call repaint() when you have made some change to the state of the applet that requires its appearance to change. Sometime shortly after you call it, the system will call the paint() routine to redraw the contents of the applet.

Question 4: What is HTML?

Answer: HTML, or HyperText Markup Language, is a language that is used for writing Web pages. A HTML document contains all the text on a Web page, "marked up" with "tags" that determine how the text looks -- its size and color and how it is broken into paragraphs, for example. Other tags can include things like horizontal lines, images, links, and applets on the page. A Web browser acts as an interpreter for the HTML language.

Question 5: What picture will be produced by the following paint() method? Draw a picture and explain it briefly in words.

               public void paint(Graphics g) {
                  g.setColor(Color.black);
                  g.fillRect(0,0,150,50);
                  g.setColor(Color.red);
                  g.fillOval(0,0,50,50);
                  g.setColor(Color.yellow);
                  g.fillOval(0,50,50,50);
                  g.setColor(Color.green);
                  g.fillOval(0,100,50,50);
               }

Answer: The picture is shown on the left below. A black rectangle is drawn that is 150 pixels wide and 50 pixels high. Then three circles are drawn. Each circle has a diameter of 50 pixels -- exactly the same as the height of the rectangle. The circles vertically, one above the other, with the first circle inside the rectangle. The circles are red, yellow, and green. The circles just touch each other.

answer to question 5

Note: What I actually wanted to draw is shown on the right above. In this picture, the circles line up horizontally, and they all lie inside the black rectangle. The commands for producing this picture would be:

               public void paint(Graphics g) {
                  g.setColor(Color.black);
                  g.fillRect(0,0,150,50);
                  g.setColor(Color.red);
                  g.fillOval(0,0,50,50);
                  g.setColor(Color.yellow);
                  g.fillOval(50,0,50,50);
                  g.setColor(Color.green);
                  g.fillOval(100,0,50,50);
               }

David Eck, 29 October 1998