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...
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.
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:
BasicMath
A template for the class which you must write. This is the only
file that you should be modifying.
BasicMathDriver
This is a file which you should not edit. It is the main driver
which gives you a menu-driven dialog for interacting with the
routines of BasicMath.
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).
int addWithIncrement(int x, int y)
x - the first operand (assumed to be non-negative)y - the second operand (assumed to be non-negative)
int multiplyWithAddition(int x, int y)
x - the first operand (assumed to be non-negative)y - the second operand (assumed to be non-negative)
int multiplyWithIncrement(int x, int y)
x - the first operand (assumed to be non-negative)y - the second operand (assumed to be non-negative)
int divideWithSubtraction(int x,
int y)
x - the first operand (assumed to be non-negative)y - the second operand (assumed to be non-negative)
double pi(double accuracy)
PI = 4*(1 - 1/3 + 1/5 - 1/7 + 1/9 - ...)Of course, we can never get the exact value, but we can get an approximate value by computing a partial sum. For this method, carry out as many iterations of the sum as are needed so that your result is within the given accuracy when compared to Java's version of the constant, available as Math.PI (you can also use the method Math.abs to compute the absolute difference between your current approximation and Java's).
Your routine should explicitly print out a sentence stating the number of iterations which were used, and then the actual computed value should serve as the return value.
accuracy - the desired accuracy
double sqrt(double N,
double accuracy)
Exercise 6.24 of the text discusses a technique for computing the square root of a value, based upon Newton's method.
Though we may not be able to get an exact value, we should be able to get an approximate value by carrying out more and more iterations of that technique. To implement this method, carry out enough iterations until the difference between yourComputedValue and (N/yourComputedValue) is less than the desired accuracy.
Your routine should explicitly print out a sentence stating the number of iterations which were used, and then the actual computed value should serve as the return value.
N - the given value (assumed to be non-negative);accuracy - the desired accuracy
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.
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
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.