0

So the gist of this program is to create a Rational class so that when you run it, a GUI input will come up and ask for a numerator and a denominator. It will then return the reduced fraction. But I keep getting this error message and I don't know why. Here's the program so far:

import javax.swing.JOptionPane;


public class lab8
{
public static void main (String args[])
{
    String strNbr1 = JOptionPane.showInputDialog("Enter Numerator ");
    String strNbr2 = JOptionPane.showInputDialog("Enter Denominator ");

    int num = Integer.parseInt(strNbr1);
    int den = Integer.parseInt(strNbr2);

    Rational r = new Rational(num,den);
    JOptionPane.showMessageDialog(null,r.getNum()+"/"+r.getDen()+" equals "+r.getDecimal());

    System.exit(0);
}
}



class Rational
{
private int num;
private int den;

public Rational()
{
    num = 0;
    den = 1;
}
public double getNum()
{
    return num;
}

public int getDen()
{
    return den;
}
 }
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

1 Answers1

2

You are trying to call a constructor that doesn't exist. Your class constructor asks for nothing:

public Rational()

while it should ask for 2 ints:

public Rational(int num, int den){
     this.num = num;
     this.den = den;
}

so you can pass both num and den to it as you are trying to do in

Rational r = new Rational(num,den);
Manuel Miranda
  • 823
  • 5
  • 13