Ryerson University
School of Computer Science
CCPS209 Term Test #1
Fall 2005

Name:____________________________________ Student #: ______________________

There is no penalty (i.e., deduction) for incorrect answers, so you are encouraged to make a guess if you do not know the answer.

A For each of the following pairs (a, b) indicate whether the relationship is typically considered (a) a = superclass and b = subclass (b) a = subclass and b = superclass (c) neither, a and b are not related in this way.

  1. body, leg
  2. cloud, sky
  3. galaxy, star
  4. candy, lollipop
  5. sugar, cake
  6. tape, masking tape
  7. girl, boy
B. Assume that
A is an abstract class.
B is a concrete subclass of A, and B implements interfaces I and J.
C is a concrete subclass of A, and C implements interface J.
B1 is a subclass of B.
All of the above classes have default constructors.
A a ; B b ; C c ; I i ; J j ; // assume these declarations
For each of the following blocks (considered in isolation) indicate if the block
(a) will not compile
(b) will compile but WILL cause an exception (run time error)
(c) will compile but MIGHT cause an exception at run time.
(d) is OK
  1. {a = new A() ; }
  2. {a = new B() ; }
  3. {a = new I() ; }
  4. {a = new B1() ; }
  5. {i = new B() ; }
  6. {i = new C() ; }
  7. {i = new B1() ; }
  8. {j = new B() ; }
  9. {j = new C() ; }
  10. {j = new B1() ; }
  11. {a = new B() ; b = a ; c = (C)a ; }
  12. {j = new C() ; a = (A)j ;}
  13. {j = new C() ; i = (I)j ;}










C.
  1. Consider the following problem: Make a class Machine and a class Robot, where Machine is a superclass of Robot, and Robot implements the interface Programmable. A Machine has a serial number (int) and a parts list (ArrayList). A Robot has a a program (String). The interface Programmable has one method void setProgram(String). Give the Machine class a toString method. Give both Machine and the Robot class an equals method. These methods override the corresponding methods in the Object class. Make Robot implement the Comparable interface so that robots can be sorted from shortest program to longest. The following code is an attempt to solve this problem, but there are some blanks (each worth one point). If the blank should have code, fill it in, otherwise leave it empty.
    
    import java.util.ArrayList ;
    
    public class Machine ______________________
    {
        private int serialNumber ;
        private ArrayList list ;
    
        /**
           Default constructor to initialize instance variables.
        */
        public Machine()
        {
           serialNumber = 0 ;
           list = ___________________ ;
        }
    
        /**
          Constructor to initialize serialNumber to a given value
          and also the list.
          @param serialNo   the given serial number 
        */
        public Machine(int serialNo)
        {
             __________________________ ;       
    
             __________________________ ;       
        }    
    
        /**
          Returns a string representation in the form
          (Recall that ArrayList has a good toString method).
          Machine[serialNumber = 54321, parts = [side panel, button, actuator]]
          @return string represenation of the machine object
        */
        public String toString()
        {
             ____________________________________________________ ;
    
        }    
    
        /**
            Method for comparing equality of two machines based on serial number,
            and parts list.  Don't worry about null or class name.
            @param other    the other machine to compare with this one
    	@return true if the machines have the same number and parts.
        */
        public boolean equals(Object other)
        {
           ___________________________________________
    
           ___________________________________________
    
        }
    }
    
    
    
    
    
    
    
    
    public class Robot _____________________________________________
    {
        private String program ;
    
        /**
            Constructor which supplies the program and the serial number
    	@param serialNo  the given serial number
            @param program1  the given program
        */
        public Robot(int serialNo, String program1)
        {   
            _____________________________________
       
            _____________________________________
        }
    
        /**
            Method for comparing two robots based on program length.
            @param other    the other robot to compare with this one
        */
        public _____ compareTo(Object other)
        {
           ___________________________________________
    
           ___________________________________________
    
        }
    
        /**
            Method for comparing equality of two robots based on serial number,
            program and parts list.
            @param other    the other robot to compare with this one
    	@return true if the robots have the same number, program and parts,
                      false otherwise
        */
        public boolean equals(Object other)
        {
           ___________________________________________
    
           ___________________________________________
    
           ___________________________________________
    
        }
    
        /**
            One more method as required so that Robot won't be abstract.
    
        */
        public void ___________________________________
        {
           ___________________________________________
    
        }
    
    }
    
    public interface Programmable
    {
        void setProgram(String program1) ;
    }