Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Rational Numbers GUI; Problem Displaying Answer

807600Jul 30 2007 — edited Jul 30 2007
I have to make a GUI that adds, subtracts, divides, and multiplies rational numbers. You input them as fractions. I think most of it is right, it does compile, but I don't know how to get my answer to display. I tried simple stuff like just adding it into the setText part at the end, but is there something that I'm missing like convert it to a string or something, I don't really know, I'm still very new to all this.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class RatDemo extends JFrame implements ActionListener {

	 	private JTextField jtfR1, jtfR2, jtfResult;

	 	private JButton jbtAdd, jbtSub, jbtDiv, jbtMul, jbtClr, jbtEx;

	 	public int numerator, denominator;

	public RatDemo(int numerator, int denominator)
	{
  		this.numerator = numerator;
  		this.denominator = denominator;
   		reduce();
	}
	private void reduce()
	{
   		int divisor = gcd((numerator < 0)?-1*numerator:numerator,
   			(denominator < 0)?-1*denominator:denominator);
   		if (divisor != 0)
   		{
       		numerator /= divisor;
       		denominator /= divisor;
	    }
	}
	private int gcd(int a, int b)
	{
      	if (b == 0) return a; else return gcd(b,a%b);
	}
	public int getNumerator()
	{
    	return numerator;
	}
	public void print1(RatDemo RR)
	{
		System.out.print( RR.numerator + "/" + RR.denominator);
	}
	public int getDenominator()
	{
    	return denominator;
	}
	public static RatDemo Add(RatDemo r1, RatDemo r2)
	{
	     	int newNum = r1.numerator * r2.denominator + r1.denominator * r2.numerator ;
	     	int newDenom = r1.denominator * r2.denominator;
	     	return new RatDemo(newNum,newDenom);
	}
	public static RatDemo Subtract(RatDemo r1, RatDemo r2)
	{
 		int newNum = r1.numerator*r2.denominator-r1.denominator*r2.numerator;
 		int newDenom = r1.denominator*r2.denominator;
  		return new RatDemo(newNum, newDenom);
	}
	public static RatDemo Multiply(RatDemo r1, RatDemo r2)
	{
	     	int newNum = r1.numerator * r2.numerator;
	     	int newDenom = r1.denominator * r2.denominator;
	     	return new RatDemo(newNum,newDenom);
	}
	public static RatDemo Divide(RatDemo r1, RatDemo r2)
	{
	     	int newNum = r1.numerator * r2.denominator;
	     	int newDenom = r1.denominator * r2.numerator;
	     	return new RatDemo(newNum,newDenom);
	}
	public static void main(String [] args)
	{
 	 	RatDemo frame = new RatDemo();
 	 	RatDemo r1 = new RatDemo(3, 17);
 	 	RatDemo r2 = new RatDemo(5, 9);;
     	RatDemo r3 = new RatDemo();

 	 	 frame.setTitle(" Rational Numbers ");
 	 	 frame.setSize(500,140);
 	 	 frame.setVisible(true);
 	 	 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
 	public RatDemo()
 	{
    	JPanel p1 = new JPanel();
    	p1.setLayout(new FlowLayout());

    	p1.add( new JLabel("Rational 1"));
    	p1.add( jtfR1 = new JTextField(6));

    	p1.add( new JLabel("Rational 2"));
    	p1.add( jtfR2 = new JTextField(6));

    	JPanel p2 = new JPanel();
    	p2.setLayout( new FlowLayout());

    	p2.add ( new JLabel("Results "));
    	p2.add( jtfResult = new JTextField(30));
    	jtfResult.setEditable(false);

    	JPanel p3 = new JPanel();
    	p3.setLayout(new FlowLayout());

    	p3.add( jbtAdd = new JButton("Add"));
    	p3.add( jbtSub = new JButton("Subtract"));
    	p3.add( jbtDiv = new JButton("Divide"));
    	p3.add( jbtMul = new JButton("Multiply"));
    	p3.add( jbtClr = new JButton("Clear"));
    	p3.add( jbtEx = new JButton("Exit"));

 	  	getContentPane().setLayout(new BorderLayout());
    	getContentPane().add(p1, BorderLayout.NORTH);
    	getContentPane().add(p2, BorderLayout.CENTER);
    	getContentPane().add(p3, BorderLayout.SOUTH);

	   	jbtAdd.addActionListener(this);
	  	jbtSub.addActionListener(this);
	  	jbtMul.addActionListener(this);
	  	jbtDiv.addActionListener(this);
	  	jbtClr.addActionListener(this);
	  	jbtEx.addActionListener(this);
	}
 	public void actionPerformed( ActionEvent e)
 	{
   		String actionCommand = e.getActionCommand();
   		if (e.getSource() instanceof JButton)
   		{
   			if ("Add".equals(actionCommand))
   			{
   				calculate('+');
	    	}
   			else if ("Subtract".equals(actionCommand))
   			{
   				calculate('-');
	    	}
   			else if ("Multiply".equals(actionCommand))
   			{
    				calculate('*');
	    	}
   			else if ("Divide".equals(actionCommand))
   			{
    				calculate('/');
	    	}
   			else if ("Clear".equals(actionCommand))
   			{
    				jtfR1.setText(" ");
    				jtfR2.setText(" ");
    				jtfResult.setText(" ");
	    	}
   			else if ("Exit".equals(actionCommand))
   			{
    				System.exit(0);
	    	}
	 	}
    }
	private void calculate(char operator)
	{
  		switch(operator)
  		{
   			case '+':
				jtfResult.setText("The sum of the rational numbers is ");
     			break;
   			case '-':
				jtfResult.setText("The difference of the rational numbers is ");
    			break;
   			case '*':
				jtfResult.setText("The product of the rational numbers is ");
    			break;
   			case '/':
				jtfResult.setText("The quotient of the rational numbers is ");
    			break;
   			default:
    			jtfResult.setText("Invalid Output");
	   }
	}
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 27 2007
Added on Jul 30 2007
2 comments
287 views