1.                  Fill in the following applet so that it draws n sail boats at random locations...

 

 

class SailBoat _____________
{

            //CONSTANTS

            private static final int SIDE = 60 ;

            /**

Instance variables to specify position  of the top left corner of the bounding box of the sial boat and the color of the sail.

            */

                        private int x, y ;                                                                                 

                        private Color color ;                             

                        _______________________________                                             

            /**

                        Constructs the data of the sailBoat.

 

            ...

 

 

            public void draw(Graphics2D g2)

            { 

                        //Draw the sail

                        g2.setColor(color);

                        g2.draw (new Rectangle (x + SIDE / 3, y, SIDE / 3, SIDE / 3)) ;      

                             

                        //Draw the mast (the vertical line connecting sail and boat)

                        g2.setColor(Color.black) ; // optional_____________________     

                        g2.draw(new Line2D.Double(x + SIDE/2, y + SIDE/3, x + SIDE / 2, y + 2 * SIDE / 3)) ;       

                        ___________________________________________________     

                        //Draw the deck (the horizontal line connecting ends of the arc)

                        g2.draw (new Line2D.Double (x, y + 2*SIDE/3, x + SIDE, y + 2 * SIDE / 3     

                        ____________________________________________________   

                        //Draw the bottom of the boat

                        g2.setColor(Color.white) ;

                        g2.fillArc(x, y + SIDE / 3, SIDE, 2 * SIDE / 3, 180, 180) ;

            }

}

 

/**

 

 

...

 

 

 

{

                        //Use JoptionPane’s showImputDialog method to get n

            String input = JoptionPane.showInputDialog (“n?”) ;                    

            n = Integer.parseInt(input) ; ________________                            

            _______________________________________                             

            random = new Random() ;

}

/**

 

 

...

 

 

//draw the randomly placed sailBoats (with proper color sails)

                                                                                                                       

            for (int i = 0 ; i < n ; i++)                                                                 

            {                                                                                                          

                        int x = random.nextInt (W) ;                                                 

                        int y = random.nextInt (H) ;                                                  

                        SailBoat boat;_                                                                       

                        if    (x < W / 2) __                                                                     

                        {                                                                                              

                                    boat = new SailBoat(x, y, Color.yellow) ; ___           

                        }

                        else    {          

                                    boat = new SailBoat(x, y, Color.red) ; ___           

                        }                                                                        ___                 

                        boat.draw (g2)_________________________                     

            }_____________________________________                                

                       

            }

}

 

2.                  Explain in a couple of lines the term precondition:

 

Precondition is a boolean condition which if satisfied by the caller of a method, then the method will do what it says it will do.

 

Give a precondition for the following method of String:

String substring(int start, int pastEnd)

 

// Precondition start > = 0 and pastEnd < = length of string

 

            Give five purposes of an operating system.

 

1.      manage interaction with user                                                      

2.      manage files                                                                                 

3.      manage I/O devices                                                                      

4.      manage time slicing for tasks                                                      

5.      manage time slicing for users                                                      

 

 

3.                  Consider a class Friend which has two instance variables int height and Color eyeColor. Write a compareTo method for Friend so that it realizes the Comparable interface. Assume that you want to sort friends based on their height (shortest first).

 

public int compareTo(Object other)

 

{

            Friend f = (Friend) other ;                                                    

                                                                                                           

            return height – f.height ;                                                        

 

}

 

 

Rewrite the method assuming you want to sort friends based on eye color such that brown-eyed friends come first and other colors come after brown.

 

public int compareTo(Object other)

            {

                        Friend f = (Friend) other ;                                                    

                                                                                                                       

                        if (eyeColor == Color.brown &&                                         

                                                f.eyeColor ! = Color.brown) return –1          

                        if (f.eyeColor == Color.brown&&                                        

                                                eyeColor ! – Color.brown) return 1 ;

                                                                                                                       

                        return 0 ;                                                                                

            }

 

 

4.                  Implement a superclass Employee. Make a class Manager...

 

 

/**

            An employee has a name and a salary.

            */

            public class Employee __________________________________

            {

                        //INSTANCE VARIABLES

                        private String name ;_

                        private double salary ;

                       

                        /**

                                    Constructs an employee with given name and salary

                                    @param name1 the name

                                    @param salary1 the salary

                        */

                        public Employee ( String name1, double salary 1_)

                        {

                                    name = name 1 ;                                            

                                    salary = salary 1 ;                                          

                        }

                        /**

                                    Produces a string representation of this  employee...

                        */

                        public String toString()

                        {

                                    String result = “Employee [name = “ + name

                                    +”, salary = “ + salary +” ]” ;                    

                                    return result ;                                                 

                        }

            }

 

/**

            A manager is a special employee that manages a team working on a project.

 */

class Manager __extends Employee_____

{

            //INSTANCE VARIABLES

            private String project__

            private ArrayList list ;

           

            /*

                        Constructs a Manager object.

 

                        ...

           

            */

public Manager ( String name1, double salary1, String project1, ArrayList list1)

{

            super(name1, salary1) ;                                                                     

            project = project1 ;                                                                           

            list = list1 ;                                                                                         

 

}

/**

                        Returns true if the other manager ...

 

*/

 

public boolean equals(Object other)

{

            Manager her = (Manager) other ;                                          

                                                                                                           

            return list.size) == her.list.size ( );                                       

 

}

 

 

5.                  Write a program which generates 1000 integers with values from 0 to 99...

 

....

 

            public class CountTest

            {

                        public static void main (String [ ] args)

                        {

                                    Random random = new Random ( ) ;

                        int [ ] count = new int [100] ;

                                    for (int i = 0 ; i < 1000 ; i++)

                                    {

                                    int n = random.nextInt (100) ;

                                    count [n]++ ;

                                    }

                                    for (int i = 0 ; i < 100 ; i++)

                        {

                                                System.out.print (i+ “    ”) ;

                                    for (int j = 0 ; j< count [i] ; j++)

                                                {

                                                            System.out.print (“*”) ;

                        }

                                                System.out.println ( ) ;

                                    }

                        }

}

 

6.                  Fill in the blanks of the following program which every INTERVAL ...

 

 

...

 

 

                                                                                               

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

{                                                                                              

            Rectangle rect = (Rectangle) list.get (i) ;                   

            g2.draw (rect) ;                                                          

}                                                                                              

 

...

 

{

 

    Rectangle rect = new Rectangle (                                          

            ranom.nextInt(PANEL.W),                                        

            random.nextInt(PANEL.H), RECT.W, RECT.H) ;    

    list.add (rect) ;                                                                       

    repaint ( ) ;                                                                             

                                                                                               

}

 

...

 

{

    Point2D.Double point = event.getPoint( ) ;                          

                                                                                               

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

    {                                                                                              

            Rectangle rect = (Rectangle) list.get (i) ;                   

                                                                                               

            if (rect.contains (point))                                             

            {                                                                                  

                        list.remove (i) ;                                               

                                                                                               

                        if (list.size ( ) == 0) timer.stop ( ) ;                

                        repaint ( ) ;                                                     

                       break ;                                                                                 

            }                                                                                  

    }                                                                                              

 

...