-
-
Save bytecodeman/90e6951e38a9432f69fdf618223edc86 to your computer and use it in GitHub Desktop.
CSC-112 HW6 Application Code for the Complex App GUI to support string input of Complex Numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Name: Antonio C. Silvestri | |
* Date: 03/01/2019 | |
* Email: silvestri@stcc.edu | |
* Course: Intermediate Topics in Java Programming (CSC-112) | |
* Problem: HW6 Application Code for the Complex App GUI to support | |
* string input of Complex Numbers | |
*/ | |
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; | |
import javafx.scene.control.Alert; | |
import javafx.stage.Modality; | |
public class ComplexGUI extends Application { | |
private static final int APPWIDTH = 900; | |
private static final int APPHEIGHT = 225; | |
private TextField tfCmpA; | |
private TextField tfCmpB; | |
private ComboBox<String> operation; | |
private TextField tfCmpC; | |
private Button btnCalculate; | |
private Button btnReset; | |
private Button btnHelp; | |
@Override | |
public void start(Stage primaryStage) { | |
BorderPane bp = setupGUI(); | |
this.btnCalculate.setOnAction(e -> doCalculation()); | |
this.btnReset.setOnAction(e -> resetApp()); | |
this.btnHelp.setOnAction(e -> appHelp()); | |
Scene scene = new Scene(bp, APPWIDTH, APPHEIGHT); | |
primaryStage.setTitle("Complex GUI App"); | |
primaryStage.setScene(scene); | |
primaryStage.show(); | |
primaryStage.setResizable(false); | |
} | |
private void doCalculation() { | |
try { | |
String strComplexA = this.tfCmpA.getText(); | |
String strComplexB = this.tfCmpB.getText(); | |
Complex a = new Complex(strComplexA); | |
Complex b = new Complex(strComplexB); | |
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.tfCmpC.setText("Not Yet Implemented"); | |
} catch (Exception ex) { | |
this.tfCmpC.setText("ERROR"); | |
} | |
} | |
private void resetApp() { | |
this.tfCmpA.setText(""); | |
this.tfCmpB.setText(""); | |
this.tfCmpC.setText(""); | |
} | |
void appHelp() { | |
// Thanks to Mr. Kevin Pham for this code | |
Alert alert = new Alert(Alert.AlertType.INFORMATION); | |
alert.initModality(Modality.APPLICATION_MODAL); | |
alert.setTitle("Directions"); | |
alert.setHeaderText("Real and Imaginary Numbers"); | |
alert.setContentText("1. Enter a value of 5+3i or any other value for the first dialog box." + | |
"\n2. Enter a value of 2i or any value for the second dialog box." + | |
"\n3. Select whether you want to add, subtract, multiply or divide." + | |
"\n4. Click on the equal button to see the answer."); | |
alert.showAndWait(); | |
} | |
private BorderPane setupGUI() { | |
final String COMPONENTSTYLE = "-fx-font-weight: bold; -fx-font-size: 12pt"; | |
final String strPlaceHolder = "A | Bi | A + Bi"; | |
this.tfCmpA = new TextField(); | |
this.tfCmpA.setStyle(COMPONENTSTYLE); | |
this.tfCmpA.setPrefWidth(200); | |
this.tfCmpA.setPromptText(strPlaceHolder); | |
this.tfCmpB = new TextField(); | |
this.tfCmpB.setStyle(COMPONENTSTYLE); | |
this.tfCmpB.setPrefWidth(200); | |
this.tfCmpB.setPromptText(strPlaceHolder); | |
this.tfCmpC = new TextField(); | |
this.tfCmpC.setStyle(COMPONENTSTYLE); | |
this.tfCmpC.setPrefWidth(200); | |
this.tfCmpC.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); | |
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); | |
this.btnReset = new Button(" Reset "); | |
this.btnReset.setStyle(COMPONENTSTYLE); | |
this.btnHelp = new Button(" Help "); | |
this.btnHelp.setStyle(COMPONENTSTYLE); | |
FlowPane fp = new FlowPane(5, 10); | |
fp.setAlignment(Pos.CENTER); | |
fp.setPadding(new Insets(5)); | |
// Build First Complex | |
fp.getChildren().addAll(lblA, tfCmpA); | |
// Place Operation Selector | |
fp.getChildren().addAll(new Label(" "), this.operation, new Label(" ")); | |
// Build Second Rational | |
fp.getChildren().addAll(lblB, tfCmpB); | |
// Place Calculate Button | |
fp.getChildren().addAll(this.btnCalculate, new Label(" ")); | |
// Build Answer Rational | |
fp.getChildren().addAll(lblC, tfCmpC); | |
// Place Reset Button | |
fp.getChildren().add(this.btnReset); | |
// Place Help Button | |
fp.getChildren().add(this.btnHelp); | |
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