Guassian Elimination
<< Trig Drill | OtherProjectsTrailIndex | MatrixApplet >>
If you export a runnable jar file you can execute this from a Terminal window with the command (actually "Terminal" is for Mac OSX or linux, for Windows Use "Command Prompt" from the Start > All Programs >Accessories menu)
java -jar GEApp.jar
You can see it working in your web browser here
Rational.java
public class Rational { private int numerator; private int denominator; public Rational(String s){ int fBar=s.indexOf("/"); if (fBar==-1){ numerator=Integer.parseInt(s); denominator=1; } else { numerator=Integer.parseInt(s.substring(0,fBar)); denominator=Integer.parseInt(s.substring(fBar+1)); simplify(); } } public Rational(int n, int d){ numerator=n; denominator=d; simplify(); } public Rational(int n){ numerator=n; denominator=1; } public Rational(){ numerator=0; denominator=1; } public Rational (Rational r){ this.numerator=r.getNumerator(); this.denominator=r.getDenominator(); } public void setNumerator(int numerator) { this.numerator = numerator; } public int getNumerator() { return numerator; } public void setDenominator(int denominator) { this.denominator = denominator; } public int getDenominator() { return denominator; } public double value(){ return (double)(numerator)/denominator; } public String toString(){ if (denominator ==1) return Integer.toString(numerator); return Integer.toString(numerator)+"/"+Integer.toString(denominator); } public Rational multiply(Rational other){ int n=this.getNumerator()*other.getNumerator(); int d=this.getDenominator()*other.getDenominator(); Rational result=new Rational(n,d); return result; } public Rational add(Rational other){ int n=this.getNumerator()*other.getDenominator()+ other.getNumerator()*this.getDenominator(); int d=this.getDenominator()*other.getDenominator(); Rational result=new Rational(n,d); return result; } public void simplify(){ int p=2; while (p<= Math.max(numerator, denominator)){ while(numerator%p==0 && denominator%p==0){ numerator/=p; denominator/=p; } if (p==2) p++; else p+=2; } } }
Equation.java
public class Equation { private Rational x; private Rational y; private Rational z; private Rational c; public Equation(String xCoeff, String yCoeff, String zCoeff, String constant){ x=new Rational(xCoeff); y=new Rational(yCoeff); z=new Rational(zCoeff); c=new Rational(constant); } public Equation (Rational x, Rational y, Rational z, Rational c){ this.x=x; this.y=y; this.z=z; this.c=c; } public Equation (Equation e){ this.x=e.getX(); this.y=e.getY(); this.z=e.getZ(); this.c=e.getC(); } public Equation (){ this.x=new Rational(); this.y=new Rational(); this.z=new Rational(); this.c=new Rational(); } public Rational getX(){ return x; } public Rational getY(){ return y; } public Rational getZ(){ return z; } public Rational getC(){ return c; } public void setX(String r){ this.x=new Rational(r); } public void setX(Rational r){ this.x=r; } public void setY(String r){ this.y=new Rational(r); } public void setY(Rational r){ this.y=r; } public void setZ(String r){ this.z=new Rational(r); } public void setZ(Rational r){ this.z=r; } public void setC(String r){ this.c=new Rational(r); } public void setC(Rational r){ this.c=r; } public String toString(){ return "("+x.toString()+")x + ("+y.toString()+")y + ("+z.toString()+")z = "+c.toString(); } public Equation multiply(Rational r){ return new Equation (x.multiply(r), y.multiply(r), z.multiply(r), c.multiply(r)); } public Equation multiply(Rational r, Equation other){ Equation e= other.multiply(r); return new Equation(x.add(e.getX()) , y.add(e.getY()), z.add(e.getZ()), c.add(e.getC()) ); } }
GEApp.java (this is if you want to run this as an application)
import java.util.Scanner; public class GEApp { static Scanner in; static Equation[] row; public static void main(String[] args) { in=new Scanner(System.in); row=new Equation[3]; for (int i=0;i<3;i++) row[i]=new Equation(); String command=getCommand(); while(!command.equals("Q")){ if (command.equals("E")){ int r=getRow("Edit which row (1-3): "); row[r].setX(getRational("Coeff of x: ")); row[r].setY(getRational("Coeff of y: ")); row[r].setZ(getRational("Coeff of z: ")); row[r].setC(getRational("equals: ")); } else if (command.equals("S")){ int a=getRow("Swap row (1-3): "); int b=getRow("with what row (1-3): "); Equation buffer=new Equation(row[a]); row[a]=row[b]; row[b]=buffer; } else if (command.equals("M")){ Rational number=getRational("Number to multiply: "); int r=getRow("Multiply "+number.toString()+" to which row (1-3): "); row[r]=row[r].multiply(number); } else if (command.equals("A")){ Rational number=getRational("Number to multiply: "); int a=getRow("Multiply "+number.toString()+" to which row (1-3): "); int r=getRow("And add it to which row (1-3): "); row[r]=row[r].multiply(number, row[a]); } command=getCommand(); } } public static String getCommand(){ System.out.println("System of Equations:"); for (Equation e:row) System.out.println(e); System.out.print("Menu: E)dit, S)wap, M)ultiply a row, A)dd a multiple of a row, Q)uit > "); String command=in.nextLine(); command=command.toUpperCase(); return command.substring(0, 1); } public static int getRow(String prompt){ System.out.print(prompt); String row = in.nextLine(); int result = Integer.parseInt(row); while (result < 1 || result > 3){ System.out.print(prompt); row = in.nextLine(); result = Integer.parseInt(row); } return result-1; } public static Rational getRational(String prompt){ System.out.print(prompt); return new Rational(in.nextLine()); } }
GECounsoleApplet.java (This is if you want to make an Applet version to run in a web browser using The ConsoleApplet
@SuppressWarnings("serial") public class GEConsoleApplet extends ConsoleApplet { private Equation[] row; protected String getTitle() { return "Guassian Elimination Applet"; } public String getCommand(){ console.putln("System of Equations:"); for (Equation e:row) console.putln(e.toString()); console.put("Menu: E)dit, S)wap, M)ultiply a row, A)dd a multiple of a row, Q)uit > "); String command=console.getln(); command=command.toUpperCase(); return command.substring(0, 1); } public int getRow(String prompt){ console.putln(prompt); String row = console.getln(); int result = Integer.parseInt(row); while (result < 1 || result > 3){ console.put(prompt); row = console.getln(); result = Integer.parseInt(row); } return result-1; } public Rational getRational(String prompt){ console.put(prompt); return new Rational(console.getln()); } public void program(){ row=new Equation[3]; for (int i=0;i<3;i++) row[i]=new Equation(); String command=getCommand(); while(!command.equals("Q")){ if (command.equals("E")){ int r=getRow("Edit which row (1-3): "); row[r].setX(getRational("Coeff of x: ")); row[r].setY(getRational("Coeff of y: ")); row[r].setZ(getRational("Coeff of z: ")); row[r].setC(getRational("equals: ")); } else if (command.equals("S")){ int a=getRow("Swap row (1-3): "); int b=getRow("with what row (1-3): "); Equation buffer=new Equation(row[a]); row[a]=row[b]; row[b]=buffer; } else if (command.equals("M")){ Rational number=getRational("Number to multiply: "); int r=getRow("Multiply "+number.toString()+" to which row (1-3): "); row[r]=row[r].multiply(number); } else if (command.equals("A")){ Rational number=getRational("Number to multiply: "); int a=getRow("Multiply "+number.toString()+" to which row (1-3): "); int r=getRow("And add it to which row (1-3): "); row[r]=row[r].multiply(number, row[a]); } command=getCommand(); } } }