Skip to content

Instantly share code, notes, and snippets.

@jananpatel2002
Created February 18, 2021 20:29
Show Gist options
  • Save jananpatel2002/9783bd3ed0dc94ea8231986c78626817 to your computer and use it in GitHub Desktop.
Save jananpatel2002/9783bd3ed0dc94ea8231986c78626817 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: 3
* Email: jkpatel2001@student.stcc.edu
* Short Description of the Problem: Complex numbers
*/
public class Complex {
private double real;
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;
}
private double 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 String toString() {
return this.real + " + (" + this.imag + " i)";
}
}
/*
* 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/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.scene.layout.HBox;
import javafx.stage.Stage;
public class ComplexGUI extends Application {
private static final int APPWIDTH = 1150;
private static final int APPHEIGHT = 220;
private TextField tfAReal;
private TextField tfAImag;
private TextField tfBReal2nd;
private TextField tfBImag2nd;
private ComboBox<String> operation;
private TextField tfCReal3rd;
private TextField tfCImag3rd;
private Label Comp;
private Label ConjA;
private Label ConjB;
private Label I;
private Label I2;
private Label I3;
private Button btnCalculate;
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.tfBReal2nd.getText());
imag = Double.parseDouble(this.tfBImag2nd.getText());
Complex b = new Complex(real, imag);
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.tfCReal3rd.setText(String.valueOf(c.getReal()));
this.tfCImag3rd.setText(String.valueOf(c.getImag()));
String comparison;
if (a.lessThan(b))
comparison = "A < B";
else if (a.greaterThan(b))
comparison = "A > B";
else
comparison = "A == B";
this.Comp.setText("< or >: " + comparison);
Complex conjugation = a.conjugate();
this.ConjA.setText("Conjugation of A: " + conjugation);
conjugation = b.conjugate();
this.ConjB.setText("Conjugation of B: " + conjugation);
} catch (Exception ex) {
this.tfCReal3rd.setText("ERROR");
this.tfCImag3rd.setText("ERROR");
}
}
private BorderPane setupGUI() {
final String COMPONENTSTYLE = "-fx-font-weight: bold; -fx-font-size: 12pt";
this.tfAReal = new TextField();
this.tfAReal.setPrefWidth(75);
this.tfAImag = new TextField();
this.tfAImag.setPrefWidth(75);
this.I = new Label("i");
this.tfBReal2nd = new TextField();
this.tfBReal2nd.setPrefWidth(75);
this.tfBImag2nd = new TextField();
this.tfBImag2nd.setPrefWidth(75);
this.I2 = new Label("i");
this.tfCReal3rd = new TextField();
this.tfCReal3rd.setPrefWidth(75);
this.tfCImag3rd = new TextField();
this.tfCImag3rd.setPrefWidth(75);
this.I3 = new Label("i");
Label lblA = new Label("A");
Label lblB = new Label("B");
Label lblC = new Label("C");
this.operation = new ComboBox<String>();
this.operation.setStyle(COMPONENTSTYLE);
String[] ComboBox = { "+", "-", "*", "/" };
this.operation.getItems().addAll(ComboBox);
this.operation.setValue(ComboBox[0]);
this.btnCalculate = new Button(" = ");
btnCalculate.setOnAction(e -> doCalculation());
// HBOXES
HBox hb1 = new HBox();
hb1.setPrefWidth(200);
hb1.setSpacing(15);
Label plus = new Label("+");
plus.setStyle(COMPONENTSTYLE);
hb1.getChildren().addAll(tfAReal, plus, tfAImag);
HBox hb2 = new HBox();
hb2.setPrefWidth(200);
hb2.setSpacing(15);
Label plus2nd = new Label("+");
plus2nd.setStyle(COMPONENTSTYLE);
hb2.getChildren().addAll(tfBReal2nd, plus2nd, tfBImag2nd);
HBox Ans = new HBox();
Ans.setPrefWidth(200);
Ans.setSpacing(15);
Label plus3rd = new Label("+");
plus3rd.setStyle(COMPONENTSTYLE);
Ans.getChildren().addAll(tfCReal3rd, plus3rd, tfCImag3rd);
FlowPane centerPane = new FlowPane(20, 10);
centerPane.setAlignment(Pos.TOP_CENTER);
centerPane.setPadding(new Insets(5));
Label lblInstructions = new Label(
"Enter the Real and Imaginery parts and click the equal button for an answer: ");
lblInstructions.setPrefWidth(APPWIDTH);
lblInstructions.setAlignment(Pos.CENTER);
centerPane.getChildren().addAll(lblInstructions, lblA, hb1, I, this.operation, lblB, hb2, I2, this.btnCalculate,
lblC, Ans, I3);
this.Comp = new Label();
this.Comp.setStyle(COMPONENTSTYLE);
this.Comp.setPrefWidth((APPWIDTH - 50));
centerPane.getChildren().add(this.Comp);
this.ConjA = new Label();
this.ConjA.setStyle(COMPONENTSTYLE);
this.ConjA.setPrefWidth((APPWIDTH - 50));
centerPane.getChildren().add(this.ConjA);
this.ConjB = new Label();
this.ConjB.setStyle(COMPONENTSTYLE);
this.ConjB.setPrefWidth((APPWIDTH - 50));
centerPane.getChildren().add(this.ConjB);
Label Name = new Label("Complex GUI by Janan Patel");
FlowPane bpane = new FlowPane(5, 5);
bpane.setAlignment(Pos.BOTTOM_RIGHT);
bpane.setPadding(new Insets(10));
bpane.setStyle(COMPONENTSTYLE);
bpane.getChildren().addAll(Name);
BorderPane bp = new BorderPane();
bp.setCenter(centerPane);
bp.setBottom(bpane);
bp.setStyle(COMPONENTSTYLE);
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);
}
@Override
public void start(Stage primaryStage) {
BorderPane bp = setupGUI();
Scene scene = new Scene(bp, APPWIDTH, APPHEIGHT);
primaryStage.setTitle("Complex GUI App");
primaryStage.setScene(scene);
primaryStage.show();
primaryStage.setResizable(false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment