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

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

hw02
functions, and Strings
shortened greetings

Part (a), due Jan.25 (Fri):

Instructions: Submit on paper, at start of class(lab). (No Blackboard submission required, for (a).)
  1. (1pt) Java's Math object knows how to answer many different questions (“has many functions/methods”). Which one of those functions returns the next integer which is bigger-or-equal-to its input (like rounding, but always upward). Look at the documentation for class Math, find the function, and give its signature:
                                     (                                  ) (Hint: It starts with the letter ‘c’; the method summaries, near to the top of the page are listed alphabetically.)
  2. (2pts) Conventions and comments:
    1. true or false?: in the examples in lecture, the name of each class starts with a capital letter.
    2. true or false?: in the examples in lecture, the name of each function starts with a capital letter.
    3. Read about javadoc documentation.
    4. A Javadoc comment begins with what special 3-character sequence?                 
    5. What Javadoc “@” keyword indicates that you are describing one of the parameters of the function?                
    6. What immediately follows that keyword?                                         
  3. (2pts) When writing the body of a function, if you need to call another function (belonging to that same object), use the Java keyword                 .
  4. (1pt) true or false?: It can make sense to use “this” in the Code Pad (outside of any particular function).
  5. (3pts) Suppose class Blizgrffblop contains a function with whose signature is
      double fizzle( String s )
    
    If bz is an instance (object) of class Blizgrffblop,
    1. Call fizzle, passing it the string "hmmm".
    2. Call fizzle, passing it the empty string.
    3. Call fizzle, passing it the empty string, and then divide the result by 7 (all in a single statement).
    (Make sure your function-calls match the required Java syntax.)
  6. (1pt) Write the signature for a function named shlark, which takes in two pieces of text, and returns a number (which is always a whole number).
  7. (2pts) For each of the following pieces of information, what is the most appropriate Java type to represent them? (The Java types we have covered are int, double, String).
    1. The number of PS-3s in stock, at a certain store.                 
    2. The current sale price of a PS-3 at that store, in dollars.                 
    3. The current sale price of a PS-3 at that store, in cents.                 
    4. The serial number of a particular PS-3 (something like 4QJ7B93).                 
  8. Part (b), due Jan.28 (Mon):

    See #15 below for details of what to turn in.

    Inside the class EmCee from lab, we'll have another function shorten, which takes in two inputs -- a last-name and a first-name, in that order -- and returns the person's initials. In addition, the function greetFriend takes the same inputs in the same order, and returns a greeting for that friend.

    mcJo.shorten( "Rogers", "Fred" ) = "F.R."
    mcJo.greetFriend( "Rogers", "Fred" ) = "Yo, F.R. -- whazzup?"
    
    (If you want to have greetFriend answer with some pattern besides “yo ... whazzup” that's fine, but whatever pattern you choose will apply to all its answers.)

  9. Make two more test cases for shorten. (At least one of those test cases should use the smallest input which still makes sense for the problem.) Make sure your test cases have the exact desired punctuation and spacing!
  10. Fill in the blanks below with valid javadoc for shorten.
  11. Fill in the blanks below with the signature for shorten. (A dummy return-statement has already been provided, in the stub function-body.)
    /** shorten:
     * Compute somebody's initials, given their last and first name.
     * @param                                                      
     * @param                                                      
     * @return                                                                                 
     * 
     * Test cases (presuming that mcJo is an instance of EmCee):
     *    mcJo.shorten(                 ,                  ) =                 
     *    mcJo.shorten(                 ,                  ) =                 
     */
                     shorten(                                  ,                                   ) {
      return "Z.Z.";  // A dummy stub.
      }
    
  12. Note: For the next four steps, follow the same format as provided for shorten: One comment with javadoc and the test cases, followed by the function signature and stub-function.
  13. Make two additional test cases for greetFriend.
  14. Write all the necessary javadoc for greetFriend.
  15. Write the signature for greetFriend.
  16. Write a stub for the function-body, just so your function compiles.
  17. Print out the documentation for this function: From the BlueJ's project, choose Tools > Project Documentation, which will fire up a browser viewing your documentation. In Blackboard, submit your entire project folder for hw02 the file EmCee.java1 (which includes the documentation).

    Optional/spiffy: You probably saw that your test cases all run together, in the auto-generated documentation. You can fix this by writing “<br/>” at the end of each line of your test-cases. (“<br/>” is HTML markup for a line-break.)
    Make sure to look over your documentation and make sure that you see each of your functions listed with a summary sentence, and further down you should have each parameter described, and a “Returns” section which includes a short sentence plus your test cases. If any of these are missing, it means you didn't get your javadoc keywords right (or in the right place).

  18. Part (c), due Jan.30 (Wed):

    Instructions: Turn in your code (both hard-copy and on WebCT) for class EmCee.
  19. Write a working function-body for shorten.
    To do this, you'll need to knowing how to extract the first letter from a String object. You can this by asking it for a “substring” of itself: If s is a String, then s.substring(0,1) returns the first letter of s. You can try the following in Code Pad:
    String s1;
    s1 = "hello";
    s1.substring(0,1)
    
    String s2;
    s2 = "bye";
    s2.substring(0,1)
    
    String s3;
    s3 = "Z";
    s3.substring(0,1)
    
    String tooShort;
    tooShort = "";
    tooShort.substring(0,1)   // ERROR!
    
    You can see the documentation for substring, to see exactly what the 0 and 1 mean.
  20. Write the function-body for greetFriend.
    Don't repeat any code from shorten; instead call that method, if you want to get the initials from some words.
  21. Write a version greetFriend_v2 which gives exactly the same answers as greetFriend, but if you used a local variable in greetFriend then don't use one in greetFriend_v2, and vice versa.

1Remember that Windows sometimes suppresses the “.java” suffix from the filename, so the file might simply be “Emcee”. Do not submit a file with some other suffix like “.class” or “.ctxt”.      

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


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