Article View: aus.computers.java
Article #215Re: upper bound and lower bound? - 4 excercises for the keen
From: Paul Murray
Date: Thu, 01 Nov 2001 08:58
Date: Thu, 01 Nov 2001 08:58
89 lines
5643 bytes
5643 bytes
You add one to the multiplier to extend the range. The problem is one you come across often in computing, and it's a source of "off-by-one" bugs. When a person says "3", do they mean "3", or do they mean "from 3 up to but not including 4"? A common place where this happens is in dates. A date is stored as an instant in time, usually the stroke of mignight at the start of the day. Say a person rents a car from the first of the month to the second. Thats (2-1)=1 day, right? Well, no. That's becase those dates are actually counted to include the *entire* day. It's not from the stroke of midnight on the first to the stroke of mignight on the second. The rental is actually calculated as "up to but not including" the third of the month. You have to add one. Same with these random numbers. We truncate the number to an int. In order to get (say) a 5, we need the floating point number produced by the multiplication to be anywhere in the range 5 up to but not including 6. If we want numbers ranging from one to two, then the range must be (2-1) plus one. The way I usually work thee out is to think of the case where there is just one or two. To get a floating point number which truncates to zero (ie, in the range 0-0), we multiply by one. so the multiplier is (b-a)+1. Another common way to visualise these problems is with a number line. You draw a line on paper, mark dates on it and scribble in ranges. *************************** Exersise 1: Here's a typical problem: Rental 1 runs from dates a1 to b1. Rental 2 runs from dates a2 to b2. By how many days (if any) do these rentals overlap? If they do not overlap, how many days gap is there between them? Do the rentals "butt up" against each other? Write these, taking the "day" as an int (to make it easier): int daysOverlapping(int a1, int b1, int a2, int b2); // return 0 if no ovelap int daysSeparating(int a1, int b1, int a2, int b2); // return 0 if no gap boolean areConsecutive(int a1, int b1, int a2, int b2); You may assume that a1<=b1 and a2<=b2, but apart from this the days might be anything. Exersise 2: For an extra challenge, use dates as parameters and return *calendar days*. You may assume that the dates being passed in have no time component. Don't just subtract the dates and divide by seconds-in-a-day. You will potentially get the wrong thing if the rental extends over the change to/from daylight savings time. Exersise 3: Same again, but as this is a rental agency, the cars may be picked up and returned at any time during the day. The rental contract states that you pay for every day "or part thereof", so calculate the above based on the day counting as the whole day in which the date occours. Exercise 4: Rentals returned before 8:30AM do not incur a charge for the day in which they are returned, unless they were rented earlier that same day (never happens, but you have to code for it). Rentals returned after 5:30 PM incur the extra day, unless they were rented after 5:30 (also never happens, but you still have to code for it). I say you have to code for pickup-and-return before 8:30 or after 5:30 on the same day, because even though your client tells you it "never happens", they are lying to you. Trust me. It "never happens" until the day it does happen. There might be only one such rental in the entire database, but you mustn't let your code crash on account of it. Any working programmer on this group will tell you - you get this problem (or variants of it) *all the time*. If it's not days, it's months. Or "90-day periods" (excluding public holidays & weekends). Most coding is business coding, and much of that involves working out how much money peope owe for services rendered. If you are not one of the lucky few working at Sun or a games outfit, this might be what you spend your career doing. Sorry to bleak you out. __________________________________________________ Spot the difference! Jer 48:10 A curse on him who is lax in doing the Lord's work! A curse on him who keeps his sword from bloodshed! Mk 16:16 He that believeth and is baptized shall be saved; but he that believeth not shall be damned. Quran 2 And kill them wherever you find them, ... if they do fight you, then slay them; such is the recompense of the unbelievers. http://www.users.bigpond.com/pmurray .
Technical Details
Message-ID:
<ocFE7.15248$g8.25512@newsfeeds.bigpond.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!newsfeed.dacom.co.kr!intgwlon.nntp.telstra.net!newsfeeds.bigpond.com!not-for-mailReferences:
<kefD7.19$O64.922@news1.mts.net> <%EOD7.227$lh4.2458@newsfeeds.bigpond.com> <9rpm3a$944$1@pulp.srv.ualberta.ca>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 113ae9,1f76e590590fcd45 X-Google-Attributes: gid113ae9,public X-Google-Thread: 10d6db,ef50a28c79cd019f,start X-Google-Attributes: gid10d6db,public X-Google-ArrivalTime: 2001-11-02 14:37:52 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!supernews.com!newsfeed.direct.ca!look.ca!newsfeed.dacom.co.kr!intgwlon.nntp.telstra.net!newsfeeds.bigpond.com!not-for-mail From: "Paul Murray" <pmurray@nospam.bigpond.com> Newsgroups: aus.computers.java,comp.lang.java.help References: <kefD7.19$O64.922@news1.mts.net> <%EOD7.227$lh4.2458@newsfeeds.bigpond.com> <9rpm3a$944$1@pulp.srv.ualberta.ca> Subject: Re: upper bound and lower bound? - 4 excercises for the keen Lines: 93 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.50.4807.1700 X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4807.1700 Message-ID: <ocFE7.15248$g8.25512@newsfeeds.bigpond.com> Date: Thu, 1 Nov 2001 19:58:03 +1100 NNTP-Posting-Host: 203.54.250.105 X-Trace: newsfeeds.bigpond.com 1004740500 203.54.250.105 (Sat, 03 Nov 2001 09:35:00 EST) NNTP-Posting-Date: Sat, 03 Nov 2001 09:35:00 EST Organization: Telstra BigPond Internet Services (http://www.bigpond.com)