Midterm, CS 110, Fall 2004

Correct answers appear in italics

To help with your time allocation, the test is composed of three sections with the following values:
Section Value
Short Answer 25 percent
Analysis 35 percent
Programming 40 percent
Point values for each question are in parens prior to the start of the question. In the programming section, if you cannot solve the entire problem be sure to do what you do know how to do. E.g, at least produce the correct layout of the widgets. Also in the programming section, partial credit will be given for a good description of what you would do, if you simply cannot figure out how to write the code. Finally, syntax errors are not important in the programming section. I am much more interested the core functionality of the program.

Short Answer

  1. (2) The acronym HTML means: Hyper-Text Markup Language
  2. (2) When you execute javac Navigate.java and there are no syntax errors, a new file will be created. The name of the new file is: Navigate.class.
  3. (2) How many classes can a Java class extend? exactly 1
    How many classes can a java class implement? 0, 1, 2 or more, limited only by the number of existing interfaces
  4. (2) Java programs that run within an HTML page are called applets .
  5. (4) Put, into the table below, the final value of each of the variables in the following syntatically correct code fragment.
    int a = (int)10;
    float b = (float)10;
    int c = (int) (a / 3);
    float d = (float) (a / 3);
    boolean e = ( d > 2 );
    a=5;
    
    a  10
    b  10.0f
    c  3
    d  3.0
    e  true
  6. (7) A URL has up to 6 parts, one of which is the protocol. Write a URL with at least 4 parts (including the protocol) then briefly describe the function of each part. I have already filled in the answer for the protocol.
    URL:  http://a.b.com:80/aaa.html?query=7#part2
    partpart nameDescription
    1 http://Protocoldefines how the information is going to be retrieved from the serving computer.
    2 a.b.com host -- note that this might be broken down into top-level-domain, domain, and host The machine on which the information resides
    3 aaa.html path the location, on the host, of the information
    4 ?query=7The queryFurther info about the page desired. Most often associated with dynamic content
  7. (6) The following rather boring program would work, but for syntax errors on 7 of its lines. For full credit, find and fix 5 of the errors.
    import java.awt.*;
    import java.applet.Applet;
    public A extends Applet 
    {
        public void paint(missing Graphics g)
        {
    	Missing g.drawLine(10,10, 40, 40);
    	int a=5.0; Should not have .0
    	Missing int b = 10;
            if a>b  Missing parens...should be (a>b)
    	    {
    		g.drawString("A is greater than B", 20, 20);
    	    }
            ekse  Misspelling else
            {
                g.drawString("B is greater than A", 20,20)Missing ;
            }
        }
    }
    

Analysis

  1. (10) A wise man once said "Programs should be written to be read by humans and only incidently to be executed by machines". Describe at least two conventions of Java programming and how those conventions support this wisdom.
    1. Capitalization: class names always start with an initial capital letter. Method and variable names always start with an initial lower case letter. Allows readers to easily determine what is what.
    2. Top level comments -- Java classes start with top level comments surrounded by /** ... **/. This conventionis bolstered by "javadoc"
    3. Indenting. Programs are easier to read when they are properly indented.
    4. Meaningful variable names: not necessary, but really helpful.
  2. (8) What is the final value of the variables m and n in the following program?
      int m = 7;
      int n = 0;
      for (int i=0; i<4; i = i + 1)
      {
         m = (m * (4*n)) + i;
         n = n + 1;
      }
    
    Showing the work leading to the answer:
    var name initial values after 1 pass after 2 passes after 3 passes after 4 passes
    i 0 1 2 3 4
    m 7 0 1 10 123
    n 0 1 2 3 4
    So, the answer is:
    m123
    n4
    spacer(2)
  3. (8) Circle the picture below that is most like what the following applets draws.
    import java.awt.*;
    import java.applet.Applet;
    public class A extends Applet
    {
        public void paint(Graphics g)
        {
    	int stx=100;
    	int sty=100;
    	int d = 50;
    	int sp = 33;
    	for (int i=0; i<3; i++)
    	    {
    		g.drawOval(stx, sty, d, d);
                    stx = stx + sp;
                }
            stx=117;
    	sty=67;
    	for (int i=0; i<2; i++)
    	    {
    		g.drawOval(stx, sty, d, d);
                    stx = stx + sp;
                }
        }
    }
    
    The rightmost picture is correct
  4. (9) Suppose that the following method is called whenever a button is pushed. For each variable, circle in the table below the most accurate description of the value of that variable.
    boolean result;
    public void mystery()
    {
       int r1 = (int)(Math.random()*10);
       int r2 = (int)(Math.random()*10);
       int r3 = r1 - ((r1/2)*2);
       int r4 = r2 - ((r2/2)*2);
       result = (r3==r4);
    }
    
    r1 A) an integer between 0 and 9
    r2 A) an integer between 0 and 9
    r3 A) 1 if r1 is odd B) 1 if r1 is even C) Both D) Neither
    r4 A) 1 if r2 is odd B) 1 if r2 is even C) Both D) Neither
    result A) true if r1 and r2 are both odd B) true if r1 and r2 are both even C) Both D) Neither

