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

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lab01b
your turn: a function from scratch
buffetPrice, toppingArea

For today's lab, you must work with a partner. We will check off your work, before leaving.

Writing a method

  1. Fire up BlueJ, and create a new project for today, lab01b.
    (Either on your H drive or your partner's. But at the end of class, email the code to the other partner, so they can have a copy on their H drive as well.)
  2. Make a class PizzaServer (again), and copy/paste in the following:
    /** Functions to calculate the area of pizza purchased, given a diameter.
     *
     * @author Ian Barland
     * @version 2007.Jan.10
     */
    class PizzaServer {
    
    
      /** Calculate the area of a pizza, given its diameter.
       * @param diam The diameter of the pizza, in inches.
       * @return The area of the pizza, in square inches.
       *
       * If 'p' is a PizzaServer instance, then:
       *    p.pizzaArea( 0) =   0        +/- 0.001    
       *    p.pizzaArea( 2) =   3.14     +/- 0.001    
       *    p.pizzaArea(12) = 113.04     +/- 0.001    
       *    p.pizzaArea(16) = 200.96     +/- 0.001    
       *    p.pizzaArea(20) = 314        +/- 0.001    
       */
      double pizzaArea( double diam ) {
        return 3.14 * (diam/2) * (diam/2);
        }
    
      }
    

    We will be adding more methods within the same class which contains pizzaArea -- not make a whole new stand-alone class. More precisely: we are adding new possible behaviors to PizzaServers!
  3. Krusteaze Pizza Korp. has an all-you-can-eat buffet meal. In order to encourage people to bring their friends, the price is $8 for the first person, plus $4.50 for each friend after that.

    Write a function buffetPrice, which is given the size of the group (1 or bigger), and returns the total cost. (Your function will be used by the cash register.)
    We'll follow the usual series of steps:

    1. Write at least three test cases, inside comments:
        /* test cases for buffetPrice, assuming 'zoe' is some existing PizzaServer:
         *   zoe.buffetPrice(  ____  )  = ______
         *   zoe.buffetPrice(  ____  )  = ______
         *   zoe.buffetPrice(  ____  )  = ______
         */
      
      Note that zero has been specifically disallowed, as a group-size.
    2. Paste these test-case comments either before or after the comments+function for pizzaArea (but still inside the curly-brackets for class PizzaServer.)
    3. What will the first line of code look like, for this function? First, let's recall how the first line of pizzaArea and ufoHeightAt each looked, from Tuesday's lab:

                  int ufoHeightAt( int frameNum )  { 
                     ... 
                     }
      
                  double pizzaArea( double diam )  { 
                     ... 
                     }
                
      This suggests our function should start out similarly:                  buffetPrice(                                   ) That is, what type of number does it take in as input -- an int, or a double? Similarly, what type of answer will a PizzaServer give -- int or double?

      This information called the function's signature.

    4. Based on your test cases, what an arithmetic formula for the general case? (This formula should involve your parameter!)
    5. Translate the arithmetic into Java.
      (I recommend first writing “return          ;” and then go back to type the Java-expression inbetween those bits of syntax.)
    6. Test it, to make sure it passes your test cases: make a PizzaServer named bo, and ask him/her/(it?) about buffetPrice.
      You should be able to call your function both by right-clicking on the red bo, and also know how to do it by typing into the code pad. (Recall Tuesday's lab, if you need examples or syntax of the calling-a-function.)

  4. Some customers are concerned with the surface area of their pizza. Others ignore the crusts, and want to ask the server how much topping-area each pizza comes with. The server happens to know that Krusteaze Pizzas have a 3″ border of crust all the way around them (what cheapskates!).

    Write the function toppingArea.

    1. What are some test cases? (at least three, written in comments)
      You'll want to sketch some pictures, to get your answers right! (What is the smallest possible diameter which can still have a 3″ crust all the way around? This is the smallest test case that will make sense, for this problem.) Feel free to call pizzaArea in the Code Pad, to figure out the exact numbers.
    2. Checking off: Raise your hand, and the instructor or peer instructor will come around to check off your work. We'll want to see your buffetPrice code, the result of calling that function on the particular inputs 1, 2, and 11. If you also have some correct test cases for toppingArea (including the smallest sensical input), you'll get a “check-plus” :-) Although we won't actually check-off due to the snow day, you should be able to get this far. If not, bring questions to class on Friday.
    3. At the end of lab, make sure you and your partner both have a copy of the code, for future reference.
    4. What is the signature of toppingArea?
    5. Now: what is the arithmetic formula, to compute the topping area, given the 3″ crust?
      Think back on how you worked out the test cases, earlier. Your code should reflect your thought process in doing the test cases!
    6. The function itself will include “return” followed by that arithmetic (translated into Java), followed by a semicolon.
    7. Compile your program, and run the test cases. Do you get the answers you expected?

  5. Before Monday, read the lecture notes on avoiding repeated code.
  6. Extra: make sure you don't have any repeated code — multiplying by 3.14, and squaring a radius, should only happen once in your entire program, up in pizzaArea, and nowhere else.
    (In my solution, the only arithmetic inside toppingArea is a single subtraction … and one multiplication(?!).)
  7. One last problem (not needed for check-off, but you should be able to work through it):
    Some people actually like the crust of a pizza! They want to ask PizzaServers how much crust there is, on a pizza of a given size!

    Follow the same steps above, to write crustArea. (In my solution, the only arithmetic inside crustArea is a single subtraction.)

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


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