JamesMadisonUniversity

Computer Science Department


CS 139 Lab:Programming Contest


Objectives:

This lab will give students practice in thinking through an algorithm cleanly and implement it quickly.

Background:

This lab is designed similar to the Programming Contests sponsored by the ACM among others.

New Terms:

No new terms. These problems should all be review.

Materials:

All functions should be written from scratch. All should be stored in a .java file named PC.java. You may download the driver to use to submit your program (Driver.java). All programs should be submitted to the Linux submit system. The Driver uses a command line argument to determine which method it is testing.
Acknowledgment From an original lab by Nancy Harris

1 General Instructions:

Download the Driver.java and the PC.java files to your programming environment.

PC.java is a set of stubs for each of the methods. You may create and test them one at a time. Driver takes in an argument corresponding to the number of the method you are testing (1 is isOdd for example). You can test one method by typing the number in as an argument.

Wait for the GO announcement.

With your partner, choose the first problem that you will tackle.

Thoroughly test your problem.

When you are convinced that you have the correct solution, submit to Linux with the Driver.java program. There will be one submit for each problem.

Correctly solved problems receive 5 points each. For every submit failure deduct 1 point for a maximum of 4 points. (Everyone gets at least 1 point for a successful submit.)

The team with the most points at the end of the hour is declared the winner. Winners receive 100 lab points for this lab - other participants will receive 95 for completing 1 or more problems during the assigned lab time.

2 The Problems - you do not need to do any of these in any particular order.

  1. Write a method called isOdd that will accept an integer parameter and returns true if the integer is odd and false if the integer is even. Consider 0 to be an even number. Your method should work with both positive and negative numbers.

  2. Write a method called isPrime that will accept an integer parameter and returns true if the integer is a prime number and false otherwise. (A prime number is divisible only by itself and 1.) You may disregard negative numbers and 0.

  3. Write a method called gcd that accepts two integer parameters and returns the integer value of the greatest common divisor of the two. You may disregard negative numbers and 0.

  4. Write a method called lcm that accepts two integer parameters and returns the integer value of the smallest number that is a multiple of both. (Note: the lcm of 2 and 4 is 4 while the lcm of 3 and 4 is 12). You may disregard negative numbers and 0.

  5. Write a method called reverse that accepts a String parameter and returns a String that is the reverse of the input String. (for example, abc would return cba).

  6. Write a method called power2 that given a positive integer will print all of the powers of two from 20 to 2n, where n is the passed value. 0 is a legitimate parameter value. Note, you should print the values one per line with no other labelling and no extra new lines.

  7. Write a method called floatEquals that accepts three double numbers; two numbers to compare and one number representing a tolerance. The method should return true if the first two numbers are the same to within the tolerance of the third number. The entire range of float numbers is permissible.

  8. Write a method called isAlpha that accepts a character parameter and returns true if that character is either an uppercase or lowercase alphabetic letter and false otherwise. You MAY NOT use any Character functions.

Updated 09/15/06 (nlh)