RU beehive logo ITEC dept promo banner
ITEC 120
2008spring
ibarland,
jdymacek

homeinfoarchiveexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

hw08
Unpredictable EmCees
mult. choice; non/static methods

Part (a): Short answer

Due Mar.21 (Fri), also accepted Mar.24 (Mon) in class.
  1. (1pt) To test whether two String (reference)s s1,s2 have the same characters, use s1                s2
  2. (1pt) To test whether two PizzaServer (reference)s p1, p2 refer to identically the same object, write p1                p2
  3. (2pts) It's a well-known children's fact that:
    There's a flea on the wing on the fly
    On the frog on the bump on the log
    In the hole in the middle of the sea
    Q: If all we have is a variable sea, what is an expression to get the flea's name?
    sea.getMiddle().getHole().                                                                                                                        
    Assume we can access sea's middle by the method getMiddle(). Once we have a middle, we can access the middle's hole by the method getHole(), etc.. (To save writing, you may abbreviate “get” with “g” -- e.g., sea.gMiddle().gHole().….)
  4. (Extra credit, 2pts) The previous problem doesn't need to be one huge expression; we can use local variables to break it into smaller, bite-sized1 computations. However, doing this means we need to mention the type of each local variable. It turns out: Complete the following code snippet, so that the flea's name ends up stored in a local variable.
          Ocean sea;
          sea = /* We assume sea has already been initialized by somebody else... */
    
          Region m;                 // Declare m.
          m = sea.getMiddle();      // Initialize m.
    
          Anomoly h = m.getHole();  // Declare and initialize h -- two steps in one.
    
          // YOUR CODE HERE
         
        
  5. (1pt) The relationship between a class and an instance is best described as:
    1. Classes are behaviors(methods), while instances are data(fields)
    2. Instances are behaviors(methods), while classes are data(fields)
    3. Instances contain classes
    4. Classes are the blueprint for creating instances
    5. Instances are the blueprint for creating classes
  6. (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. new Student() = s1.getClassYear
    2. getClassYear = s1()
    3. getClassYear(s1)
    4. s1 = getClassYear()
    5. s1.getClassYear()
    6. (s1.getClassYear()).toString()
  7. (1pt) true or false?: When you declare a variable to hold a PizzaServer reference, a new PizzaServer instance is automatically created.
  8. The next sequence of problems refer to the provided classes PhoneNumber and ContactInfo.

     
    public class PhoneNumber {
    
      private String areaCode;   // Exactly 3 digits.
      private String exchange;   // Exactly 3 digits.
      private String extension;  // Exactly 4 digits.
    
      /** @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(                                   ) {
    
    
    
        // YOUR CODE HERE
    
    
    
    
        }
    
      }
    

  9. (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.)
  10. /** A class to organize the contact info for a person.  */
    public class ContactInfo {
    
      private String name;             // You may abbreviate this "n"
      private PhoneNumber homeNumber;  // You may abbreviate this "hN"
      private PhoneNumber workNumber;  // You may abbreviate this "wN"
    
      /** @return the person's home phone number. 
       *  You may abbreviate this "gHN()"
       */
      public PhoneNumber getHomeNumber() { return this.homeNumber; }
    
      /** @return the person's work phone number. 
       *  You may abbreviate this "gWN()"
       */
      public PhoneNumber getWorkNumber() { 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 ) {
        // YOUR CODE HERE (one line suffices)
    
        }
    } 
    
  11. (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).
  12. (1pt) On the homework grading-guide, what does eta (η) stand for?
  13. (5pts) Sketch 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;
    
    (Be sure to include all necessary PhoneNumber objects! For fields which are Strings, leave the contents blank (since we have omitted the details of what actual strings are being used).)
  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?                 

Crazy Emcees

In lab02b.html, we wrote code so that EmCees could make regular introductions, as well as spy-introductions. We'll take that code and modify it. (You can use the posted lab solution if you like.)

We will augment EmCees so that they have yet another way to make an introduction: When asked to String introduceSurprise(String first, String last), they will sometimes return regular introduction, and sometimes return a spy-introduction. But there is rhyme and reason to their method:

Note that each EmCee has their own cycle. (If two different, brand-new EmCees are each asked to introduceSurprise, they will both respond with a regular introduction, since it's the first time each of them is asked that question. … This would make a good test case!)

Also, this cycle only counts the number of times introduceSurprise has been called. How many times an EmCee is directly asked to introduce or introduceSpy has no effect on introduceSurprise. (That's another good thing to verify, with test cases.)

Part (b)

(15 pts) Due Mar.24 (Mon); Submit the html documentation and test-class for EmCee as usual. (You don't need to submit those for class Die, if you didn't change that class.)

Part (c)

(15 pts), due Mar.26 (Wed) in class. Submit the code for EmCee, as usual.
(Don't submit any code for class Die unless you modified that class.)

Implement introduceSurprise, so that all your test cases pass, and that when you call showTests multiple times, you get different answers displayed on the console.

One possible hint: As a quantity gets bigger and bigger, its remainder (%) mod 4 cycles through 0,1,2,3,0,1,2,3,0,1,….

Be sure not to repeat code!

This is a bit streamlined; a few things to note:

1byte-sized?      

2Every anomoly has a log? That's definitely a weird world being modeled! Similarly, saying that every bump contains a frog is also a bit bizarre. (That's why this problem is extra-credit.)      

3 If you want to add class Die to your current BlueJ project, you can Edit > Add Class from File... (or, of course, you make a new class and paste in the code from your lab.) There is no need to modify your existing class Die from lab.      

4 In order for the test-class to let you call the method, its name must start with the first four letters “test”.      

homeinfoarchiveexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


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