I have everything done except for the areas in bold i am having issues with them
ID: 3711899 • Letter: I
Question
I have everything done except for the areas in bold i am having issues with them.
Multiplying two integers, a * b, where a is the multiplicand, b is the multiplier, is actually done by adding the first number, a, to itself b times.
Dividing two numbers, a/b, where a is the divident, b is the divisor, is likewise actually done by subtracting the second number, b, from the first number, a, repeatedly until a is smaller than b (this is the remainder). The number of subtractions done is the number of times b goes into a.
Write two iterative methods, one for multiplication by addition and one for division by subtraction.
Write a recursive method that will multiply two numbers by adding the first to itself this way.
Write a second recursive method that will divide two numbers this way.
After testing all methods for accuracy, run the two multiply methods repeatedly, increasing the factors by a factor of ten until your program experiences failure. Quit running the method that failed, but continue running the method that did not fail, still increasing the factors by a power of ten until it, too, fails
Partial Pseudo-code:
In main
Create a loop so you can call your recursive and iterative methods lots of times and get a better picture of the performance difference.
Write a small driver that tests each of the four methods (exhaustively)
After testing, run a loop that
prompts for either the two multiply or the two divide methods
prompts for two integers
runs the selected methods
a * b
In multiply (recursive)
if a or b is zero
return zero
else
subtract 1 from b
return a + (multiply with new parameters)
a/b
In divide (recursive)
If b is zero, throw an exception
if a is less than b
return 0
else
subtract b from a
return 1 + (divide with new parameters)
Explanation / Answer
import java.util.Scanner;
public class Driver {
public static int IterativeMultiplication(int a, int b) {
int sum = 0;
for(int i = 0; i < b; i++)
sum += a;
return sum;
}
public static int RecurisveMultiplication(int a, int b) {
if (a == 0 || b == 0) {
return 0;
}
else if( b < 0 ) {
return - a + RecurisveMultiplication(a, b + 1);
}
else {
return a + RecurisveMultiplication(a, b - 1);
}
}
public static int IterativeDivison(int x, int y)
{
int c = 0;
if (y == 0)
return 0;
while (x >= y)
{
x = x - y;
c++;
}
return c;
}
public static int RecursiveDivison(int x, int y)
{
if( y == 0 )
return 0;
else if(x-y == 0)
return 1;
else if( x < y)
return 0;
else
return ( 1 + RecursiveDivison(x-y, y));
}
public static void main(String[] args){
int a, b;
Scanner in = new Scanner(System.in);
while(true) {
a = in.nextInt();
b = in.nextInt();
System.out.println("1. Multiplication 2. Division 3. Exit Your Choice: ");
int choice = in.nextInt();
if(choice == 1) {
System.out.println("Iterative Multiplication = "+IterativeMultiplication(a, b));
System.out.println("Recursive Multiplication = "+ RecurisveMultiplication(a, b));
}else if(choice == 2) {
System.out.println("Iterative Division = "+IterativeDivison(a, b));
System.out.println("Recursive Division = "+ RecursiveDivison(a, b));
}
else
break;
}
}
}
**Comment for any further queries.