CPSC 124: Lab 4

More about Methods

OUT IN THE REAL WORLD, there is a lot of interest in Java and a lot of information about Java. If you have not already done so, you might want to check out some of the links on the CS124 information page. For example, you'll find a link to an online magazine, JavaWorld, all about Java. There's also a link to Gamelan, which is a huge collection of Java resources, including many sample applets.

The official source of online information about Java is the Java page at Sun MIcrosystems, where Java was invented. Since this is a very busy site, I've put some of the same information on our local Web server. Probably, the most important part of the available documentation is the Java API Documentation. Recall that API stands for "Application Programming Interface.". The Java API is a collection of classes, including many methods, that can be used to write application programs in Java.

The Java API is divided into packages. Each package includes classes relevant to some aspect of Java programming. For example, the package java.awt includes all the classes that are needed for writing programs with a graphical user interface, including things like buttons, menus, and images. The package java.lang defines basic classes such as String and Math. The documentation contains a list of the methods in each class, with a short description of what each method does. The API is very large and complex, but by now you know enough about classes and objects that you can begin to explore it. You'll be using classes from java.awt starting next week.

The actual work for today's lab is to write a program that uses subroutines. There is only one exercise for this lab. The program that you should write is described below. Three versions of the program are described, of increasing difficulty. If you do just the first version, the maximum grade you can get on the lab is a B (17 out of 20 points). If you do the second version, you can get up to an A (19 out of 20). For a possible grade of 20 out of 20, you should do the third version.

As always when you write a program, part of your grade depends on your programming style. Among other things, this means:

Exercise, version 1

Time can be measured in many different units: seconds, minutes, days, weeks, fortnights, years, centuries. Write a program that will read a time measurement entered in the form of a number followed by a unit of measure (such as "2.13 days"). The program should then convert the time measurement to a variety of other units, and it should print out a list of the results. For example, if the user entered 2.13 days, the computer would respond by printing:

           184032.0   seconds
             3067.2   minutes
              51.12   hours
               2.13   days
            0.30429   weeks
            0.15215   fortnights
            0.00583   years
           5.832E-5   centuries

(The number on the last line is an example of scientific notation. It is equivalent to 5.832 times 10-5. There is no way for you to avoid having very large and very small real numbers print out in scientific notation.)

The program should not just process one measurement. It should continue reading and processing time measurements until the user enters a zero. The best way to organize things is to convert whatever measurement the user enters into seconds. Then you can use the number of seconds as a basis for computing and printing out all the other values. An outline for the main program could be:

             do {
                  read a number
                  if (the number is not 0) {
                     read a string giving the unit of measurement
                     convert the measurement to seconds
                     print out converted measurements
             } while (the number is not 0);

You are required to use a subroutine to convert the input measurement into seconds. This subroutine should have two parameters--one of type double and one of type String. It should have a return value of type double that gives the number of seconds. You are also required to use a subroutine to print out all the converted measurements.

You should read the unit of measurement using the routine console.getWord(), which reads a sequence of non-blank characters but stops reading when it gets to a blank space, tab, or end-of-line.

Your program should accept both singular and plural versions of the units of measurement, such as "day" and "days". It would be nice if it would also accept abbreviations such as "min" for "minutes". You are not required to check for illegal input, such as "42 bananas".

Recall from Section 2.8 that you should never use the == operator to compare strings. Instead, compare them with the method equalsIgnoreCase(), which is discussed in Section 2.8.

Here are some rules you can use for the conversions:

           1 minute    =   60 seconds
           1 hour      =   60 minutes
           1 day       =   24 hours
           1 week      =   7 days
           1 fortnight =   2 weeks
           1 year      =   365.25 days
           1 century   =   100 years

Exercise, version 2

Of course, it would be nice to be able to enter a time measurement in a combination of units of measurement, such as "1 day 13 hours 5 minutes". If you want to do version 2 of the program, you should allow the user to enter measurements in this form. The input should consist of alternating numbers and units of measurement, all on one line of input. Don't try to enforce any other rules. For example, the input "3 days 1 second 5 days 1 day" is OK.

To do this program, you have to have some way of detecting the end of a line of input. Otherwise, you have no way of knowing when to stop reading the input. Unfortunately, I didn't provide a simple method for checking ends-of-line when I wrote the Console class. Here is a boolean-valued function that you can type into your program and use for this purpose:

        static boolean endOfLine() {
              // Returns true if there are no more non-blank
              // characters on the current line of input; returns
              // false if the next non-blank character is the
              // end-of-line.  This function assumes that 
              // console is a global variable in the class.
           while (console.peek() == ' ' || console.peek == '\t') {
           if (console.peek() == '\n')
              return true;
              return false;

Exercise, version 3

Why limit your program to measurement of time? For the most advanced version of your program, extend your program so that it will accept measurements of either time or distance. Distance measurements can be expressed in inches, feet, yards, rods, furlongs, and miles. The rules of conversion are:

             1 foot      =    12 inches
             1 yard      =     3 feet
             1 rod       =  16.5 feet
             1 furlong   =   220 yards
             1 mile      =     8 furlongs

The program should accept inputs such as "3 miles 1 furlong 7 feet" as well as "12 days 3.7 hours". If the input is a distance measurement, the program should print out the measurement converted to inches, feet, yards, rods, furlongs, and miles. If it is a time measurement, then the program should convert the measurement into the various units of time. You can assume that the user will not mix time measurements with distance measurements.

If you really want to go crazy, you could also make your program accept velocity measurements using the word "per", as in "55 miles per hour" or "2.5 furlongs per fortnight".

[ Lab Index | Online Notes ]