I've been working on an assingment that requires us to get fractions to multiply,divide,subtract, and add. I'm pretty that part of my code works, but when I go to run it I get an error that I don't understand how to fix. Can anyone explain what the error means and what should I do to solve it. I've tried making everything equal to num and den, but I think that is causing the problem.
There is an error about something being divisible by zero. The fractionnum and den should be delete and everything is equal to num and den.
[import java.util.*;
import java.util.Scanner;
public class TextLab05 {
/**
* @param args
*/
// TODO Auto-generated method stub
static int num1, den1; // numerator and denominator of the 1st rational number
static int num2, den2; // numerator and denominator of the 2nd rational number
public static void main (String args[])
{
enterData();
Rational r1 = new Rational(num1,den1);
Rational r2 = new Rational(num2,den2);
Rational r3 = new Rational();
r3.multiply(r1,r2);
System.out.println("\n\n" + r1.getOriginal() + " * " + r2.getOriginal() + " = " + r3.getRational());
r3.divide(r1,r2);
System.out.println("\n" + r1.getOriginal() + " / " + r2.getOriginal() + " = " + r3.getRational());
}
public static void enterData()
{
Scanner input = new Scanner(System.in);
System.out.print("\nEnter the 1st numerator ----> ");
num1 = input.nextInt();
System.out.print("\nEnter the 1st denominator --> ");
den1 = input.nextInt();
System.out.println("\nEnter the 2nd numerator ----> ");
num2 = input.nextInt();
System.out.println("\nEnter the 2nd denominator --> ");
den2= input.nextInt();
}
}
class Rational
{
private int firstNum; // entered numerator
private int firstDen; // entered denominator
private int num; // reduced numerator
private int den; // reduced denominator
int fractionNum;
int fractionDen;
//Rational
public Rational(int n1,int d1)
{
this.firstNum=n1;
this.firstDen=d1;
}
public Rational ()
{
}
// getNum
public int getNum()
{
num=firstNum;
return num;
}
// getDen
public int getDen()
{
den=firstDen;
return den;
}
// getDecimal
public double getDecimal()
{
return (double)num/den;
}
// getRational
public String getRational()
{
return fractionNum+"/"+fractionDen;
}
// getOriginal
public String getOriginal()
{
return firstNum+"/"+firstDen;
}
// reduce
public void reduce()
{
int GCF;
GCF= getGCF(num,den);
fractionNum=num/GCF;
fractionDen= den/GCF;
}
public void displayData()
{
System.out.println();
System.out.println(getNum() + "/" + getDen() + " equals " + getDecimal());
System.out.println();
System.out.println("and reduces to " + getRational());
System.out.println();
}
private int getGCF(int n1,int n2)
{
int rem = 0;
int gcf = 0;
do
{
rem = n1 % n2;
if (rem == 0)
gcf = n2;
else
{
n1 = n2;
n2 = rem;
}
}
while (rem != 0);
return gcf;
}
{
}
public void multiply(Rational r1,Rational r2)
{
num=r1.getNum()*r2.getNum();
den=r1.getDen()*r2.getDen();
reduce();
}
public void divide (Rational r1,Rational r2)
{
num=r1.getNum()*r2.getDen();
den=r1.getDen()*r2.getNum();
reduce();
}
public void add (Rational r1, Rational r2)
{
}
public void subtract (Rational r1, Rational r2)
{
}
{
There is an error about something being divisible by zero. The fractionnum and den should be delete and everything is equal to num and den.