• 0

[Java] doing math with no symbols


Question

For my Comp Sci class, we have to create a program which can add, subtract, multiply, divide, and mod without the use of any type of loop or the symbols besides a provided class named MCH. The point is the implementation of recursion. I have already got them all to work for positive numbers, but I am having some trouble with making them all work with negatives. I have add and subtract working, but honestly I hate recursion and would rather use for loops. The code is below:

MCH:


public class MCH
{
public static int pred(int num)
{
return --num;
}

public static int succ(int num)
{
return ++num;
}
}
[/CODE]

MathClass:

[CODE]
import javax.swing.*;

public class MathClass
{
public static void main(String[] args)
{
for(;;)
{
int num1 = Integer.parseInt(JOptionPane.showInputDialog("Number 1: "));
int num2 = Integer.parseInt(JOptionPane.showInputDialog("Number 2: "));

int sum = add(num1,num2);
int diff = subtract(num1,num2);
int prod = multiply(num1,num2);
int quot = divide(num1,num2);
int rem = mod(num1,num2);

System.out.println(num1 + " + " + num2 + " = " + sum);
System.out.println(num1 + " - " + num2 + " = " + diff);
System.out.println(num1 + " * " + num2 + " = " + prod);
System.out.println(num1 + " / " + num2 + " = " + quot);
System.out.println(num1 + " % " + num2 + " = " + rem);
}
}

public static int grabANumber()
{
int x = Integer.parseInt(JOptionPane.showInputDialog("Enter a number: "));
return x;
}

public static int add(int num1,int num2)
{
if(num1 == 0)
return num2;

if(num2 == 0)
return num1;

if((num1 > 0 && num2 < 0) || (num1 < 0 && num2 < 0))
return subtract(num1, Math.abs(num2));
else
return add(MCH.succ(num1), MCH.pred(num2));
}

public static int subtract(int num1,int num2)
{
if(num1 == 0)
return num2;

if(num2 == 0)
return num1;

if((num1 > 0 && num2 < 0) || (num1 < 0 && num2 < 0))
return add(num1, Math.abs(num2));
else
return subtract(MCH.pred(num1), MCH.pred(num2));
}

public static int multiply(int num1,int num2)
{
if(num1 == 0)
return 0;

if(num2 == 0)
return 0;

if(num1 > 0 && num2 < 0)
return add(multiply(num1, MCH.succ(num2)), num1);
else
return add(multiply(num1, MCH.pred(num2)), num1);
}

public static int divide(int num1,int num2)
{
if(num1 == 0)
return 0;

if(num2 == 0)
return 0;

if(num1 < num2)
return 0;

return add(1, divide(subtract(num1, num2), num2));
}

public static int mod(int num1,int num2)
{
return subtract(num1, multiply(num2, divide(num1, num2)));
}
}
[/CODE]

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

If your problem is with division and multiplication with negative numbers just do the operation the same way as positive and then do sub(0,result) to change it to a negative if you must.

You can also do it with bitwise operators if allowed which would be way faster.

Link to comment
Share on other sites

  • 0

ok, but because it is recursion how do I tell it to only convert the ending integer on the last way out in the recursion process and to see if it is supposed to be negative.

Link to comment
Share on other sites

  • 0

Solved. 3 hours of work and 2 brains but we managed to have it work for every possible solution. Also, mod doesn't ave to work for negatives.


import javax.swing.*;

public class MathClass
{
public static void main(String[] args)
{
for(;;)
{
int num1 = Integer.parseInt(JOptionPane.showInputDialog("Number 1: "));
int num2 = Integer.parseInt(JOptionPane.showInputDialog("Number 2: "));

int sum = add(num1,num2);
int diff = subtract(num1,num2);
int prod = multiply(num1,num2);
int quot = divide(num1,num2);
int rem = mod(num1,num2);

System.out.println(num1 + " + " + num2 + " = " + sum);
System.out.println(num1 + " - " + num2 + " = " + diff);
System.out.println(num1 + " * " + num2 + " = " + prod);
System.out.println(num1 + " / " + num2 + " = " + quot);
System.out.println(num1 + " % " + num2 + " = " + rem);
}
}

public static int grabANumber()
{
int x = Integer.parseInt(JOptionPane.showInputDialog("Enter a number: "));
return x;
}

public static int add(int num1,int num2)
{
if(num1 == 0)
return num2;

if(num2 == 0)
return num1;

if((num1 < 0 && num2 > 0) || (num1 < 0 && num2 < 0))
return add(MCH.succ(num1), MCH.pred(num2));
else
return add(MCH.pred(num1), MCH.succ(num2));
}

public static int subtract(int num1,int num2)
{
if(num1 == 0)
return num2;

if(num2 == 0)
return num1;

if(num2 < 0)
return add(num1, Math.abs(num2));
else if((num1 < 0 && num2 > 0) || (num1 < 0 && num2 < 0))
return subtract(MCH.succ(num1), MCH.succ(num2));
else
return subtract(MCH.pred(num1), MCH.pred(num2));
}

public static int multiply(int num1,int num2)
{
if(num1 == 0)
return 0;

if(num2 == 0)
return 0;
if(num1 < 0 && num2 < 0)
return multiply(Math.abs(num1), Math.abs(num2));
else if(num1 < 0 && num2 > 0)
return add(multiply(num1, MCH.pred(num2)), num1);
else if(num1 > 0 && num2 < 0)
return add(multiply(num2, MCH.pred(num1)), num2);
else
return add(multiply(num1, MCH.pred(num2)), num1);
}

public static int divide(int num1,int num2)
{
if(num1 == 0)
return 0;

if(num2 == 0)
return 0;

if(Math.abs(num1) < Math.abs(num2))
return 0;

if(num1 < 0 && num2 < 0)
return divide(Math.abs(num1), Math.abs(num2));
else if(num1 < 0 && num2 > 0)
return add(-1, divide(add(num1, num2), num2));
else if(num1 > 0 && num2 < 0)
return add(-1, divide(add(num1, num2), num2));
else
return add(1, divide(subtract(num1, num2), num2));
}

public static int mod(int num1,int num2)
{
return subtract(num1, multiply(num2, divide(num1, num2)));
}
}
[/CODE]

Link to comment
Share on other sites

  • 0

I guess that this is the only way they can teach it, lol. I just can't see a scenario when what you try to do in a recursion loop can't be done in a for or while loop.

Link to comment
Share on other sites

  • 0

I guess that this is the only way they can teach it, lol. I just can't see a scenario when what you try to do in a recursion loop can't be done in a for or while loop.

Because there are cases where it just makes sense, for instance recursing through a tree.

Link to comment
Share on other sites

  • 0

I guess that this is the only way they can teach it, lol. I just can't see a scenario when what you try to do in a recursion loop can't be done in a for or while loop.

There are languages with no for/while loops so the only way to create a loop is recursion.

Plus some problems are naturally recursive so it makes more sense to solve them with recursion.

i.e. calculate the binomial coefficients (there is a recursive type) or factorial of a number.

Link to comment
Share on other sites

This topic is now closed to further replies.