RU beehive logo ITEC dept promo banner
ITEC 120
2007fall
ibarland,
jdymacek

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

hw09
assorted short-answer questions
due Oct.24

  1. (1pt) What Java keyword is used to create an instance of a class?                 
  2. (1pt) The relationship between a class and an instance is best described as:
    1. Instances contain classes
    2. Instances are the blueprint for creating classes
    3. Classes are the blueprint for creating instances
    4. Instances are behaviors(methods), while classes are data(fields)
    5. Classes are behaviors(methods), while instances are data(fields)
  3. (2pts) Suppose a class Student has been defined, and it includes the method with signature int getClassYear(). If s1 is a variable holding (a reference to) a Student, then which of the following would get s1's class year?
    1. getClassYear = s1()
    2. getClassYear(s1)
    3. s1.getClassYear()
    4. new Student() = s1.getClassYear
    5. (s1.getClassYear()).toString()
    6. s1 = getClassYear()
  4. (1pt) When checking whether two Strings have the same characters, use (choose one:) (a) == or (b) the equals method.
  5. (1pt) When checking whether two Dogs are exactly the same instance, use (choose one:) (a) == or (b) the equals method.
  6. The next sequence of problems refer to the provided classes PhoneNumber and ContactInfo.

     
    public class PhoneNumber {
    
      private String areaCode;
      private String exchange;
      private String extension;
    
      /** @return This PhoneNumber's area code.
        *   You may abbreviate this "gAC()"
        */
      public String getAreaCode()  { return this.areaCode; }
    
      /** @return This PhoneNumber's exchange.
        *   You may abbreviate this "gExch()"
        */
      public String getExchange()  { return this.exchange; }
    
      /** @return This PhoneNumber's extension
        *   You may abbreviate this "gExt()"
        */
      public String getExtension() { return this.extension; }
    
      /** Determine whether two PhoneNumbers represent the same phone.
        * @param                                                             
        * @return whether this PhoneNumber represents the same number as                 .
        */
                                equals(                                   ) {
    
    
    
    
    
    
    
    
      }
    
    }
    

  7. (4pts) Write a function equals() for class PhoneNumber; a few blanks have been provided to get you started. (Two phone numbers are considered equal if they have the same area code, exchange, and extension.)
  8. /** A class to organize the contact info for a person.  */
    public class ContactInfo {
    
     private String name;
     private PhoneNumber homeNumber;
     private PhoneNumber workNumber;
    
      /** @return the person's home phone number. */
      public PhoneNumber getHomeNumber() {  // You an abbreviate this "gHN()"
        return this.homeNumber; 
      }
    
      /** @return the person's work phone number. */
      public PhoneNumber getWorkNumber() {  // You can abbreviate this "gWN()".
        return this.workNumber; 
      }
    
      /** Are `this' and `other' ContactInfos for two co-workers?
       * @param other The other contactInfo to compare with this one.
       * @return whether `other' is a co-worker of `this'.
       */
      public boolean isCoworkerOf( ContactInfo other ) {
    
      }
    } 
    
  9. (4pts) Write a function isCoworkerOf( ContactInfo other ) for class ContactInfo. Two ContactInfos are considered co-workers if their work phones are equal.
    You may assume that the PhoneNumber method equals() has been tested and verified (even if you haven't yet completed the previous problem).
  10. (4pts) Note that PhoneNumber's field areaCode is declared private.
    1. T/F: Inside a method of ContactInfo, it is legal to write homeNumber (bypassing its getter).
    2. T/F: Inside a method of ContactInfo, it is legal to write getHomeNumber().exchange (bypassing the getter getExchange()).
    3. T/F: Inside a method of PhoneNumber, it is legal to access the field homeNumber (bypassing its getter).
    4. T/F: Inside a method of PhoneNumber, it is legal to access the field exchange (bypassing its getter).
  11. (1pt) When using a class written by somebody else, you don't care about its implementation, only its interface -- that is, the signature of all its                                                                                   (two words -- one adjective, one noun).
  12. (3pts) Draw a picture of what things look like after executing the first three lines below.
    ContactInfo ci1 = new ContactInfo( /* ... details omitted ... */ );
    ContactInfo ci2 = new ContactInfo( /* ... details omitted ... */ );
    ContactInfo ci3 = ci1;
    
  13. (3pts)
    1. How many variables are declared in the previous problem?
    2. How many ContactInfos are created?
    3. Answer each of the true/false questions:
      Assume that the method setName works as expected. You are not told what the code for swapWith does, exactly; what it does cannot change the correct answer.
      ci1.getName() == ci2.getName()  // true or false?: 
      ci1 == ci2;   // true or false?: 
      ci1 == ci3;   // true or false?: 
      ci3.setName( "Bobo" );
      ci1 == ci3;   // true or false?: 
      ci1.swapWith(ci2);  // Code not shown -- it might do anything
      ci1 == ci3;   // true or false?: 
      ci2 == ci3;   // true or false?: 
      
  14. (3pts)
    1. What is the syntax for calling a (regular, non-static) method in Java?                                                                                                                                                                 
    2. What is the syntax for calling a static method in Java?                                                                                                                                                                 
    3. When should you make a method static?                                                                                                                                                                 
    4. What keyword/variable cannot be used inside a static method?                 
  15. (7pts) Every new Blopper contains a two brand-new Chorbles, one Doooozi (which must be specified separately for each Blopper), and the name of their favorite artist (which is initially "M.C. Hammer").

    1. Draw a picture of a Blopper object.
      (When drawing an object whose internals you know nothing about, just draw a (large) empty box.)
    2. Write a class (fields only) to represent a Blopper.
    3. Write a constructor for this class.
      You are told, on good authority, that creating a new Chorble needs no further information.
    4. Now imagine you are sitting at the code pad1
      Declare a local variable which can hold a Blopper.
      (How do you declare a local variable? It requires two words and a semicolon, and nothing more.)
    5. Suppose you have a Doooozi stored in a variable d. (a) Create a new Blopper, and (b) remember it for future use, storing it in the variable you declared in the previous step.
    Hint: remember step 1 of The Design Recipe: Determine how to represent your information. What Java data type is best used to represent the name of an artist? What data type best represents all the information associated with an entire Chorble (whatever that is)?
    These are all part of the question of how to represent an entire Blopper (which is comprises several pieces of information, so we use a class with several fields).

    (If it makes you any happier, you can imagine replacing “Blopper” with “Kennel”, “Chorble” with “EmCee”, and “Doooozi” with “Dog”; the essence of the problem would be the same.)


1Or more realistically, that you are writing code which is inside an entirely different class/method.      

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2007, Ian Barland, Radford University
Last modified 2007.Oct.24 (Wed)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme