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

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

hw02
Strings, javadoc, local variables
shortened greetings

Part (a), due Aug.31 (Fri):

Instructions: Submit on paper, at start of class(lab). (No Blackboard submission required, for (a).)
  1. (1pt) Java has Math function (method) which 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. (3pts) 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. A Javadoc comment begins with what special 3-character sequence?                 
    4. What Javadoc “@” keyword indicates that you are describing one of the parameters of the function?                
    5. 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. (1pt)
    1. Declare a local variable of type double, named dinnerPrice.                                         
    2. Then, as a separate statement, initialize dinnerPrice to be 7.5.                                         
  9. (1pt)
    1. Declare a local variable of type String, named royalTitle.                 
    2. Then, as a separate statement, initialize royalTitle to be "Her Royal Highness".                 
  10. (5pts) For the code below, which line number(s)…
    1. declare a function (that is, include a signature)?
    2. call a function? (that is, have an object-dot-methodname-openParen-arguments).
    3. declare a local variable?
    4. initialize a local variable?
    5. Use the value stored in a local variable1?
    6. Use the value stored in a parameter?
    (Each answer will be a list of numbers, e.g. “b. 7, 12.”)
     1    /** Calculate the are of *crust* on a pizza, given its diameter.
     2     *  Krusteaze pizza has 3" of crust all the way around.
     3     * @param diam The diameter of the pizza (in inches); must be 6 or more.
     4     * @return The amount of crust (in square inches)
     5     *  p.crustArea( 6) = p.pizzaArea( 6) - p.toppingArea( 6) =~  28.26
     6     *  p.crustArea(12) = p.pizzaArea(12) - p.toppingArea(12) =~  84.78
     7     *  p.crustArea(20) = p.pizzaArea(20) - p.toppingArea(20) =~ 160.14
     8     */
     9    double crustArea( double diam ) {
    10      double totalArea;
    11      totalArea = this.pizzaArea(diam);
    12  
    13      double innerArea;
    14      innerArea = this.toppingArea(diam);
    15  
    16      return (totalArea - innerArea);
    17      }
  11. Part (b), due Sep.03 (Mon):

    Instructions: turn in the html-documentation for the functions, both hardcopy and in Blackboard > Assignments > hw02b > attach files. (details in #18, below).

    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.)

  12. 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!
  13. Fill in the blanks below with valid javadoc for shorten.
  14. Fill in the blanks below with the signature for shorten. (A dummy return-statement has already been provided, in the stub function-body.)
  15. /** 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.
      }
    
    Your code should compile, and if you select the “interface” (“documentation”, in BlueJ v2.2.0 ) view from the upper-right corner of BlueJ's editor window, you'll see the documentation for your function, auto-generated from your javadoc comments.
  16. Make two additional test cases for greetFriend. (Write them as inside comments inside the file.)
  17. Write all the necessary javadoc for greetFriend.
  18. Write the signature for greetFriend.
  19. Write a stub for the function-body, just so your function compiles.
  20. For the above four steps, follow the same format as provided for shorten: One comment with javadoc and the test cases, then the function signature and stub-function.
  21. 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. Print this page, and you can also save-as-html and submit that to WebCT. (Alternately, you can submit your .java file to WebCT instead, since that contains all the information necessary to generate the web page).
    Unfortunately, you can't print it from inside BlueJ (?!?!). In order to print the web documentation:
    • Be sure to view the documention from within BlueJ.
      (This is necessary to force BlueJ to generate those web pages.)
    • Start up a web browser, and open the local file from your H: drive. If you named your project “hw02”, and your class “EmCee”, then navigate to H:/itec120/hw02/docs/EmCee.html and open this file. You should see the same thing you were looking at from inside BlueJ.
    • Now print the page (from within the web browser).
    • Submit this same .html file to WebCT.

      Alternately, you can submit your java file (since that contains all the javadoc used to generate the html page):
        H:/itec120/hw02/EmCee.java
      If you submit this “.java” file, you don't have to submit the .html file, although there's nothing wrong with submitting both.
      Note that Windows Explorer hides the “.java” suffix. Do not submit files with other suffixes (no “.class” or “.ctxt” files).

    • Optional/advanced:
      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).
    optional: 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.)
  22. Part (c), due Sep.05 (Wed):

    Instructions: Turn in your code (both hard-copy and on WebCT) for class EmCee.
  23. 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.
  24. 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.
  25. Write a version greetFriend_v2 which gives exactly the same answers as greetFriend, but if you used a local variable in greetFriend then don' use one in greetFriend_v2, and vice versa.

1Remember that while “local variable” and “parameter” are very similar concepts, they are technically different from each other.      

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


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