Skip to content

Instantly share code, notes, and snippets.

@jananpatel2002
Created February 25, 2021 20:06
Show Gist options
  • Save jananpatel2002/118682a99234623e3a191764cc854416 to your computer and use it in GitHub Desktop.
Save jananpatel2002/118682a99234623e3a191764cc854416 to your computer and use it in GitHub Desktop.
/*
* Name: Janan Patel
* Date: 2/18/2021
* Course Number: CSC-112-D01
* Course Name: Computer Science
* Problem Number: 4
* Email: jkpatel2001@student.stcc.edu
* Short Description of the Problem: Complex numbers
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Complex {
private double real;
private double imag;
public double getReal() {
return real;
}
public void setReal(double real) {
this.real = real;
}
public double getImag() {
return imag;
}
public void setImag(double imag) {
this.imag = imag;
}
public Complex(double real, double imag) {
this.real = real;
this.imag = imag;
}
public Complex(double real) {
this.real = real;
this.imag = 0;
}
public Complex() {
this.real = 0;
this.imag = 0;
}
public Complex add(Complex c2) {
double real = this.real + c2.real;
double imag = this.imag + c2.imag;
return new Complex(real, imag);
}
public Complex subtract(Complex c2) {
Complex a = this;
return a.add(c2.negate());
}
public Complex multiply(Complex c2) {
double real = (this.real * c2.real) - (this.imag * c2.imag);
double imag = (this.real * c2.imag) + (this.imag * c2.real);
return new Complex(real, imag);
}
public Complex divide(Complex c2) {
double real = ((this.real * c2.real) + (this.imag * c2.imag)) / ((c2.real * c2.real) + (c2.imag * c2.imag));
double imag = ((this.imag * c2.real) - (this.real * c2.imag)) / ((c2.real * c2.real) + (c2.imag * c2.imag));
return new Complex(real, imag);
}
public double abs() {
double absolutevalue = Math.sqrt((this.real * this.real) + (this.imag * this.imag));
return absolutevalue;
}
public Complex negate() {
return new Complex(-this.real, -this.imag);
}
public Complex conjugate() {
return new Complex(this.real, -this.imag);
}
public double distance(Complex c2) {
double distance = Math.sqrt(
((this.real - c2.real) * (this.real - c2.real)) + ((this.imag - c2.imag) * (this.imag - c2.imag)));
return distance;
}
public boolean equals(Complex c2) {
boolean x = Math.abs(this.distance(c2) / Math.max(this.abs(), c2.abs())) < 1E-6;
if (x == true) {
return true;
} else
return false;
}
public boolean greaterThan(Complex c2) {
boolean isOrisnt;
if (this.abs() > c2.abs()) {
isOrisnt = true;
} else
isOrisnt = false;
return isOrisnt;
}
public boolean lessThan(Complex c2) {
boolean isOrisnt;
if (this.abs() < c2.abs()) {
isOrisnt = true;
} else
isOrisnt = false;
return isOrisnt;
}
public Complex(String compnumbers) throws RuntimeException {
// real part regex
String regex = "^[+-]?(\\d+(\\.\\d+)?)$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(compnumbers.trim());
if (matcher.find()) {
this.real = Double.parseDouble(matcher.group(0));
this.imag = 0;
return;
}
// Imaginary part regex
regex = "^([+-]?(\\d+(\\.\\d*)?|\\.\\d+))[i]$";
pattern = Pattern.compile(regex);
matcher = pattern.matcher(compnumbers.trim());
if (matcher.find()) {
this.real = 0;
this.imag = Double.parseDouble(matcher.group(1));
return;
}
// Imaginary part regex with invalid i placement for positive.
regex = "^(i)(\\d+(\\.\\d*)?|\\.\\d+)";
pattern = Pattern.compile(regex);
matcher = pattern.matcher(compnumbers.trim());
if (matcher.find()) {
this.real = 0;
this.imag = Double.parseDouble(matcher.group(2));
return;
}
// Imaginary part regex with invalid i placement for negative.
regex = "^(([-])?(i)(\\d+(\\.\\d*)?|\\.\\d+))";
pattern = Pattern.compile(regex);
matcher = pattern.matcher(compnumbers.trim());
if (matcher.find()) {
this.real = 0;
this.imag = Double.parseDouble(matcher.group(2) + matcher.group(4));
return;
}
// Real and Imag regex
regex = "^([+-]?(\\d+(\\.\\d*)?|\\.d+))([+-](\\d+(\\.\\d*)?|\\.\\d+))[i]$";
pattern = Pattern.compile(regex);
matcher = pattern.matcher(compnumbers.trim());
if (matcher.find()) {
this.real = Double.parseDouble(matcher.group(1));
this.imag = Double.parseDouble(matcher.group(4));
return;
}
// just i regex
regex = "^([+-]?)i$";
pattern = Pattern.compile(regex);
matcher = pattern.matcher(compnumbers.trim());
if (matcher.find()) {
this.real = 0;
if (matcher.group(1).equals("-")) {
this.imag = -1;
} else
this.imag = 1;
return;
}
// real part with i alone imag regex
regex = "^([+-]?(\\d+(.\\d*)?|.\\d+))([+-])i$";
pattern = Pattern.compile(regex);
matcher = pattern.matcher(compnumbers.trim());
if (matcher.find()) {
this.real = Double.parseDouble(matcher.group(1));
if (matcher.group(4).equals("-")) {
this.imag = -1;
} else
this.imag = 1;
return;
}
else
throw new RuntimeException("This is not a valid complex number");
}
public String toString() {
return this.real + " + (" + this.imag + " i)";
}
}
/*
* Name: Janan Patel
* Date: 2/25/2021
* Course Number: CSC-112-D01
* Course Name: Computer Science
* Problem Number: 5
* Email: jkpatel2001@student.stcc.edu
* Short Description of the Problem: Complex numbers with ReGex GUI
*/
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 = 950;
private static final int APPHEIGHT = 220;
private TextField compTfA;
private TextField compTfB;
private TextField compTfC;
private Button calcButton;
private ComboBox<String> operation;
private void doCalculation() {
try {
String firstString = this.compTfA.getText().trim();
String secondString = this.compTfB.getText().trim();
Complex a = new Complex(firstString.trim());
Complex b = new Complex(secondString.trim());
String operator = this.operation.getValue();
var c = new Complex();
switch (operator) {
case "+":
c = a.add(b);
break;
case "-":
c = a.subtract(b);
break;
case "/":
c = a.divide(b);
break;
case "*":
c = a.multiply(b);
break;
}
this.compTfC.setText(c.toString());
} catch (Exception ex) {
this.compTfC.setText("ERROR");
}
}
private BorderPane setupGUI() {
final String COMPONENTSTYLE = "-fx-font-weight: bold; -fx-font-size: 14pt";
this.compTfA = new TextField();
this.compTfA.setStyle(COMPONENTSTYLE);
this.compTfA.setPrefWidth(160);
this.compTfB = new TextField();
this.compTfB.setStyle(COMPONENTSTYLE);
this.compTfB.setPrefWidth(160);
this.compTfC = new TextField();
this.compTfC.setStyle(COMPONENTSTYLE);
this.compTfC.setPrefWidth(160);
this.compTfC.setEditable(false);
FlowPane fp = new FlowPane(10, 10);
fp.setAlignment(Pos.CENTER);
fp.setPadding(new Insets(10));
Label lblInstructions = new Label(
"Enter the Real and Imaginery parts and click the equal button for an answer: ");
lblInstructions.setAlignment(Pos.TOP_CENTER);
lblInstructions.setPrefWidth(APPWIDTH);
Label lblStatus = new Label("Complex GUI with Regex by Janan Patel");
FlowPane bpane = new FlowPane(10, 15);
bpane.setStyle(COMPONENTSTYLE);
bpane.setAlignment(Pos.BASELINE_CENTER);
bpane.setPadding(new Insets(25));
bpane.getChildren().addAll(lblStatus);
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.setStyle(COMPONENTSTYLE);
String ComboBox[] = { "+", "-", "/", "*" };
this.operation.getItems().addAll(ComboBox);
this.operation.setValue(ComboBox[0]);
this.calcButton = new Button(" = ");
this.calcButton.setStyle("-fx-font-weight: bold; -fx-font-size: 15pt");
fp.getChildren().addAll(lblA, compTfA, lblInstructions, new Label(" "), this.operation, new Label(" "), lblB,
compTfB, this.calcButton, lblC, compTfC);
BorderPane bp = new BorderPane();
bp.setCenter(fp);
bp.setBottom(bpane);
return bp;
}
@Override
public void start(Stage primaryStage) {
BorderPane bp = setupGUI();
Scene scene = new Scene(bp, APPWIDTH, APPHEIGHT);
primaryStage.setTitle("Complex GUI App with Regular Expressions");
primaryStage.setScene(scene);
primaryStage.show();
primaryStage.setResizable(false);
this.calcButton.setOnAction(z -> doCalculation());
}
/**
* 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