Java Snippet
Playing around with Swing and string parsing, finally bothered getting this finished.
It factors any factorable trinomial.
Not really an experienced dev, so any advice on optimization or what I'm doing stupidly would be great.
Runnable .jar file here:
http://www.mediafire.com/?jol41bkku9v0mqi
Code in spoiler below.
Java code:
package factortrinomial;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FactorTrinomial extends JFrame implements ActionListener{
public static final int WIDTH=400;
public static final int HEIGHT=200;
public static final int NUMBER_OF_CHAR=30;
public static String output;
private JTextField trinomial;
public static void main(String[] args){
FactorTrinomial gui = new FactorTrinomial(); //creates an object, or window, using the no argument constructor below to set up its layout.
gui.setVisible(true);
}
public FactorTrinomial(){
super("Trinomial Factorer");
setSize(WIDTH, HEIGHT);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(2,1)); //creates a grid space of two rows and a single column that encases the whole window.
JPanel topPanel = new JPanel(); //creates a container which then fills the top row.
topPanel.setLayout(new BorderLayout());
topPanel.setBackground(Color.WHITE);
trinomial = new JTextField(NUMBER_OF_CHAR);
topPanel.add(trinomial, BorderLayout.SOUTH); //adds a text field at the bottom of the top container, near the middle.
JLabel trinomialLabel = new JLabel("Enter your trinomial below (e.g. x^2+8x+16)");
topPanel.add(trinomialLabel, BorderLayout.NORTH); //puts instructive text at the top.
JLabel trinomialLabel2 = new JLabel("If the result is nonsensical, the trinomial cannot be factored");
topPanel.add(trinomialLabel2, BorderLayout.CENTER); //puts more instructive text beneath that.
add(topPanel); //adds the whole panel, including the text area and instructive texts.
JPanel bottomPanel = new JPanel(); //creates the lower panel.
bottomPanel.setLayout(new FlowLayout());
bottomPanel.setBackground(Color.BLUE);
JButton enterButton = new JButton("Enter");
enterButton.addActionListener(this);
bottomPanel.add(enterButton); //creates an enter button and a listener for said button.
JButton clearButton = new JButton("Clear");
clearButton.addActionListener(this);
bottomPanel.add(clearButton); //creates a clear button to erase.
add(bottomPanel);
}
public void actionPerformed(ActionEvent e){
String actionCommand = e.getActionCommand(); //gets the text from the button pressed.
if (actionCommand.equals("Enter")) {
output=trinomial.getText();
output=Trinomial(output);
trinomial.setText(output);
}
else if(actionCommand.equals("Clear")) {trinomial.setText("");}
else {trinomial.setText("Error");}
}
public static String Trinomial(String Equation){
int counter=0,counter2=0,cTerm=0,r=0,t=0,q=1,leftTop=0;
String Exponent="1";
String Exponent2="1";
String Coefficient1="1";
String Coefficient2="1";
String finalTerm="1";
String Teste=""; //initializes a bunch of stuff that can't be initialized inside a loop, since that obviously ruins the loop.
for (int i=0;i<Equation.length();i++) {
String Tester=Equation.substring(i, i+1);
if (Tester.equals("x")) {
int logger=i;
if (Equation.substring(i+1,i+2).equals("^")) { //checks for exponents
for (int x=i+2;x<Equation.length();x++) {
String Test=Equation.substring(x, x+1);
if (Test.equals("+") || Test.equals("-") || Test.equals("*") || Test.equals(" ") || Test.equals("/") ){
if (counter==0){
Exponent=Equation.substring(i+2, x);
counter++;
break;
}
if (counter==1){
Exponent2=Equation.substring(i+2, x);
break;
}
}
}
}
if (logger==0){
Coefficient1="1";
counter2++;
}
else if (logger==1){
Coefficient1=Equation.substring(0,1);
if (Coefficient1.equals("-")){Coefficient1="-1";}
counter2++;
}
else if (logger>1){
for (int a=i-1;a>=0;a--){
String Tes=Equation.substring(a, a+1);
if (Tes.equals(" ") || Tes.equals("+") || Tes.equals("*") || Tes.equals("/") || Tes.equals("-")){
if (counter2==0){
if (Tes.equals("-")){Coefficient1=Equation.substring(a,i);}
else {Coefficient1=Equation.substring(a+1,i);}
counter2++;
break;
}
if (counter2==1){
if (Tes.equals("-")){Coefficient2=Equation.substring(a,i);}
else {Coefficient2=Equation.substring(a+1,i);}
break;
}
}
if (a==0){
if (Equation.substring(0,i).equals("-")){Coefficient1="-1";}
Coefficient1=Equation.substring(0,i);
counter2++;
break;
}
}
}
}
}
for (int e=Equation.length();e>=0;e--){
if (e==Equation.length()){
Teste=Equation.substring(e);
}
else {
Teste=Equation.substring(e, e+1);
}
if (Teste.equals(" ") || Teste.equals("+") || Teste.equals("-") || Teste.equals("*") || Teste.equals("/")){
if (Teste.equals("-")){finalTerm=Equation.substring(e);}
else {finalTerm=Equation.substring(e+1);}
break;
}
}
cTerm=Integer.parseInt(finalTerm);
int coefficient=Integer.parseInt(Coefficient1);
int topBox=0;
int bottomBox=Integer.parseInt(Coefficient2);
for (q=coefficient;q>1;q--){
if (coefficient%q==0 && cTerm%q==0 && bottomBox%q==0){
coefficient=coefficient/q;
cTerm=cTerm/q;
bottomBox=bottomBox/q;
break;
}
}
topBox=cTerm*coefficient;
System.out.println(topBox+" "+bottomBox);
done:for (r=Math.abs(topBox);r>=Math.abs(topBox)-(2*Math.abs(topBox));r--){
for (t=Math.abs(topBox);t>=Math.abs(topBox)-(2*Math.abs(topBox));t--){
if (t*r==topBox){
if (r+t==bottomBox){
break done;
}
}
}
}
if (coefficient==1){
return (q+"(x+"+r+")(x+"+t+")");
}
/*None of these variables are necessary; however, they make the code more explicit. This method uses the X box factoring sequence, which one needs to familiarize oneself with to understand.
This method just assumes that x is one, and the first term has an exponent of 2.
To make this more understandable: any variable with top or bottom is a common multiple outside the box.
Anything with upper or lower are actual numbers within the box*/
else {
int upperLeft=coefficient;
int upperRight=r;
int lowerLeft=t;
int lowerRight=cTerm;
for (int s=Math.abs(upperRight);s>=Math.abs(upperRight)-2*(Math.abs(upperRight));s--){
if (upperRight%s==0 && upperLeft%s==0){
leftTop=s;
break;
}
}
int topRight=upperRight/leftTop;
int leftBottom=lowerRight/topRight;
int topLeft=lowerLeft/leftBottom;
return (q+"("+topLeft+"x+"+topRight+")("+leftTop+"x+"+leftBottom+")");
}
}
}
Edit: reviewing this, it seems somewhere along the way something didn't save, this version isn't compatible with negative numbers. Will fix soon.
Last edited by Boredpayne; Jul 18, 2011 at 02:27 AM.