1.       Implement a superclass Shape and a subclass Circle which inherits from Shape. Details of the implementation are given in the comments in the program shell below. Fill in the blank lines with appropriate code, or leave them blank where appropriate.

 

 

 /**

A shape has a position, given by variables x, y which are double. (x, y) corresponds to the top left corner of the bounding box of the shape.

*/

public class Shape  ____________________________________________

{

            //INSTANCE VARIABLES

            private __double x ;__________

            private __double y ;__________

 

            /**

                        Constructs a shape with given coordinates.

                        @param x1 the x-coordinate

                        @param y1 the y-coordinate

            */

            public Shape (__double x1, double y1____________)

            {

              _____x = x1 ;______________________________

             

              _____y = y1________________________________

            }

            /**

Overrides the Object.toString method and produces a string representation of this shape in the following style:

                        Shape[x = 100.1, y = 50.23]

                        @return the string representation

            */

public String toString(________)

{

              __return “Shape[x= “ + x ____________________

 

              _________+ “, y = “ + y + “] “ ;______________

            }

            /**

                        Accessor for x.

                        @return the x coordinate

            */

            public _double  get X (________)

            {

               ____return x ;____________________________

 

            }

            /**



                        Accessor for y.

                        @return the y coordinate

            */

            public _double_ get Y (_______)

            {

              _____return y ; ___________________________

 

            }

}

 

/**

            Circle is a subclass of Shape. It has a radius

            */

            class Circle ___extends Shape__________________

            {

                        //INSTANCE VARIABLES

 

            private __double radius ;___

                       

            _______________________

            /**

                        Constructs a Circle object.

                        @param double x1 the x-coordinate of the top left corner

                        @param double y1 the y-coordinate of the top left corner

                        @param double radius1

            */

            public Circle (_double x1, double y1, double radius1___)

            {

 

            _____super (x1, y1) ; ___________________________

 

            _____radius = radius 1 ;_________________________

 

            _____________________________________________

            }

            /**

                        Override the Object.equals method so that two circles

                        are equal if they have the same radius.

                        @param Object the other circle

                        @return true if the circles have the same radius,

                                    otherwise return false. Also return false if

                                    the incoming object is not a Circle.

            */

            public _boolean_ equals (__Object_ other____________)

            {

              _____if (! other  instanceof  Circle) return false ; _____

           

              _____Circle c = (Circle) other ; __________________

 

              _____return radius == c.radius ;____ _____________

              _____________________________________________

            }

            /**

                        A method to draw the circle

                        @param g2 the graphics context

                        Draws an Ellipse 2D.Double object to represent the circle.

            */

            public _void_ draw ( Graphics2D g2 )

            {

               _____g2.draw (new Ellipse2D.Double(get X( ), getY( ),_

 

               _________2*radius, 2*radius)) ; __________________

 

               _____________________________________________

 

               _____________________________________________

 

            }

}

 

  1. Write a static method findMaximum which takes as a parameter an ArrayList. The method should assume that the ArrayList contains Comparable objects, and it should return the largest.

 

 

ANSWER:

 

 

 

public static Comparable findMaximum (ArrayList list)

 

            {           if (list.size( ) == 0) return null ;

                        Comparable largest = (Comparable) list.get (0) ;

                        for (int i = 1 ;  i <  list.size ( ) ;  i++)

                        {           Comparable c = (Comparable) list.get(i) ;

                                    if (largest.compareTo(c) < 0)

                                                largest = c ;

                        }

                        return largest ;

            }

 

 

 

 

 

 

 

 

 

3. Fill in the blanks of the following program. The driver class is not shown. You must write the instance variables an the listener methods.

 

 

/**

1.       Colorframe is a type of Jframe that has two panels,

Panel1 in the center and panel2 in the south of the content pane.

2.       The one in the SOUTH has a single buton.

3.       When the button is pressed, it starts or stops a timer, depending on whether the timer.isRunning() is false or true, respectively.

4.       When the timer is running, then on each tick it

a.       Causes a variable red to increase by 5, up to a maximum of 255

b.       Causes panel1to setBackground(new Color(red, 100, 10))

c.       Causes panel1 to repaint itself.

Thus, when you click the button, the center panel becomes more and more red, until either the maximum red is used or the button is clicked again.

*/

 

import java.awt.* ;

import java.awt.event.* ;

import javax.swing.* ;

 

 

 

public class ColorFrame __extends  JFrame__

{

            private __JPanel  panel1__ ;

           

            private __int  red________ ;

           

            private __Times  redTimer   ;

 

            public ColorFrame()

            {

                        red = 0 ;

 

                        panel1 =new JPanel() ;

                        panel1.setPreferredSize(new Dimension(300, 50)) ;

                        panel1.setBackground(new Color(red, 100, 100)) ;

                       

                        JPanel panel2 = new JPanel () ;

                        Jbutton b1 = new JButton (“red”) ;

                        panel2.add(b1) ;

                        b1.addActionListener(new RedListener()) ;

 

                        getContentPane().add(panel1, BorderLayout.CENTER) ;

                        getContentPane().add(panel2, BorderLayout.SOUTH) ;

 

                        redTimer = new Timer(100, new timerListener()) ;

 

}

 

class RedListener implements ActionListener

           

                        ublic void actionPerformed(ActionEvent e)

                       

                        ___if (redTimer.isRunning())___

 

                        _____redTimer.stop() ;________

 

                        _else redTimer.start() ; ________

}

           

class TimeListener implements ActionListener

           

                        ublic void actionPerformed(ActionEvent e)

                       

                        ___if (red < = 250)__________

 

                         _________red += 5 ;­­­­­­­________

 

                        _________panel1.setBackground (new Color(red, 100, 10)) ;

 

                         _________panel1.repaint() ;___

                       

}

 

}