Chris Pollett>Old Classes>PIC 20, Winter 2000>Practice Final

Winter 2000 PIC 20 Practice Final

The final cover from Chapter 9 page 427 on interfaces through Chapter 16 on animation (page 794). You are not respsonsible for Chapter 10 on Strings, nor are you responsible for pages 661--687 in Chapter 13.

  1. What is the painters algorithm? Give a brief example.
  2. Suppose the following nodes are stored in the order given below
                  1*                  2*
                         3*
    		  4*
    
    
    Explain how the Jarvis' Gift-Wrapping algorithm would compute the convex hull of this figure.
  3. Give a brief example of an interface that includes both the interface definition and an example class that implements it.
  4. Explain the following terms with example: (a) anonymous object, (b) anonymous inner class, (c) Adapter class (d) synchronized methods
  5. Write a short class that extends JFrame which opens a window and draws in yellow, monospaced, 12 point, the words "hi there" and then draws in the color with RGB coordinates (10,20,30) a rounded rectangle of width 20 and height 30.
  6. Explain with code how to write a program both as an application and as an applet.
  7. Write a class that extends JPanel with a JLabel and JComboBox on it. The JComboBox should allow you to select between the options: 1,2,3,4. When an option is selected that number is displayed in the JLabel. Use GridLayout to layout your components.
  8. Distinguish between the following in Java: Error, Exception, and RunTimeException. What class do they all inherit from? What is the purpose of a finally block? How does one declare that a method might generates a certain kind of exception? Say, for instance, that it might generate a MyException.


    The next three problems I didn't get to go over the solution in class. So I'm putting the solution on the web.

  9. Distinguish between premptive threads versus time-slicing threads. What is a monitor? How can one use threading with a class that already extends some class other than Thread?
    ANS:
    In preemptive threading if a thread of higher priority than the currently
    running thread becomes ready then the scheduler will preempt the currently
    running thread and execute the thread of higher priority. In time-slicing,
    each ready thread gets a slice out of some fixed amount of time in
    which the scheduler will let it run. This slice of time is larger for threads
    of higher priority. A monitor is part of the scheduling program that ensures
    that only one thread can execute a synchronized method at a time.
    
    To make a class that uses threads without extending threads one can use
    the Runnable interface. i.e.,
    
    
    
    
    class MyClass extends SomeOtherClass implements Runnable
    {
    	public MyClass()
    	{
    		Thread t = new Thread(this);
    	}
    
    	public void run()
    	{ //whatever want in run method
    	}
    }
    
    
  10. Give the syntax for loading a sound file in (a) an applet, (b) an application. Give the syntax for loading a .jpeg file into a new ImageIcon.
    In the init method of an applet one could call
    
    AudioClip au = getAudioClip( getDocumentBase(),"filename");
    
    For a application in its constructor you could do:
    
    try{
    AudioClip au = Applet.newAudioClip(new URL( "http://whatever"));
    }
    catch (MalformedURLException e)
    {
    // handle it
    }
    
    The syntax for loading a .jpg (in same directory as application) on a
    not as yet constructed ImageIcon is:
    
    ImageIcon im = new ImageIcon("filename.jpg");
    
  11. Use a Timer to write a short applet that draws on itself a new letter of the alphabet every 500 ms, starting with the letter A and cycling through the letters in sequence. (At Z it should start over at A.)
    ANS:
    public class MyApplet extends JApplet
    {
    	String abc[] = "abcdefghijklmnopqrstuvwxyz";
    	int i=0;
    	Container c;
    
    	public void init()
    	{
    		c= getContentPane();
    
    		Timer t = new Timer(500, new ActionListener(){
    			public void actionPerformed(ActionEvent e)
    			{
    				i = (i+1)%26;
    				c.repaint();
    			}
    		});
    	}
    
    	public void paint(Graphics g)
    	{
    		g.drawString(""+abc.charAt(i), 10,10);
    	}
    }