Main Content

First Java Application - calculating the weight of a tablecloth

Archive - Originally posted on "The Horse's Mouth" - 2014-11-29 22:37:41 - Graham Ellis

From last week's Java course - the first practical demonstration program I wrote:
• prompts for user input
• reads that input
• calculates
• outputs results

That's the stage I look to get to quickly on a course to give my delegates some idea early on of how the language goes together - source code in this case is [here].

In Java, reading from the keyboard is reading from a stream for which you have to use an appropriate class. You must catch exceptions which can be of various types, all of which are direct or indirect subclasses of a base class object. If this is starting to feel complex, fear not. "If you want to do something that's commonly done, chances are people have done it before and made it available" ... and indeed I have provided a class (well reader) which hides all this complexity from the newcomer and lets you read numbers easily.

The actual application reads the width and length of a table, and calculates the weight of a table cloth to fit that table, with appropriate overhang. Should you ask "who weighs their table cloths" I will answer "we do" - weighing linen is a very useful way for a commercial cafe or hotel to tell similar but not the same pieces apart once they've been washed and ironed, and our scales are a vital piece of laundry equipment!