CS 214 Project 4: Using Repetition


This week's project is to use the equivalent of a C++ for construct in each of our four languages. More precisely, here is a C++ function and program that, given an integer n, returns n!.

   #include <iostream>
   using namespace std;

   double factorial(unsigned n) {
      double answer = 1.0;

      for (int i = 2; i <= n; i++) {
         answer *= i;
      }

      return answer;
   }


   int main() {
      cout << "\nTo compute n!, enter n: ";
      unsigned n;
      cin >> n;

      cout << n << "! = " << factorial(n) << endl;
   }

Your project is to implement the equivalent program in Java, Ada, Clojure, and Ruby. Each program should have a subprogram that computes n!, and an interactive "driver" that handles I/O and invokes that subprogram.

Don't forget the documentation!

Java

Write a Java method that computes n! using a for loop, as described in the lab exercise.

Ada

Since this is an integer counting problem, you should compute n! using the counting version of the loop construct , as described in the lab exercise.

Clojure

Compute n! by designing and implementing a recursive function, or a non-recursive function that uses Clojure's recursive loop() function.

Ruby

Write a Ruby method that computes n! using a for loop, as described in the lab exercise.

Turn in. Using an approach like what we did in the lab, make a single script file named proj04-results, in which you list each program and demonstrate its correctness. Then submit your project by copying that single file into your personal folder in /home/cs/214/current/:

   cp proj04-results /home/cs/214/current/yourUserName
replacing yourUserName with your login name. The grader will access and grade your project results from there, using the criteria from this grade sheet.


Calvin > CS > 214 > Projects > 04


This page maintained by Joel Adams.