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

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

hw06
constructors

Part (a)

Due Feb.22 (Fri) (20pts): only hardcopy is needed.

  1. (1pt) The purpose of a constructor is to make sure that                                                                                 
  2. 01.      class Glarkzle {
    02.
    03.        int MAX_ORDER_SIZE = 25;
    04.
    05.        int numFloops;
    06.        String homePlanet;
    07.
    08.        Glarkzle( int _numFloops, String _homePlanet ) {
    09.          /* ... body not shown ... */
    10.          }
    11.
    12.        // Setters/getters not shown...
    13.
    14.        String placeOrder( int numOrdered ) {
    15.          /* ... body not shown ... */
    16.          }
    17.
    18.        }
          
    1. (1pt) Which line(s) declare a field?                 
    2. (1pt) Which line(s) declare a named constant?                 
    3. (1pt) Which line(s) declare a constructor?                 
    4. (1pt) Declare a variable to hold an object of this class. (Don't do anything more):                 
    5. (2pts) Complete the picture of what things will look like immediately after declaring the the variable, by labeling the (currently-uninitialized) box appropriately:
      the environment:
             +--------+
      ____   |        |
             +--------+
      
      (Note that the contents of the labeled-box should be empty, since you haven't initialized the variable yet.)
    6. (3pts) Write an expression which calls the constructor, storing the result in the variable you just declared above.                                                                                 
    7. (3pts) Draw a picture of what things will look like immediately after calling the constructor and initializing the variable. That is, complete the box for the newly-created object, as in hw05a.
      the environment:                    The object bench (the “heap”)
             +--------+                   
      ____   |    *---|-------\           
             +--------+        \          +-----------------------------+ 
                                \         |                             |
                                 \------> |                             |
                                          |                             |
                                          |                             |
                                          |                             |
                                          +-----------------------------+
      
    8. (2pts) Write an expression which calls the placeOrder method of the instance you constructed, concatentating "Your order ID: " to the front of the result.                 
  3. (2pts)
    1. true or false?: a setter method doesn't return a value.
    2. true or false?: a setter method takes one argument.
    3. true or false?: Compiling a class causes one instance of that class to be created.
  4. (3pts) For this problem, you might want to look at the documentation for class String. Hint: String's full name is java.lang.String; see the links at the top of each class web page.
    1. What is the result of typing "abcde".charAt(2) in the Code Pad?
      (You can see also the optional part of lect06a: the truth about strings)
    2. According to the documentation for String, what is the return type of charAt(int)?
      (Note that your answer does not start with a capitalized letter, so the return type isn't a class; it is actually the last of the primitive types we'll mention in class1).
    3. What is a String method which determines whether one String is equal to another, ignoring case? Type an expression in Code Pad which calculates whether or not "theRearEarthAtStorehouse" equals "thereAreArtHatsToRehouse" (ignoring case).                                                                                 
      (Okay, you can simply compare hiThere to hitHere if you want to do less typing.)
    4. Note that these questions all involve walking up to some particular String object and asking it a question. In the first case the input happens to be an int, and in the second case it happens to be another String, but otherwise the general task is the same.


Parts (b), (c)

Part (b) (20 pts) due Feb.25 (Mon): turn in two printouts: the documentation HotChocoBooth.html, and the actual code for the test cases (HotChocoBoothTest.java — not the other way around!
(You'll presumably create those tests through BlueJ's “Create Test Method…” feature; when done recording, open the khaki test class and print.) You should have a separate test case for each of the methods mentioned below. Your test cases for this course will no longer be written in comments. On WebCT, attach HotChocoBooth.java (which has the documentation), as well as HotChocoBoothTest.java (which has the tests).

Part (c) (20 pts) due Feb.27 (Wed). Turn in just HotChocoBooth.java (hardcopy and on WebCT).

The BCB (“Better Cocoa Bureau”) has hired you to help keep track of how Hot Chocolate booths do business. Every Hot Chocolate Booth has a capacity (how much hot chocolate they can possibly make per day), and a cash reserve. During a business day, a booth will:

A fine, high-capacity samovar
for piping hot chocolate
  1. buy supplies at $5 per liter. A booth will as much supplies as it has capacity, or as much as it can afford (which ever is less).
  2. During the day, all the hot chocolate will get sold. However, the sales price depends on the the day's temperature: the price fetched is $(65-t)/5 per liter, where t is the temperature in fahrenheit.
  3. A hot chocolate booth might sometimes upgrade its capacity: this costs $100 per (additional) liter.
  4. When somebody creates a brand new Hot Chocolate Booth, they provide an initial amount of money, and state how much capacity they want to buy immediately (at $100 per liter).

Examples

  1. The booth “Cuckoo for Cocoa”
    1. starts with $340, and immediately purchases 3 liters of capacity, leaving them with $40 cash reserves.
      HotChocoBooth cc = new HotChocoBooth( 340, 3 );
    2. On their first day of business, they purchase supplies: they can brew 3 liters (bringing their cash reserves down to $25), and then the day turns out to be 30°, meaning they sold their hot chocolate for $(65-30)/5 = $7 per liter, bringing their cash reserves to $25+$21 = $46.
      cc.doBusiness( 30 );
    3. On the second day, they again purchase supplies for 3 liters (bringing their cash reserve down to $46-$15=$31), and the warm weather means they have to sell their hot chocolate for only $(65-62)/5 = $0.60 per liter, bringing their cash reserves to $31+$1.80 = $32.80.
      cc.doBusiness( 62 );
  2. The less-successful booth “Koko Kaka”:
    1. starts with $500, and invests in 2 liters of capacity. This leaves them with $300 cash reserve.
      HotChocoBooth kk = new HotChocoBooth( 500, 2 );
    2. On their first day of business, they buy supplies for 2 liters (costing them $10, leaving their cash reserve at $290). The temperature is 30°, so they can sell their hot chocolate at $(65-30)/5 = $7 per liter. That puts their cash reserve at $290+$14 = $304.
      kk.doBusiness( 30 );
    3. Flushed with success, they invest in, expanding their capacity to 5 liters, leaving them with $4 cash reserves.
      kk.expandCapacity( 3 );
    4. The next morning, they can only afford supplies for 0.8 liters (putting their cash reserves at $0). The temperature, unfortunately, reaches 62°, so they are forced to sell their hot chocolate at $0.60 per liter, leaving them with cash reserves of $0+$0.48 = $0.48.
      kk.doBusiness( 62 );

Methods

Grading

As usual: Your test cases will be graded on whether a faulty program could still pass all your tests. Your code will be graded on readability, indentation, using named constants, not repeating code, passing your test cases, as well as correctness.

Assumptions

A skeleton

You can use any or all of this skeleton file, but be sure to add/change anything you need to. You might find it necessary to include more methods than those specifically asked for.

1So the primitive types you need to know are boolean, double, char, int (integers up to ±2 billion (approx)), long(integers up to ±10 quintillion (approx)). The primitive types which you don't need to know are float (single-precision floating-point), short (integers up to ±32767), and byte (integers up to ±128).      

2Or Java's conditional operator, ? :, which you can read about in the book.      

3 in case of a n-way tie, each winner will get Math.ceil(10/n) points).      

4 (With a bit of algebra you can compute at what temperature hot chocolate is free; we're saying that it will never get warmer than that temperature. What temperature is needed to break even, on the cost of supplies?)      

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


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