Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RatNum {
- private int num;
- private int den;
- public RatNum() {
- this.num = 0; //numerator
- this.den = 1; //denominator
- }
- public RatNum(int a) {
- // a denotes the numerator
- this.num = a;
- this.den = 1;
- }
- public RatNum(int a, int b) {
- if (b==0) {
- throw new NumberFormatException("Denominator = 0");
- }
- this.num = a;
- this.den = b;
- if (gcd(a,b) == 1) {
- int RN = a/b;
- } else {
- int gcdN = (a)/(gcd(a,b)); // gcdN = greatest common divisor, Numerator
- int gcdD = (b)/(gcd(a,b)); // gcdN = greatest common divisor, Denominator
- int RN = (gcdN)/(gcdD);
- }
- }
- public RatNum(RatNum r) { // %%%%%%%%%%%%%%%%%%%% SEEEEEEEEEEEEEEEEE HÄRRRRRRRRRRRRRRRRRR %%%%%%%%%%%%%%
- this.num = r.num;
- this.den = r.den;
- }
- public int getNumerator() {
- return this.num;
- }
- public int getDenominator() {
- return this.den;
- }
- public static int gcd(int a,int b) {
- if (a == 0 || b == 0) {
- throw new IllegalArgumentException("Please, enter a non-zero integer.");
- }
- if(a < 1) {
- a = a * (-1);
- }
- if(b < 0) {
- b = b * (-1);
- }
- int r = a % b;
- if (r==0) {
- return b;
- } else {
- a=b;
- b=r;
- return gcd(a,b);
- }
- }
- public static void main(String[] args) {
- System.out.println(gcd(4,8));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement