package PS4; import javax.swing.*; public class RecursionTest { private static int count; public static void main(String[] args) { int z; String input= JOptionPane.showInputDialog("Enter x"); int x= Integer.parseInt(input); input= JOptionPane.showInputDialog("Enter y"); int y= Integer.parseInt(input); z= MultResult(x, y); // Swap so y is smallest, if desired System.out.println(x + " * " + y + " = " + z); System.out.println("Number of additions: " + count); System.exit(0); } public static int MultResult(int x, int y) { int result; boolean isNegative= false; count++; if (y < 0) { y= -y; isNegative= true; } if (x== 0 || y == 0) // x or y small enough result= 0; else if (y == 1) // y small enough result= x; else if (y % 2 == 0) { // y even int term= MultResult(x, y/2); // Don't do 2 recursive calls! result= term + term; } // MultResult(x, y/2) + MultResult(x, y/2) else // y odd result= x + MultResult(x, y-1); // Add println to trace if (isNegative) result= -result; return result; } }