Jump to content

9 posts in this topic

Posted

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:
[CODE]
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]

Share this post


Link to post
Share on other sites

Posted

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.

Share this post


Link to post
Share on other sites

Posted

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.

Share this post


Link to post
Share on other sites

Posted

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.


[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 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]

Share this post


Link to post
Share on other sites

Posted

Oh boy. Recursion. Don't you just love how they make you do seemingly pointless stuff with it?

But it is useful and extremely powerful.

Share this post


Link to post
Share on other sites

Posted

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.

Share this post


Link to post
Share on other sites

Posted

[quote name='Billaguana' timestamp='1320336753' post='594429171']
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.
[/quote]

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

Share this post


Link to post
Share on other sites

Posted

[quote name='Billaguana' timestamp='1320336753' post='594429171']
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.
[/quote]
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.

Share this post


Link to post
Share on other sites

Posted

Oh, I see. Thank you for the clearification.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.