-
Recently Browsing 0 members
- No registered users viewing this page.
-
Similar Content
-
Download: Java All-in-One For Dummies, 7th Edition ($27 Value) — free for your email
By News Staff,
- ebook offer
- sponsored
- (and 3 more)
- 0 replies
- 3 views
-
Learn Java with Projects (worth $44.99) but free in exchange for your email
By News Staff,
- packt
- ebook offer
- (and 2 more)
- 0 replies
- 3 views
-
JavaScript from Frontend to Backend ($20.99 Value) FREE for your work email
By News Staff,
- ebook offer
- sponsored
- (and 3 more)
- 0 replies
- 3 views
-
Google Search and Lens add more features to solve math and science problems
By Anushe Fawaz,
- google lens
- (and 7 more)
- 0 replies
- 3 views
-
- 0 replies
- 3 views
-
Question
Billaguana
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:
MathClass:
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