Skip to content

Instantly share code, notes, and snippets.

@bytecodeman bytecodeman/Complex.java Secret
Last active Feb 18, 2019

Embed
What would you like to do?
CSC-112 HW4 Complex GUI Solution
/*
* Name: Antonio C. Silvestri
* Date: 2/13/2019
* Email: silvestri@stcc.edu
* Course: Intermediate Topics in Java Programming (CSC-112)
* Problem: HW3 - Implement complex numbers
*/
package complex;
public class Complex implements Comparable<Complex> {
private final double imag;
private final double real;
public final static Complex ZERO = new Complex();
/*** Constructors ***/
public Complex(double real, double imag) {
this.real = real;
this.imag = imag;
}
public Complex(double real) {
this(real, 0);
}
public Complex() {
this(0,0);
}
public Complex(String complexNumber) {
// Stub Method
this.real = 0;
this.imag = 0;
}
/*** Getters ***/
public double getReal() {
return this.real;
}
public double getImag() {
return this.imag;
}
/*** Operations ***/
public Complex add(Complex lhs) {
// a+bi + c+di is (a+c)+(b+d)i
return new Complex(this.real + lhs.real, this.imag + lhs.imag);
}
public Complex subtract(Complex lhs) {
// a+bi - c+di is (a-c)+(b-d)i
return add(lhs.negate());
}
public Complex multiply(Complex lhs) {
// (a+bi) * (c+di) is (ac-bd)+(ad+bc)i
double ac = this.real * lhs.real;
double bd = this.imag * lhs.imag;
double ad = this.real * lhs.imag;
double bc = this.imag * lhs.real;
return new Complex(ac - bd, ad + bc);
}
public Complex divide(Complex lhs) {
// (a+bi) / (c+di) is (ac+bd)/(c^2+d^2) + (bc-ad)/(c^2+d^2)i
double a = this.real;
double b = this.imag;
double c = lhs.real;
double d = lhs.imag;
double quotientReal = ((a * c) + (b * d)) / ((c * c) + (d * d));
double quotientImag = ((b * c) - (a * d)) / ((c * c) + (d * d));
return new Complex(quotientReal, quotientImag);
}
public double abs() {
// |a + bi| is sqrt(a^2 + b^2)
return Math.sqrt(Math.pow(this.real, 2) + Math.pow(this.imag, 2));
}
public Complex conjugate() {
// Complex conjugate of a+bi is a-bi
return new Complex(this.real, -this.imag);
}
public double distance(Complex lhs) {
// Distance between a+bi and c+di is sqrt((a-c)^2 + (b-d)^2)
return Math.sqrt(Math.pow(this.real - lhs.real, 2) + Math.pow(this.imag - lhs.imag, 2));
}
public Complex negate() {
// -(a+bi) is -a-bi
return new Complex(-this.real, -this.imag);
}
/*** Comparisons ***/
public boolean equals(Complex lhs) {
/*
* If A is the larger complex number and B is the smaller one, A and B
* are approximately equal if the following is true: (|A| - |B|)/|A| <
* 1E-6 (one millionth)
*/
return this.distance(lhs) / (this.greaterThan(lhs) ? this.abs() : lhs.abs()) < 1E-6;
}
public boolean greaterThan(Complex lhs) {
return this.abs() > lhs.abs();
}
public boolean lessThan(Complex lhs) {
return this.abs() < lhs.abs();
}
public String toString() {
return getRoundedtoString(2);
}
// Round components to the specified number of decimal places
private String getRoundedtoString(int decimalPlaces) {
// Assume negative input should be changed to positive
decimalPlaces = Math.abs(decimalPlaces);
// Ensure that any number which rounds to 0 is displayed as positive 0
double realPart = Math.abs(this.real) < Math.pow(10, -decimalPlaces) ? 0.0 : this.real;
double imagPart = Math.abs(this.imag) < Math.pow(10, -decimalPlaces) ? 0.0 : this.imag;
// Format numbers according to decimal places
String format = "%." + decimalPlaces + "f";
String toString = realPart < 0 ? "-" : "";
toString += String.format(format, Math.abs(realPart));
toString += imagPart < 0 ? " - " : " + ";
return toString + String.format(format + "i", Math.abs(imagPart));
}
@Override
public int compareTo(Complex lhs) {
if (this.lessThan(lhs))
return -1;
if (this.greaterThan(lhs))
return 1;
return 0;
}
}
/*
* Name: Antonio C. Silvestri
* Date: 2/14/2019
* Email: silvestri@stcc.edu
* Course: Intermediate Topics in Java Programming (CSC-112)
* Problem: HW4 Application Code for the Complex App
*/
package complex;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;
public class ComplexGUI extends Application {
private static final int APPWIDTH = 700;
private static final int APPHEIGHT = 325;
private TextField tfAReal;
private TextField tfAImag;
private TextField tfBReal;
private TextField tfBImag;
private ComboBox<String> operation;
private TextField tfCReal;
private TextField tfCImag;
private Button btnCalculate;
private Label lblComparison;
private Label lblConjugateA;
private Label lblConjugateB;
private Label lblNegateA;
private Label lblNegateB;
@Override
public void start(Stage primaryStage) {
BorderPane bp = setupGUI();
btnCalculate.setOnAction(e -> doCalculation());
Scene scene = new Scene(bp, APPWIDTH, APPHEIGHT);
primaryStage.setTitle("Complex GUI App");
primaryStage.setScene(scene);
primaryStage.show();
primaryStage.setResizable(false);
}
private void doCalculation() {
try {
double real = Double.parseDouble(this.tfAReal.getText());
double imag = Double.parseDouble(this.tfAImag.getText());
Complex a = new Complex(real, imag);
real = Double.parseDouble(this.tfBReal.getText());
imag = Double.parseDouble(this.tfBImag.getText());
Complex b = new Complex(real, imag);
String op = this.operation.getValue();
Complex c = null;
switch (op) {
case "+":
c = a.add(b);
break;
case "-":
c = a.subtract(b);
break;
case "*":
c = a.multiply(b);
break;
case "/":
c = a.divide(b);
break;
}
this.tfCReal.setText(String.valueOf(c.getReal()));
this.tfCImag.setText(String.valueOf(c.getImag()));
String strComp;
if (a.compareTo(b) > 0)
strComp = "A > B";
else if (a.compareTo(b) < 0)
strComp = "A < B";
else
strComp = "A == B";
this.lblComparison.setText("Comparison: " + strComp);
Complex temp;
temp = a.conjugate();
this.lblConjugateA.setText("Conjugate A: " + temp);
temp = b.conjugate();
this.lblConjugateB.setText("Conjugate B: " + temp);
temp = a.negate();
this.lblNegateA.setText("Negate A: " + temp);
temp = b.negate();
this.lblNegateB.setText("Negate B: " + temp);
} catch (Exception ex) {
this.tfCReal.setText("ERROR");
this.tfCImag.setText("ERROR");
this.lblComparison.setText("");
this.lblConjugateA.setText("");
this.lblConjugateB.setText("");
this.lblNegateA.setText("");
this.lblNegateB.setText("");
}
}
private BorderPane setupGUI() {
final String COMPONENTSTYLE = "-fx-font-weight: bold; -fx-font-size: 14pt";
this.tfAReal = new TextField();
this.tfAReal.setStyle(COMPONENTSTYLE);
this.tfAReal.setPrefWidth(100);
this.tfAImag = new TextField();
this.tfAImag.setStyle(COMPONENTSTYLE);
this.tfAImag.setPrefWidth(100);
this.tfBReal = new TextField();
this.tfBReal.setStyle(COMPONENTSTYLE);
this.tfBReal.setPrefWidth(100);
this.tfBImag = new TextField();
this.tfBImag.setStyle(COMPONENTSTYLE);
this.tfBImag.setPrefWidth(100);
this.tfCReal = new TextField();
this.tfCReal.setStyle(COMPONENTSTYLE);
this.tfCReal.setPrefWidth(100);
this.tfCReal.setEditable(false);
this.tfCImag = new TextField();
this.tfCImag.setStyle(COMPONENTSTYLE);
this.tfCImag.setPrefWidth(100);
this.tfCImag.setEditable(false);
Label lblA = new Label("(A)");
lblA.setStyle(COMPONENTSTYLE);
Label lblB = new Label("(B)");
lblB.setStyle(COMPONENTSTYLE);
Label lblC = new Label("(C)");
lblC.setStyle(COMPONENTSTYLE);
Label lblPlusA = new Label("+");
lblPlusA.setStyle(COMPONENTSTYLE);
Label lblPlusB = new Label("+");
lblPlusB.setStyle(COMPONENTSTYLE);
Label lblPlusC = new Label("+");
lblPlusC.setStyle(COMPONENTSTYLE);
Label lbliA = new Label("i");
lbliA.setStyle(COMPONENTSTYLE);
Label lbliB = new Label("i");
lbliB.setStyle(COMPONENTSTYLE);
Label lbliC = new Label("i");
lbliC.setStyle(COMPONENTSTYLE);
this.operation = new ComboBox<String>();
this.operation.getItems().addAll(new String[] { "+", "-", "*", "/" });
this.operation.setStyle(COMPONENTSTYLE);
this.operation.setValue("+");
this.btnCalculate = new Button(" = ");
this.btnCalculate.setStyle(COMPONENTSTYLE);
FlowPane fp = new FlowPane(5, 10);
fp.setAlignment(Pos.CENTER);
fp.setPadding(new Insets(5));
// Build First Complex
fp.getChildren().addAll(lblA, tfAReal, lblPlusA, tfAImag, lbliA);
// Place Operation Selector
fp.getChildren().addAll(new Label(" "), this.operation, new Label(" "));
// Build Second Rational
fp.getChildren().addAll(lblB, tfBReal, lblPlusB, tfBImag, lbliB);
// Place Calculate Button
fp.getChildren().addAll(this.btnCalculate, new Label(" "));
// Build Answer Rational
fp.getChildren().addAll(lblC, tfCReal, lblPlusC, tfCImag, lbliC);
this.lblComparison = new Label();
this.lblComparison.setStyle(COMPONENTSTYLE);
this.lblComparison.setPrefWidth(APPWIDTH);
fp.getChildren().add(this.lblComparison);
this.lblConjugateA = new Label();
this.lblConjugateA.setStyle(COMPONENTSTYLE);
this.lblConjugateA.setPrefWidth(APPWIDTH);
fp.getChildren().add(this.lblConjugateA);
this.lblConjugateB = new Label();
this.lblConjugateB.setStyle(COMPONENTSTYLE);
this.lblConjugateB.setPrefWidth(APPWIDTH);
fp.getChildren().add(this.lblConjugateB);
this.lblNegateA = new Label();
this.lblNegateA.setStyle(COMPONENTSTYLE);
this.lblNegateA.setPrefWidth(APPWIDTH);
fp.getChildren().add(this.lblNegateA);
this.lblNegateB = new Label();
this.lblNegateB.setStyle(COMPONENTSTYLE);
this.lblNegateB.setPrefWidth(APPWIDTH);
fp.getChildren().add(this.lblNegateB);
Label lblStatus = new Label("Complex GUI by A.C. Silvestri");
FlowPane bpane = new FlowPane(5, 5);
bpane.setAlignment(Pos.BASELINE_CENTER);
bpane.setPadding(new Insets(5));
bpane.setStyle("-fx-font-weight: bold; -fx-font-size: 12pt");
bpane.getChildren().addAll(lblStatus);
BorderPane bp = new BorderPane();
bp.setCenter(fp);
bp.setBottom(bpane);
return bp;
}
/**
* The main method is only needed for the IDE with limited JavaFX support. Not
* needed for running from the command line.
*/
public static void main(String[] args) {
launch(args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.