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

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

hw04
quotient and remainder

Part (a)

Due Feb.08 (Fri): Problems 1-12; only hardcopy is needed.

Part (b)

Due Feb.11 (Mon): Problems 13-15, the html-documentation only (signatures, javadoc, and test cases). Turn in a hard-copy in class and also electronically submit (on WebCT) a copy of the java file.

(Note that the javadoc has already been completed for you this week; although you can put a comment with an @author tag preceding the class declaration.)

Part (c)

Due Feb.13 (Wed): Problems 14-15, implementation (Java code).

Note that you can consider this homework of studying for the exam.

  1. (2pts) Based on the following function-call, what is the signature for this function? (Make up any parameter names you like.)
                    
    2 + bob.gloopz( "Tuesday", false )
    Extra credit (1pt): what are all the possible return types this function might have?
  2. (2pts) Based on the following signature give a sample call to the function, and determine whether the result is bigger than zero. You can assume z is (a variable referring to) an object of the appropriate type.
    double roundTripTime( String url, int portNumber )
    
    
  3. (3pts) Give the signature of a function named startsWith, which takes in two Strings returns a boolean (indicating whether the first input begins with all the letters of the second input, if you're curious).                                         
  4. (1pt) Which one of the following is not a javadoc “@” keyword?
    1. @return
    2. @author
    3. @double
    4. @param
  5. (1pt) What does 22/5 evaluate to?
    What does 22.0/5.0 evaluate to?
  6. (2pts) Rewrite the following expression for clarity (without doing any arithmetic yourself):
    2*3-26/2/3*3+1                     
    What is the result of evaluating the expression?                 
  7. (3pts) For each of the following, does it describe a named constant (“nc”) or local variable (“lv”)?
    1.          must be declared and initialized in the same statement.
    2.          Its initial value depends on parameters.
    3.          Should be declared inside a class declaration, but before any functions.
    4.          Its first letter should be lower-case.
    5.          Written in ALL_CAPS.
  8. (2pts)
    1. Declare a local variable suitable for storing whether or not a store accepts credit cards.                     
    2. Initialize that variable to hold whatever value you prefer.                     
  9. (2pts) Without doing any arithmetic yourself…
    1. write a java expression for the remainder after dividing 579 by 100 (in this example, 79).           
    2. write a java expression for the quotient after dividing 579 by 100 (in this example, 5).           
  10. (2pts) What does the following snippet initialize guess to?
    double weight = 30;
    boolean enjoysAnts = true;
    double mph = 30;
    String guess;
    
    if (weight > 100) {
      if (enjoysAnts) {
        guess = "aardvark";
        }
      else {
        guess = "buffalo";
        }
      }
    else {
      if (mph >= 30) {
        guess = "cheetah";
        }
      else {
        guess = "dik-dik";
        }
      }
    
    (Coloring is provided to help you see the structure.)
  11. (2pts) If startPage and endPage are both ints, write an expression which captures...
    1. endPage minus startPage is at least 5”.
    2. endPage minus startPage is at least 5, and endPage is at least 100”.
  12. (2pts)
    boolean outOfBounds( int i ) {
      return ((i < 0) || (i >= 100));
      }
    
    What does this outOfBounds return, if given ...
    1. -50
    2. 0
    3. 50
    4. 100
    5. 150
  13. Consider the following function:
      /** Given a number of cents, return a String for that amount in dollars,
       *        but expressed in a very odd way, as shown in the tests below.
       * @param cents An amount of money (in cents).
       * @return A String representing that amount, in dollars.
       * Examples (assuming that `c` is a Cashier):
       *   c.centsToStringOdd(579) = "dollars 5, and cents 79"
       *   c.centsToStringOdd(148) = "dollars 1, and cents 48"   (Note "dollars 1" is okay grammar, in this odd phrasing)
       *   c.centsToStringOdd(  8) = "dollars 0, and cents 08"   (Note that we return the character '0' in front of the '3'!)
       *   c.centsToStringOdd(                ) =                 
       *   c.centsToStringOdd(                ) =                 
       *   c.centsToStringOdd(                ) =                 
       */
    
    For part (b), complete the test cases above and write the signature.
    (You will not write any code for this, not even for part (c)!)
  14. Consider the following function:
      /** Given a number of cents, return a String for that amount in dollars,
       *        including a preceding dollar-sign,
       *        but using a (european-style) decimal-comma rather than a decimal-point.
       * @param cents An amount of money (in cents).
       * @return A String representing that amount, in dollars.  Use a comma
       *          for the decimal-separator.
       * Examples (assuming that `c` is a Cashier):
       *   c.centsToStringEuroStyle(579) = "$5,79
       *   c.centsToStringEuroStyle( 48) = "$0,48"
       *   c.centsToStringEuroStyle(                ) =                 
       *   c.centsToStringEuroStyle(                ) =                 
       *   c.centsToStringEuroStyle(                ) =                 
       */
    
    For part (b), complete the test cases above and write the signature. Thorough and careful testing is particularly suggested for this problem.
    For part (c), write the code and make sure all your test cases pass. Hint: Don't do any floating-point division; use % and / (quotient). If you are confused about ‘how to get the comma in there’, think about how you'd write a solution to centsToStringOdd and just remove the words “ and cents ”.
  15. The fancy bread shop, Oh Bonnie Pain, sells delicious Sugarbomb Donuts for 85 cents each. In order to seem european, they will list their prices using a comma as the decimal-separator.
      /** Respond to a donut order, at Oh Bonnie Pain.
       * @param nombreDonuts The number of donuts ordered.  Non-negative.
       * @return A sophisticated-sounding message reporting the total price.
       * Tests (presuming `c` is a Cashier):
       *   c.donutOrderrier( 10 ) = "10 sucrebombe donuts will cost $8,50.  Merci!"
       *   c.donutOrderrier(     ) =                                                             
       *   c.donutOrderrier(     ) =                                                                                                     
       */ 
    
    For part (b), complete the test cases above and write the signature.
    For part (c), write the code and make sure all your test cases pass.
  16. Extra Credit: (4pts test cases/docs; 4pts code; due with part (c))
    Write a function centsToString which is more flexible than either centsToStringEuroStyle or centsToStringOdd: It takes in three arguments: a number of cents, and what text used to indicate the currency, and a decimal separator (a String).
    Now, rewrite the body of centsToStringEuroStyle with just one short line:
                                                                                                        
    Also, write the body of centsToStringOdd with just one short line:
                                                                                                        

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


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