Jump to content



Photo

[Java] doing math with no symbols

java math symbols

  • Please log in to reply
8 replies to this topic

#1 Billaguana

Billaguana

    Too tired to stop

  • Joined: 11-May 11
  • Location: Depends who's asking
  • OS: Win 7/8
  • Phone: HTC Trophy

Posted 02 November 2011 - 21:08

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;
}
}

MathClass:
import javax.swing.*;

public class MathClass
{
public static void main(String[] args)
{
  for(;<img src='http://www.neowin.net/forum/public/style_emoticons/<#EMO_DIR#>/wink.png' class='bbc_emoticon' alt=';)' />
  {
  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)));
}
}



#2 gian

gian

    Neowinian

  • Joined: 07-September 04
  • Location: Greece

Posted 02 November 2011 - 21:18

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.

#3 OP Billaguana

Billaguana

    Too tired to stop

  • Joined: 11-May 11
  • Location: Depends who's asking
  • OS: Win 7/8
  • Phone: HTC Trophy

Posted 02 November 2011 - 21:32

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.

#4 OP Billaguana

Billaguana

    Too tired to stop

  • Joined: 11-May 11
  • Location: Depends who's asking
  • OS: Win 7/8
  • Phone: HTC Trophy

Posted 03 November 2011 - 02:36

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(;<img src='http://www.neowin.net/forum/public/style_emoticons/<#EMO_DIR#>/wink.png' class='bbc_emoticon' alt=';)' />
  {
  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)));
}
}


#5 Shadrack

Shadrack

    Neowinian Senior

  • Tech Issues Solved: 3
  • Joined: 20-December 01

Posted 03 November 2011 - 02:54

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.

#6 OP Billaguana

Billaguana

    Too tired to stop

  • Joined: 11-May 11
  • Location: Depends who's asking
  • OS: Win 7/8
  • Phone: HTC Trophy

Posted 03 November 2011 - 16:12

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.

#7 Lant

Lant

    Neowinian Senior

  • Joined: 13-April 06

Posted 03 November 2011 - 16:29

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.

#8 gian

gian

    Neowinian

  • Joined: 07-September 04
  • Location: Greece

Posted 03 November 2011 - 17:11

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.

#9 OP Billaguana

Billaguana

    Too tired to stop

  • Joined: 11-May 11
  • Location: Depends who's asking
  • OS: Win 7/8
  • Phone: HTC Trophy

Posted 03 November 2011 - 22:39

Oh, I see. Thank you for the clearification.