Need help with some code, It is supposed to add two integers using only recursion, cannot use any operators such as (+,-,/,*,%) can use -- and ++, i got it working for multiplying positive numbers but it only works with negatives sometimes, when sending in
4,4 = 16 Check
-4,4 = -16 Check
4,-4 = 16 Wrong
-4,-4 = -16 Wrong
I can't seem to figure out a way to make it work in all cases.
import javax.swing.*;
import java.lang.*;
public class Recursion{
public static void main(String[] args)
{
initialize();
gatherAndCall();
}
private static int tot = 0;
private static int divide = 0;
private static int count = -1;
public static void initialize()
{
int tot = 0;
int divide = 0;
int count = -1;
}
public static void choose()
{
String[] choose = {"continue","exit"};
String options = (String) JOptionPane.showInputDialog(null, "Please select an option below...",
"Toggle Display", JOptionPane.QUESTION_MESSAGE, null, // icon
choose, // Array of choices
choose[0]); // Initial choice
if(options.equals("continue"))
{
initialize();
tot = 0;
gatherAndCall();
}
if(options.equals("exit"))
System.exit(0);
}
public static void gatherAndCall()
{
String[] choose = {"add","subtract","multiply","divide","mod","all"};
String options = (String) JOptionPane.showInputDialog(null, "Please select an option below...",
"Toggle Display", JOptionPane.QUESTION_MESSAGE, null, // icon
choose, // Array of choices
choose[5]); // Initial choice
String numbers =JOptionPane.showInputDialog("Please Enter Two Number Seperated By A Comma: ");
int loc = numbers.indexOf(',');
String sub1 = numbers.substring(0,loc);
String sub2 = numbers.substring(loc+1);
int a = Integer.parseInt(sub1);
int b = Integer.parseInt(sub2);
if(options.equals("add"))
System.out.println(a+" Plus "+b+" = " +Recursion.add(a,b));
if(options.equals("subtract"))
System.out.println(a+" Minus "+b+" = " + Recursion.subtract(a,b));
if(options.equals("multiply"))
System.out.println(a+" Times "+b+" = " + Recursion.multiply(a,b));
if(options.equals("divide"))
if(Recursion.divide(a,b)==12321)
System.out.println("You divided by Zero");
else{
System.out.println(a+" Divided "+b+" = " +Recursion.divide(a,b));
}
if(options.equals("mod"))
System.out.println(a+" Mod "+b+" = " +Recursion.mod(a,b));
if(options.equals("all"))
{
System.out.println(a+" Plus "+b+" = " +Recursion.add(a,b));
System.out.println(a+" Minus "+b+" = " +Recursion.subtract(a,b));
System.out.println(a+" Times "+b+" = " +Recursion.multiply(a,b));
System.out.println(a+" Divided "+b+" = " +Recursion.divide(a,b));
System.out.println(a+" Mod "+b+" = " +Recursion.mod(a,b));
}
choose();
}
public static int add(int a, int b)
{
if(a <= 0)
return (a < 0) ? add(MCH.succ(a), MCH.pred(b)) : b;
else
return (a > 0) ? add(MCH.pred(a), MCH.succ(b)) : b;
}
public static int subtract(int a, int b)
{
if(b <= 0)
return (b < 0) ? subtract(MCH.succ(a), MCH.succ(b)) : a;
else
return (b > 0) ? subtract(MCH.pred(a), MCH.pred(b)) : a;
}
public static int multiply(int a, int b)
{
if (b==0)
return(tot);
else
{
tot = add(tot,a);
if(b<0)
b=MCH.succ(b);
else
b=MCH.pred(b);
multiply(a,b);
}
return (tot);
}
}
Thanks for any help also if any part of the code looks really inefficient and you know a better method please let me know thanks.