My task is to code an applet that should take 2 fractions and either add,
subtract, multiply, or divide them (depending on which JButton the user
uses). Then, the fraction is to be converted to lowest terms. Also the
fractions will be entered in in terms of 2/5 for "two-fifths".

All of the calculations should be computed using basic methods. As you can
see, I have some of them already in there.

I have most of the code done but I can't seem to get past this runtime
error. This code will compile, but once I hit any one of the JButtons I
get the same runtime error.
Go ahead and compile it, and run the applet to see what I am talking
about.

Also if you guys want to push me any insight on the multiply and divide
methods, I would appreciate it.

The spacing got screwey when i cut/pasted it here, so please bear with me.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;


public class Fractions extends JApplet implements ActionListener
{
JLabel firstFraction, secondFraction, answerLabel;      // Declares Labels
JTextField firstField, secondField, answerResults;      // Declares Fields
JButton add, mult, sub, div;                                    // Declares Buttons


public void init ()     // Start GUI setup
{
setSize(350, 200);
Container c = getContentPane();
c.setLayout(new FlowLayout () );


firstFraction = new JLabel( "Fraction 1 ");
c.add(firstFraction);
firstField = new JTextField(25);
c.add (firstField);


secondFraction = new JLabel( "Fraction 2 ");
c.add(secondFraction);
secondField = new JTextField(25);
c.add (secondField);


add = new JButton("+");
add.addActionListener(this);
c.add(add);


sub = new JButton("-");
sub.addActionListener(this);
c.add(sub);


mult = new JButton("*");
mult.addActionListener(this);
c.add(mult);


div = new JButton("/");
div.addActionListener(this);
c.add(div);


answerResults = new JTextField(20);
answerResults.setEditable(false);
c.add(answerResults);
}                               // End GUI setup


public void actionPerformed(ActionEvent e)
{
int numerator1 = ( getNumerator ( firstFraction.getText () ) ); // Gets Numerator of first fraction entered by user.
int denomerator1 = ( getDenominator ( firstFraction.getText () ) ); // Gets Denomerator of first fraction entered by user.
int numerator2 = ( getNumerator ( secondFraction.getText () ) );    // Gets Numerator of second fraction entered by user.
int denomerator2 = ( getDenominator ( secondFraction.getText () ) );    // Gets Denomerator of second fraction entered by user.


if ( e.getSource() == add )
{
addFractions(numerator1, numerator2, denomerator1, denomerator2);
}
else if ( e.getSource () == sub )
{
subtractFractions(numerator1, numerator2, denomerator1, denomerator2);
}
}
public void subtractFractions(int numerator1, int numerator2, int denomerator1, int denomerator2)
{
int numeratorsubtract = ( numerator1 * denomerator2 +( numerator2 * (-1))* denomerator1 );
int denomeratorsubtract = ( denomerator1 * denomerator2);
answerResults.setText ("" + numeratorsubtract +"/" + denomeratorsubtract );
}
public void addFractions(int numerator1, int numerator2, int denomerator1, int denomerator2)
{
int numeratoradd = ( numerator1 * denomerator2 + numerator2 * denomerator1 );
int denomeratoradd = (denomerator1 * denomerator2);
answerResults.setText ("" + numeratoradd +"/" + denomeratoradd);
}
public int getNumerator(String fraction)
{
int numerator = fraction.indexOf("/");
return ( Integer.parseInt ( fraction.substring( 0, numerator ) ) );


}
public int getDenominator(String fraction)
{
int denominator = fraction.indexOf("/");
return ( Integer.parseInt ( fraction.substring( denominator + 1, fraction.length() ) ) );
}
}

You only work with strings explicitly in getNumerator and getDenominator, so those should be your first targets for debugging. I notice that you make a lot of assumptions about the strings in question. indexOf can return -1, which would be a bad thing, and the input could be malformed, which would also be a bad thing.

Find out what line your execution is bombing out on and I'll offer a few more suggestions. 70% of programming is debugging, and a large portion of that is learning how to find out where errors are coming from.

Cool thanks for the reply!

I found the bug, I was looking for the string in the Jtext area when I should have been getting the string from the Label... UGG

/slapsself

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.