Radford University ITEC

ITEC120-ibarlandinfolectureslabs

finishing Booleans; syntax review

Reviewing hw03

(2006.Oct.09, Mon) Some key elements from the hw03 solution:

ITEC120-ibarlandinfolectureslabs


(2006.Oct.10, Tue)

Comparing doubles

Beware numerical inaccuracies: (2.0 - 1.1 == 0.9), or ( (7.0/25.0)*25.0 == 7.0 ).
double x = 2.0 - 1.1;
double y = 0.9;

x == y
// huh?  That's an unexpected answer.  How to debug?

x
// Oh, now it's clear what happened.
Do not use == with doubles. So how do we see if two doubles are practically-equal? One immediate imponderable is “how close is close enough to be considered practically equal”? Well, for the moment1, let's say being within 0.001 is close enough: ((y-0.001 < x) && (x < y+0.001)) is a start.

Returning boolean

Write approxEq: a function which takes in two doubles, and says whether or not they are approximately equal. (What are test cases?)

Passing in booleans

Back in an early lab, we wrote a couple of methods salesTaxOn and priceAfterTax. But some items are tax-free. Change priceAfterTax so that it accepts a second argument isTaxable — a “flag” indicating whether or not this item is subject to sales tax.

Naming convention: start boolean variable names with “is”.

Overloading

Interestingly, we can have two versions of priceAfterTax in the same class. Even though they have the same name, when somebody calls one of them, it's possible to distinguish which one of the two functions they want: by looking at the full signature (do they pass a double, or a double-and-a-boolean), Java can tell which method was intended.

Repeated code? How to avoid this? Have one of the two functions call the other, filling in the “default” arguments! This is a very common pattern: one primary version of the function taking many arguments, and several smaller versions with fewer arguments, each method just adding one default argument and calling the next version. Alternately, often there are exactly two versions of a function: one version taking the common arguments, plus one big version with all sorts of rare arguments2

Consider an example from an early lab: the sales tax on most items is 5%, but on cars it is 3%, and houses have yet a different rate. Now we can have three methods: taking a price, taking a price and a flag “isTaxable”, taking a price and a flag and a tax-rate. Write this. (You could also conceive of taking a price and a tax-rate without a flag “isTaxable”, but we won't do that here.

Notes on overloading: You must be able to distinguish them just by the input types; Java doesn't use the return type (partly because it does so much auto-converting of things to Strings behind-the-scene).

Overloading is convenient, but not a deep concept: if the language didn't provide it for us, we'd just give slightly different names to each function (“salesTax_isTaxable”, “salesTax_isTaxable_taxrate”, etc.). After all, when somebody is calling the function, they know right then exactly which flavor of the method they want, so they'd know which name to call. Having the language allow overloading just lets us avoid having to think up and remember several similar names.

ITEC120-ibarlandinfolectureslabs

Review

2006.Oct.12 (Thu)

We've covered a lot of Java syntax over the last seven weeks. Here's a not-quite-complete list of what we've covered:

The book's Appendix L has a complete description of Java's syntax. It gives it in a more visual format (its circular boxes correspond to our code font, its square boxes correspond to our nonterminal font, its loops correspond to our “…” (“0 or more”), its splitting-paths options correspond roughly to our | “or”, and its path-which-optionally-bypasses-a-section corresponds to our [bracketed-optional-parts].


1We'll later realize that we really should compare the relative size of the two numbers: 0.001 might be good for comparing people's height in inches, but not for the width of two hairs in inches.      back

2

In other languages, there might be other ways to call a method besides statically declaring each parameter. For example, you can imagine a function to create a window on the screen; this function might always require a title for the window, but would give it a default size unless you indicate otherwise: createWindowFrame( "window-title", height: 30, width: 30 ) There might further be 43 other (more obscure) parameters which could also be included via keyword.

If your language doesn't provide this support built-in, you could fake it, by having your functions accept a list of keyword/value pairs, and then your function walks through that list to extract the values passed in. (At least, once, we talk about lists, we can do this.)

     back

3declaring a field and declaring a local variable have very similar syntax, (one occurs at the top of a class, the other inside a method). But keep in mind that they are different.      back

ITEC120-ibarlandinfolectureslabs


©2006, Ian Barland, Radford University
Last modified 2006.Oct.12 (Thu)
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Powered by PLT Scheme