BasicMath

Programming Assignment 5

Due: 8pm, Monday 22 March 2004


Contents:


Overview

The Java language, with its supporting libraries, offers support for many arithmetic computations. However, in this assignment we are going to have you re-invent the wheel in this regard, showing how many computations can be performed based on the use of simpler computations.

For example, had Java provided us with the increment operator, ++, yet not with addition, it would have been enough. Addition by a natural number, n, could be implemented by incrementing n times.

Had Java provided us with addition, yet not with multiplication, it would have been enough. Multiplying by a natural number, n, could be implemented by adding n copies of the original number.

You get the point...

Newly Introduced Techniques


Collaboration Policy

For this assignment, you must work individually in regard to the design and implementation of your project. Please make sure you adhere to the policies on academic integrity.


Files You Will Need

We have placed a copy of the template files for this assignment, BasicMath, in each of your home directories on patel2.slu.edu.

We start you with two classes in the project:


Your Task

Your task will be to implement the following methods in the provided class, BasicMath. Note Well: for the first four of these methods, you may assume that the parameters are non-negative integers (though it may still be interesting to see how your implementation behaves if the user enter negative values).


Submitting Your Assignment

Please see details regarding the submission process from the general programming web page, as well as a discussion of the late policy.

Grading Standards

The assignment is worth 10 points. The primary criteria will be that your routines work correctly, and that you obey the constraints in regard to the allowable operations for each method.


Extra Credit

Write code for an additional method:
void primeFactorization(int N)

This method should compute the prime factorization of an integer N, explicitly printing out the result in a form such as:

1960 = 2^3 * 5^1 * 7^2

Parameters:
N - the given value (assumed to be positive);

Advice: In approaching the task, there are many possible approaches. Pages 314-318 of the text are loosely related, though not directly applicable to computing the factorization. We suggest the following high-level algorithm:

Initialize integer p to 2.
So long as p divides N:
   Set N = N/p.
Let t represent the number of factors of p which were found during the previous step.
If t>0, then add text such as, p^t, to the output.
Increment p
Repeat until N==1.
This is not necessarily the most efficient approach, but it should work well for reasonably sized values of N. You may notice that incrementing the counter p means that sometimes p will not be prime. However, if you've implemented everything correctly, no composite factors will be reported because you would have removed all of the smaller prime factors in an earlier pass.

Note: To check whether a given integer p evenly divides an integer N, use the modulus operator, namely check whether (N%p)==0.

Finally, generating nicely formatted output takes additional care. You can make use of the method System.out.print, which generates output without a following newline (as opposed to System.out.println). Additional logic must be used, for example, in getting the asterisks to appear correctly.


Michael Goldwasser
Last modified: Monday, 15 March 2004