RU beehive logo ITEC Department logo
ITEC 120
2007spring
ibarland,
jpittges

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

hw02
hw02
local variables, booleans

Due Date: Jan.29 (Mon.), 17:00.

  1. (1pt) Declare a local variable of type int, named numBuddies. Then, in a separate line, initialize numBuddies to be 17.
  2. (1pt) Declare a local variable of type String, named knightTitle. Then, in a separate line, initialize knightTitle to be "Brave Sir".
  3. (2pts) Re-write the body of this function to be one single return statement. Do not do any simplifying arithmetic (which would obscure what is happening), and do not keep any local variables (even though they arguably improve the code's clarity, oh well).
        /** Convert meters into feet.
         * @param meters A distance, in meters.
         * @return that same distance, as measured in feet.
         */
        double metersToFeet( double meters ) {
          double cm;
          cm = meters * 100;
          double inches;
          inches = cm / 2.54;  // This is the only metric length conversion I know!
          return inches/12;
          }
    
    (You can just write down the one return statement.)
  4. (3pts) Re-write this function so that it uses one or two local variables. Do not do any simplifying arithmetic yourself.
        /** Convert feet into furlongs.
         * @param feet A distance, in feet.
         * @return that same distance, as measured in furlongs.
         */
        double feetToFurlongs( double feet ) {
          return (feet / 3) / 220; 
          // Dividing by 3 gives us yards; dividing yards by 220 gives us furlongs.
          }
      
    You can just write down the body of the function — that is, the part between {}.
  5. (4pts) For the code below, which line numbers…
    1. declare a function (that is, include a signature)?
    2. call a function?
    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      }
  6. (1pt) Without doing any arithmetic yourself, write a Java expression which asks “is 210 is bigger than 1010?”. What is the result of evaluating this expression in BlueJ's Code Pad?
  7. (2pts) For the function described, define its signature. (You'll need to come up with a good, descriptive name for the function.)
    /** Look up how many credit-hours a course is.
     * @param deptName The name of a department (the four-letter abbreviation).
     * @param courseNumber The number of a course, .
     * @return The number of credit-hours for that course is worth.
     */
    
    Make up one plausible test-case for this function.
  8. (2pts) For the function described, define its signature. (You'll need to come up with a good, descriptive name for the function.)
    /** Determine whether a student is allowed to register for a particular course.
     * @param studentID A student's ID number.
     * @param deptName The name of a department (the four-letter abbreviation).
     * @param courseNumber The number of a course.
     * @return whether or not the named student is allowed to enroll in the course.
     */
    
    Make up one plausible test-case for this function.
  9. We enter the world of competitive volleyball; for this problem make new class VolleyballJudge. When writing programs, remember the steps of The Design Recipe.
    1. (6pts) Write a method didFirstTeamWin, which takes in two volleyball scores, and returns a true/false answer: whether or not the first team has won — that is, whether the score represents a completed game and the first team has a higher score. (So if didFirstTeamWin returns false, it means that either the first team lost or the game is still be continuing.) In volleyball, you keep playing until one team reaches 21 (or more) points, and the difference between the scores is two or more (that is, you can't win by just one point, and there are never ties).
    2. (4pts) Write a method isGameOver, which takes in two volleyball scores, and determines whether the game is still in progress. Call didFirstTeamWin.
      (Hint: call it twice — perhaps with the inputs in a different order.)
    3. (4pts) Write a method announceWinner, which takes in four inputs — a team's name, their current score, the opposing team's name, and the opposing team's score.2 Here are three test cases (assuming that vj is some instance of class VolleyballJudge):
      vj.announceWinner( "beachfront boppers", 21, "valley volleyers", 15 ) = "beachfront boppers won!"
      vj.announceWinner( "beachfront boppers", 15, "valley volleyers", 21 ) = "valley volleyers won!"
      vj.announceWinner( "beachfront boppers", 21, "valley volleyers", 20 ) = "game still in progress."
      
      Include at least one additional test case, and make sure it involves a particularly simple input. (Hint: is the empty string ever a legal team name? Is 0 ever a legal volleyball score?)
    4. When printing a hardcopy of your class VolleyballJudge, turn both the interface view3 as well as the implementation (with the interface view first).

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

2 It is mildly unsatisfying, aesthetically, that we hand the judge four independent inputs, even though really we have in mind two name/score pairs. We'd prefer to somehow bundle together the name and score of each team, and then just hand the judge those two bundles. Don't worry, we'll see how to bundle related information together in a week 4.      

3Recall from lab01b, that the upper-right window can switch between these views. If you get an error when trying the interface view, check the class message boards for possible solutions.      

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


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