Skip to content

Instantly share code, notes, and snippets.

# bytecodeman/Rational.java Last active Feb 19, 2019

Rational Number Sample Code
 /* * Rational Number Class * Prof. A.C. Silvestri * CSC-112 Intermediate Java * 02/08/18 */ public class Rational implements Comparable { final private int num; final private int den; public final static Rational ZERO = new Rational(0); public final static Rational ONE = new Rational(1); // **************************************************************** public int getNumerator() { return num; } public int getDenominator() { return den; } // **************************************************************** public Rational() { this(0, 1); } public Rational(int num) { this(num, 1); } public Rational(int numerator, int denominator) { if (denominator == 0) { throw new ArithmeticException("denominator is zero"); } // reduce fraction int g = gcf(numerator, denominator); if (denominator > 0) { this.num = numerator / g; this.den = denominator / g; } else { this.num = -numerator / g; this.den = -denominator / g; } } // **************************************************************** public Rational times(Rational b) { int num = this.num * b.num; int den = this.den * b.den; return new Rational(num, den); } public Rational plus(Rational b) { Rational a = this; // special cases (Really Not Needed) if (a.compareTo(Rational.ZERO) == 0) return b; if (b.compareTo(Rational.ZERO) == 0) return a; int num = a.num * b.den + a.den * b.num; int den = a.den * b.den; return new Rational(num, den); } public Rational negate() { return new Rational(-this.num, this.den); } public Rational abs() { if (num >= 0) return this; else return negate(); } public Rational minus(Rational b) { Rational a = this; return a.plus(b.negate()); } public Rational reciprocal() { return new Rational(den, num); } public Rational divides(Rational b) { Rational a = this; return a.times(b.reciprocal()); } // **************************************************************** public double toDouble() { return (double) num / den; } public String toString() { if (den == 1) return num + ""; else return num + "/" + den; } // return { -, 0, + } if a < b, a = b, or a > b public int compareTo(Rational b) { Rational a = this; int lhs = a.num * b.den; int rhs = a.den * b.num; if (lhs < rhs) return -1; if (lhs > rhs) return +1; return 0; } // is this Rational object equal to y? public boolean equals(Object y) { if (this == y) return true; if (y == null) return false; if (y.getClass() != this.getClass()) return false; Rational b = (Rational) y; return compareTo(b) == 0; } private static int gcf(int m, int n) { int retval = 0; if (m < 0) m = -m; if (n < 0) n = -n; if (n == 0) retval = m; else { int rem; while ((rem = m % n) != 0) { m = n; n = rem; } retval = n; } return retval; } // Not needed or used, but just cool to see @SuppressWarnings("unused") private static int lcm(int m, int n) { if (m < 0) m = -m; if (n < 0) n = -n; return m * n / gcf(m, n); } }
 /* * Skeleton Application Code for the Rational App * * Prof. A.C. Silvestri * CSCI-211 Intermediate Java Programming * 2/4/2019 */ 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.Pane; import javafx.scene.layout.VBox; import javafx.scene.shape.Line; import javafx.stage.Stage; public class RationalGUI extends Application { private static final int APPWIDTH = 720; private static final int APPHEIGHT = 300; private TextField tfANum; private TextField tfADen; private TextField tfBNum; private TextField tfBDen; private ComboBox operation; private TextField tfCNum; private TextField tfCDen; private Button btnCalculate; private Label lblComparison; private Label lblReciprocalA; private Label lblReciprocalB; @Override public void start(Stage primaryStage) { BorderPane bp = setupGUI(); btnCalculate.setOnAction(e -> doCalculation()); Scene scene = new Scene(bp, APPWIDTH, APPHEIGHT); primaryStage.setTitle("Rational GUI App"); primaryStage.setScene(scene); primaryStage.show(); primaryStage.setResizable(false); } private void doCalculation() { try { int num = Integer.parseInt(this.tfANum.getText()); int den = Integer.parseInt(this.tfADen.getText()); Rational a = new Rational(num, den); num = Integer.parseInt(this.tfBNum.getText()); den = Integer.parseInt(this.tfBDen.getText()); Rational b = new Rational(num, den); String op = this.operation.getValue(); Rational c = null; switch (op) { case "+": c = a.plus(b); break; case "-": c = a.minus(b); break; case "*": c = a.times(b); break; case "/": c = a.divides(b); break; } this.tfCNum.setText(String.valueOf(c.getNumerator())); this.tfCDen.setText(String.valueOf(c.getDenominator())); 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); Rational temp = a.reciprocal(); this.lblReciprocalA.setText("Reciprocal A: " + temp); temp = b.reciprocal(); this.lblReciprocalB.setText("Reciprocal B: " + temp); } catch (Exception ex) { this.tfCNum.setText("ERROR"); this.tfCDen.setText("ERROR"); this.lblComparison.setText(""); this.lblReciprocalA.setText(""); this.lblReciprocalB.setText(""); } } private BorderPane setupGUI() { final String COMPONENTSTYLE = "-fx-font-weight: bold; -fx-font-size: 16pt"; this.tfANum = new TextField(); this.tfANum.setStyle(COMPONENTSTYLE); this.tfADen = new TextField(); this.tfADen.setStyle(COMPONENTSTYLE); this.tfBNum = new TextField(); this.tfBNum.setStyle(COMPONENTSTYLE); this.tfBDen = new TextField(); this.tfBDen.setStyle(COMPONENTSTYLE); this.tfCNum = new TextField(); this.tfCNum.setStyle(COMPONENTSTYLE); this.tfCNum.setEditable(false); this.tfCDen = new TextField(); this.tfCDen.setStyle(COMPONENTSTYLE); this.tfCDen.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(); this.operation.getItems().addAll(new String[]{"+", "-", "*", "/"}); this.operation.setStyle(COMPONENTSTYLE); this.operation.setValue("+"); this.btnCalculate = new Button(" = "); this.btnCalculate.setStyle(COMPONENTSTYLE); //Build First Rational VBox vbR1 = new VBox(); vbR1.setPrefWidth(100); Pane linePane1 = new Pane(); linePane1.setPrefHeight(20); Line vinculum1 = new Line(0, 9, 100, 9); vinculum1.setStrokeWidth(3); linePane1.getChildren().add(vinculum1); vbR1.getChildren().addAll(tfANum, linePane1, tfADen); //Build Second Rational VBox vbR2 = new VBox(); vbR2.setPrefWidth(100); Pane linePane2 = new Pane(); linePane2.setPrefHeight(20); Line vinculum2 = new Line(0, 9, 100, 9); vinculum2.setStrokeWidth(3); linePane2.getChildren().add(vinculum2); vbR2.getChildren().addAll(tfBNum, linePane2, tfBDen); // Build Answer Rational VBox vbAns = new VBox(); vbAns.setPrefWidth(100); Pane linePane3 = new Pane(); linePane3.setPrefHeight(20); Line vinculum3 = new Line(0, 9, 100, 9); vinculum3.setStrokeWidth(3); linePane3.getChildren().add(vinculum3); vbAns.getChildren().addAll(tfCNum, linePane3, tfCDen); FlowPane fp = new FlowPane(20, 10); fp.setAlignment(Pos.CENTER); fp.setPadding(new Insets(5)); fp.getChildren().addAll( lblA, vbR1, this.operation, lblB, vbR2, this.btnCalculate, lblC, vbAns); this.lblComparison = new Label(); this.lblComparison.setStyle(COMPONENTSTYLE); this.lblComparison.setPrefWidth(APPWIDTH); fp.getChildren().add(this.lblComparison); this.lblReciprocalA = new Label(); this.lblReciprocalA.setStyle(COMPONENTSTYLE); this.lblReciprocalA.setPrefWidth(APPWIDTH); fp.getChildren().add(this.lblReciprocalA); this.lblReciprocalB = new Label(); this.lblReciprocalB.setStyle(COMPONENTSTYLE); this.lblReciprocalB.setPrefWidth(APPWIDTH); fp.getChildren().add(this.lblReciprocalB); Label lblStatus = new Label("Rational 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); } }
to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.