Programming in Java

  1. (20) Halloween is coming, and so are ghosts! Write a complete program that draws a ghost that looks like the one at left by putting a filled circle on top of a filled rectangle. The ghost should also have two eyes in its head. Make the ghost appear or disappear by adding a button labeled labeled "(in)visible". When you hit that button the ghost should appear if it was invisible and disappear if it was visible. The eyes should always remain visible. Point values: 10 points for the ghost, 4 points for the layout and connecting the button to the applet, 4 points for making the ghost appear and disappear, 3 points for keeping the eyes visible.

    Solution starts here
    import java.awt.*;
    import java.applet.Applet;
    import java.awt.event.*;
    
    /**
     * Author: Me
     * File: Ghost.java (displayed in midterm.html)
     * Purpose: Answer to midterm question
     * Description: draw a small ghost in the form or a circle sitting on top of
     * a rectangle.  The ghost has two eyes (above the rectangle).  When hit a 
     * button, the ghost goes invisible, but the eyes always remain visible.
     **/
    public class Ghost extends Applet implements ActionListener
    {
        /**
         * The invisibility button
         **/
        Button invis;
        
        /**
         * If true, then the ghost will be visible
         **/
        boolean vis=true;
    
        /**
         * Respond to button pushes.  Just changes the state of vis.
         **/
        public void actionPerformed(ActionEvent ae)
        {
    	if (vis) 
    	    vis=false;
    	else
    	    vis=true;
    	repaint();
        }
    
        /**
         * Set up the interface.  Mostly thisis just putting in the invis button
         **/
        public void init()
        {
    	setLayout(new GridLayout(6,1));
    	invis = new Button("(in)Visible");
    	add(invis);
    	invis.addActionListener(this);
    	
        }
    
        /**
         * Do the actual work of drawing the ghost (or not)
         **/
        public void paint(Graphics g)
        {
    	if (vis)
    	    {
    		// the ghost
    		g.setColor(Color.green);
    		g.fillRect(10,115,30,45);
    		g.fillOval(10, 100, 30, 30);
    	    }
    	// the eyes
    	g.setColor(Color.black);
    	g.fillOval(20, 108, 4, 3);
    	g.fillOval(26, 108, 4, 3);
        }
    }
    
  2. (20) Write a program that writes a number, sarting in the upper left corner of the display and going down and to the right until it reaches the botton, or left side, of the display. If at the left side, just stop. If at the bottom, continue moving to the left but start going up. Bounce off the top or bottom until reaching the left wall. The image at left shows what a the program might look like after 10 button hits. The button controls when the screen is redrawn. Steps in the X direction should be fixed in size at 20. Steps in the Y direction should be fixed at 55. The number drawn should be the number of times the button has been hit. Partial credit will be as follows: the interface and attaching it correctly to the program, 5 points; printing the number of button hits (instead of just some number), 5 points; the number just going down and to the right after a button hit; 5 points; the number able to "bounce" off the bottom and top of the screen, 5 points.
    Recall that to get the size of an applet you can put the following lines into your paint method:
    int appX = (int) getSize().getWidth();
    int appY = (int) getSize().getHeight();

    Solution starts here

    import java.awt.*;
    import java.applet.Applet;
    import java.awt.event.*;
    /**
     * AUTHOR: me
     * File: Angles.java displayed in midterm.html
     * Purpose: question answer from midterm
     * Description: displays the button count going diagonally down the applet.  
     *    If the number reaches the bottom, bounce and start going up.  Then at
     *    top go back down, etc until you reach the right side of the applet.
     *    Control the angle of descent with a scrollbar.
     **/
    public class Angles extends Applet implements ActionListener
    {
        /**
         * the number of button hits
         **/
        int bc=0;
        /**
         * the button.  Redraw everything when hit.
         **/
        Button b;
        /**
         * The scrollbar that controls the angle of descent of the button count
         * on the applet.
         **/
        Scrollbar s;
        /**
         * Respond to a button hit.  Just increment the button counter and repaint
         **/
        public void actionPerformed(ActionEvent ae)
        {
    	bc = bc + 1;
    	repaint();
        }
       
        /**
         * Set up the layout
         **/
        public void init()
        {
    	setLayout(new GridLayout(10, 1));
    	s = new Scrollbar(Scrollbar.HORIZONTAL, 20,1,20,50);
    	add(s);
    	b = new Button("Do it!");
    	add(b);
    	b.addActionListener(this);
        }
        
        /**
         * Do the drawing
         **/
        public void paint(Graphics g)
        {
    	int ymin = (int)(getSize().getHeight()*2/10) + 10; // this is the extra credit portion. Go down in the applet 2/10 of the way before starting to draw because my grid layout has 10 rows and I used 2 of them. The go down another 10 to leave space for drawing characters.
    	int x = 0;
    	int y = ymin;
    	int xinc = 20;
    	int yinc = s.getValue();
    	int direction = 1;  // controls whether number are going up (negative) or down (positive).
    	while (x < getSize().getWidth())
    	    {
    		g.drawString(""+bc, x, y);
    		x = x + xinc;
    		y = y + direction*yinc;
    		if (y>getSize().getHeight())
    		    {
                            // below the bottom
    			y = (int)getSize().getHeight();
    			direction=-1;
    		    }
    		if (y < ymin)
    		    {
                            // above the top
    			y = ymin;
    			direction = 1;
    		    }
    	    }
        }
    }