-
-
Save bytecodeman/6e09b24dcb1fd1e82d1cb6155757bc25 to your computer and use it in GitHub Desktop.
CSC-112 - HW7 Solution Complex Number Regex with GUI
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 double imag; | |
private 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); | |
} | |
/*** Getters ***/ | |
public double getReal() { | |
return this.real; | |
} | |
public double getImag() { | |
return this.imag; | |
} | |
/*** Setters ***/ | |
public void setImag(double imag) { | |
this.imag = imag; | |
} | |
public void setReal(double real) { | |
this.real = real; | |
} | |
/*** 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 String APPTITLE = "Complex GUI App"; | |
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; | |
// 3.1415 + 2342.3i | |
private void doCalculation() { | |
try { | |
Complex a = new ComplexWithRegEx(this.tfCmpA.getText()); | |
Complex b = new ComplexWithRegEx(this.tfCmpB.getText()); | |
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(c.toString()); | |
} 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.btnCalculate.setOnAction(e -> doCalculation()); | |
this.btnReset = new Button(" Reset "); | |
this.btnReset.setStyle(COMPONENTSTYLE); | |
this.btnReset.setOnAction(e -> resetApp()); | |
this.btnHelp = new Button(" Help "); | |
this.btnHelp.setStyle(COMPONENTSTYLE); | |
this.btnHelp.setOnAction(e -> appHelp()); | |
FlowPane fp = new FlowPane(5, 10); | |
fp.setAlignment(Pos.CENTER); | |
fp.setPadding(new Insets(5)); | |
// First Complex | |
fp.getChildren().addAll(lblA, tfCmpA); | |
// Place Operation Selector | |
fp.getChildren().addAll(new Label(" "), this.operation, new Label(" ")); | |
// Second Complex | |
fp.getChildren().addAll(lblB, tfCmpB); | |
// Place Calculate Button | |
fp.getChildren().addAll(new Label(" "), this.btnCalculate, new Label(" ")); | |
// Answer Complex | |
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; | |
} | |
@Override | |
public void start(Stage primaryStage) { | |
Scene scene = new Scene(setupGUI(), APPWIDTH, APPHEIGHT); | |
primaryStage.setTitle(APPTITLE); | |
primaryStage.setScene(scene); | |
primaryStage.show(); | |
primaryStage.setResizable(false); | |
} | |
/** | |
* 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); | |
} | |
} |
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
package complex; | |
import java.util.regex.Matcher; | |
import java.util.regex.Pattern; | |
public class ComplexWithRegEx extends Complex { | |
public ComplexWithRegEx(String complexNumber) { | |
complexNumber = complexNumber.trim(); | |
String regex; | |
Pattern pattern; | |
Matcher matcher; | |
// Just Real Specified | |
regex = "^[+-]?(\\d+(\\.\\d*)?|\\.\\d+)$"; | |
pattern = Pattern.compile(regex); | |
matcher = pattern.matcher(complexNumber); | |
if (matcher.find()) { | |
this.setReal(Double.parseDouble(matcher.group(0))); | |
this.setImag(0); | |
return; | |
} | |
// Just Imaginary Specified | |
regex = "^([+-]?(\\d+(\\.\\d*)?|\\.\\d+))i$"; | |
pattern = Pattern.compile(regex); | |
matcher = pattern.matcher(complexNumber); | |
if (matcher.find()) { | |
this.setReal(0); | |
this.setImag(Double.parseDouble(matcher.group(1))); | |
return; | |
} | |
// Just i Specified | |
regex = "^([+-]?)i$"; | |
pattern = Pattern.compile(regex); | |
matcher = pattern.matcher(complexNumber); | |
if (matcher.find()) { | |
this.setReal(0); | |
int sign = matcher.group(1).equals("-") ? -1 : 1; | |
this.setImag(sign); | |
return; | |
} | |
// Real and Imag Parts | |
regex = "^([+-]?(\\d+(\\.\\d*)?|\\.\\d+))([+-](\\d+(\\.\\d*)?|\\.\\d+))i$"; | |
pattern = Pattern.compile(regex); | |
matcher = pattern.matcher(complexNumber); | |
if (matcher.find()) { | |
this.setReal(Double.parseDouble(matcher.group(1))); | |
this.setImag(Double.parseDouble(matcher.group(4))); | |
return; | |
} | |
// Real and a plain i | |
regex = "^([+-]?(\\d+(\\.\\d*)?|\\.\\d+))([+-])i$"; | |
pattern = Pattern.compile(regex); | |
matcher = pattern.matcher(complexNumber); | |
if (matcher.find()) { | |
this.setReal(Double.parseDouble(matcher.group(1))); | |
this.setImag(matcher.group(4).equals("-") ? -1 : 1); | |
return; | |
} | |
throw new RuntimeException(String.format("Bad Complex Number Specified: %s", complexNumber)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment