Ryerson University: School of Continuing Education

CCPS209, Midterm Test 1, Spring 2004


Last Name                                 
First Name                                
Student Number                                 


Instructions
Question Out of Mark
1 4          
2 3          
3 4          
4 4          
Total 15          
  1. Fill in the following applet so that it draws n figures similar to the one shown below, at random locations, where n is a value obtained from the user. The labels in the rectangles are also obtained from the user, i.e., "Shape" and "Rectangle" are just examples. The figure is specified in the Figure class.
    /**
       An applet to draw n figures at random locations and with
       random colors, where n is an integer obtained from the user.
     */
    import java.applet.Applet ;
    import java.awt.Graphics ;
    import java.awt.Color ;
    import java.awt.Graphics2D ;
    import java.awt.geom.Ellipse2D ;
    import java.awt.geom.Rectangle2D ;
    import java.awt.geom.Line2D ;
    import javax.swing.JOptionPane ;
    import java.util.Random ;
    
    public class Q1Applet extends Applet
    {
        //INSTANCE VARIABLES
        private String label1, label2 ;
        private int n ;
        private Random random = new Random() ;
    
        /**
           Constructs the applet and gets information from 
           the user.  Use JOptionPane.showInputDialog to get
           the two labels and the number n.
         */
        public Q1Applet()
        {
            label1 = JOptionPanelshowInputDialog("label1 = ") ;
            label2 = JOptionPanelshowInputDialog("label2 = ") ;
            n = Integer.parseInt(JOptionPanelshowInputDialog("n = ")) ;
        }
        /**
           Paints n figures of the specified type
           @param g the graphics context
         */
        public void paint(Graphics g)
        {
            Graphics2D g2 = (Graphics2D) g ;
    
            //get the current size of the applet window
            int W = getWidth() ;
            int H = getHeight() ;
    
            //draw the randomly placed figures within the window
    
            for (int i = 0 ; i < n ; i++)
            {
               int x = random.nextInt(W) ; 
               int y = random.nextInt(H) ; 
               Figure figure = new Figure(x, y, label1, label2) ;
               figure.draw(g2) ;
            }
        }
    }
    
    /**
       A figure that can draw itself at a given location.
       An example of the figure is shown above.
     */
    
    class Figure        
    {
        //INSTANCE VARIABLES for top left corner of the bounding box
        //    and the labels that appear in the rectangles
    
        private double x, y ;
        private String label1, label2 ;
    
        // The following constants give the width W, height H of the 
        // rectangles, and the length L of the line connecting the rectangles
    
        private static final int W = 100, H = 40, L = 80 ;
    
        /**
           Constructs the data of the figure.
           @param x1 the x coordinate of the top left corner of the bounding box
           @param y1 the y coordinate of the top left corner
           @param lab1 the first label
           @param lab2 the second label
         */
        public Figure(double x1, double y1, String lab1, String lab2)
        {
            x = x1 ; 
            y = y1 ; 
            label1 = lab1 ; 
            label2 = lab2 ; 
        }
        /**
           Draws the figure given the context.
           @param g2 the graphics context
         */
        public void draw(Graphics2D g2)
        {
            g2.draw(new Rectangle(x, y, W, H)) ;
    
            g2.draw(new Rectangle(x, y + H + L, W, H)) ;
    
            g2.draw(new Line2D.Double(x + W/2, y + H, x + W/2, y + H + L)) ;
    
            g2.drawString(label1, x + W/4, y + H/2) ;
    
            g2.drawString(label2, x + W/4, y + H + L + H/2) ;
        }
    }
    
    
    
    
  2. Short answer questions.
    What is an immutable class?
    
    An immutable class is a class that has no mutator methods.
    
    Why in general is printing a bad side effect in a method.
    
    Calling System.out.println() in your method unnecessarily 
    couples your class to System and PrintStream, and whatever language
    (e.g., English) you use may not be appropriate for the users.  Also,
    a class containing such print statements cannot be used as embedded software.
    
    
    Write a reasonable precondition for the method StringTokenizer.nextToken()
    
    (Precondition: hasMoreTokens() must be true.)
    
    After writing the precondition, according to Horstmann there are 
    two good ways to deal with the precondition inside the method.  What
    are they?
    
    1)Test for the precondition and throw an exception if it is not
       satisfied.
    
    2)Do not test for the precodition...just assume it is satisfied.
    
    What is wrong with the following method which is designed so that setToMaxInt(x) ; will set variable x in the calling program to the largest integer value?
        public static void setToMaxInt(int x) { x = Integer.MAX_VALUE ; } 
    
        Java uses call by value, so changing the parameter 
         value in the method has no effect outside the method.
    
  3. Write a compareTo method for a class Acid. Assume that you want to sort Acid objects from lowest pH to highest pH. Assume that there is a double instance variable pH.
    public int compareTo(Object other)
    {
        Acid acid = (Acid) other ;
    
        if (pH < acid.pH) return - 1 ;
        if (pH > acid.pH) return + 1 ;
        return 0 ;
    }
    
    Write a static method which takes three Comparable objects as parameters and returns the ``middle one'', i.e., one which is not largest or smallest.
    
    public static Comparable middle(Comparable a, Comparable b, 
    Comparable c)
    {
        if (a.compareTo(b) >= 0 && a.compareTo(c) <= 0) return a ; 
        if (a.compareTo(c) >= 0 && a.compareTo(b) <= 0) return a ; 
        if (b.compareTo(a) >= 0 && b.compareTo(c) <= 0) return b ; 
        if (b.compareTo(c) >= 0 && b.compareTo(a) <= 0) return b ; 
        return c ;
    }
    
  4. Write a static method which takes as parameters the lengths of the sides of a triangle and computes the area of the triangle using Heron's formula: Area = sqrt(s(s-a)(s-b)(s-c)) , where s represents half the perimeter of the triangle. Assume all types are double.
    public static double triangleArea(double a, double b, double c)
    {
        double s = (a + b + c) / 2.0 ;
        return Math.sqrt(s * (s - a) * (s - b) * (s - c)) ;
    }