1. You’ll need to create four classes with these names: a. MyMath b. MyMathRecur
ID: 3781728 • Letter: 1
Question
1. You’ll need to create four classes with these names:
a. MyMath
b. MyMathRecursive
c. MyMathIterative
d. MyMathTester
2. MyMath must be an abstract class. It must contain the following:
a. One ‘protected’ instance variable of type ‘int’ that will be used for mathematical
calculations.
b. One constructor that initializes the instance variable.
c. One getter method for the instance variable.
d. One abstract method with the following signature:
i. ‘abstract public int factUntil(int x);’
e. The factUntil method calculates factorial on ‘x’ with a caveat. It stops calculating
when it processes either the value 1 or the value equal to the instance variable.
For example, if the instance variable is 5, then
i. factUntil( 7 ) will calculate 7 * 6 * 5 and then stop.
ii. factUntil( 5 ) will calculate 5 and then stop.
iii. factUntil( 4 ) will calculate 4 * 3 * 2 * 1 and then stop.
3. MyMathRecursive and MyMathIterative must not be abstract and must each extend
MyMath. MyMathRecursive must use recursion and MyMathIterative must not use
recursion and must use iteration. Don’t use the built-in Math class.
4. MyMathTester must include only a main method that tests 3 different factUntil
parameters each for 2 different instance variables each for MyMathRecursive and
MyMathIterative. Each ‘test’ must print out the instance variable, ‘x’ parameter, and
result.
Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
public abstract class MyMath {
protected int m;
public MyMath(int m){
this.m = m;
}
public int getM(){
return m;
}
abstract public int factUntil(int x);
}
#########################################
public class MyMathRecursive extends MyMath {
public MyMathRecursive(int m) {
super(m);
}
@Override
public int factUntil(int x) {
if(x == 1)
return 1;
if(x == getM())
return x;
return x*factUntil(x-1);
}
}
########################################
public class MyMathIterative extends MyMath {
public MyMathIterative(int m) {
super(m);
}
@Override
public int factUntil(int x) {
int end = (x >= getM()) ? getM() : 1;
int fact = 1;
for(int i=x; i>=end; i--)
fact = fact*i;
return fact;
}
}
#############################################
public class MyMathTester {
public static void main(String[] args) {
MyMath iterative = new MyMathIterative(5);
MyMath recursive = new MyMathRecursive(5);
System.out.println("Iterative: FactUtil(7): "+iterative.factUntil(7) );
System.out.println("Iterative: FactUtil(4): "+iterative.factUntil(4) );
System.out.println();
System.out.println("Recursive: FactUtil(7): "+recursive.factUntil(7) );
System.out.println("Recursive: FactUtil(4): "+recursive.factUntil(4) );
}
}
/*
Sample output:
Iterative: FactUtil(7): 210
Iterative: FactUtil(4): 24
Recursive: FactUtil(7): 210
Recursive: FactUtil(4): 24
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.