JavaFX Programming Using Scenebuilder with eclipse or another ide code this in JAVA code this program 02 - Rational GUI: Using the Rational class code from Lab 4, create a GUI which allows you to add, subtract, multiply, and divide Rational numbers by entering the numerator and denominator in separate fields. The results should also be displayed in two separate fields

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

JavaFX Programming

Using Scenebuilder with eclipse or another ide code this in JAVA code this program

02 - Rational GUI: Using the Rational class code from Lab 4, create a GUI which allows you to add, subtract, multiply, and divide Rational numbers by entering the numerator and denominator in separate fields. The results should also be displayed in two separate fields.

 

public class Rational {

// data

private int num;

private int den;

 

// constructors

/**

* Constructs a Rational from a numerator and denominator

*

* @param n the numerator

* @param d the denominator

*/

public Rational(int n, int d){

if (d==0) {

System.out.println("Cannot divide by 0");

d=1;

}

num = n;

den = d;

simplify();

}

 

/**

* Constructs a Rational from an integer

*

* @param i the integer. numerator=i, denominator = 1.

*/

public Rational(int i) {

this(i, 1);

}

 

// selectors

/**

* gets numerator

*

* @return numerator

*/

public int getNumerator(){

return num;

}

 

/**

* gets denominator

*

* @return denominator

*/

public int getDenominator(){

return den;

}

 

// manipulators

/**

* Changes numerator, leaving denominator the same

*

* @param n the new numerator

*/

public void setNumerator(int n) {

num = n;

simplify();

}

 

/**

* Changes denominator, leaving numerator the same

*

* @param d the new denominator

*/

public void setDenominator(int d) {

if (d==0)

System.out.println("Cannot divide by 0");

else {

den = d;

simplify();

}

}

 

// special methods

/**

* Formats a rational for Strings and printing

*

* @return if denominator==0, numerator. otherwise, numerator/denominator.

*/

public String toString(){

// use getNumerator() not num. allows easy implementation changes.

int n = getNumerator();

int d = getDenominator();

if (d==1)

return n+"";

else

return n + "/" +d;

}

 

/**

* Determines if two rational objects represent the same number.

* Both numerator and denominator must be the same.

*

* @param r the rational number to compare against

* @return true if same. false if not.

*/

public boolean equals(Object o) {

if (o instanceof Rational){

Rational r = (Rational) o;

return getNumerator()==r.getNumerator() &&

getDenominator()==r.getDenominator();

} else

return false;

}

 

// other methods

/**

* Multiplies one rational by another.

*

* @param r the rational number to multiply by

* @return the resulting rational

*/

public Rational multiply(Rational r) {

int n = getNumerator() * r.getNumerator();

int d = getDenominator() * r.getDenominator();

Rational product = new Rational(n, d);

return product;

}

/**

* Adds one rational by another.

*

* @param r the rational number to add

* @return the resulting rational

*/

public Rational add(Rational r) {

int n1 = getNumerator();

int n2 = r.getNumerator();

int d1 = getDenominator();

int d2 = r.getDenominator();

Rational product = new Rational(n1*d2 + n2*d1, d1*d2);

return product;

}

 

// utility methods, not for public use

private void simplify() {

int n = getNumerator();

int d = getDenominator();

int gcd = GCD(n, d);

num = n/gcd;

den = d/gcd;

}

 

private int GCD(int a, int b) {

if (b==0)

return a;

else

return GCD(b,a%b);

}

}

Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY