in Java how would i add the println that i have main so that it prints in the constructor the same thing import java.util.*; public class Rational{ int n; int d; Rational(int num,int den){ if(den==0){...


in Java


how would i add the println that i have main so that it prints in the constructor the same thing



import java.util.*;
public class Rational{
    int n;
    int d;

    Rational(int num,int den){
        if(den==0){
        throw new ArithmeticException("Can't divide by 0");
        }
        if(num==0 && den!=0)
        {
            num =0;
            den=1;
        }
        if(den<0 &&="" num="">0)
        {
            den = den*(-1);
            num = num *(-1);
        }
        if(den<0 &&=""><>
        {
            den = den*(-1);
            num = num *(-1);
        }
        int gcd =1;
        for(int i = 1; i <= num="" &&="" i=""><= den;="">
        {
        if(num%i==0 && den%i==0)
        gcd = i;
        }
      num = num / gcd;
      den = den/gcd;
      this.n=num;
      this.d=den;

    }
    void print(){
        System.out.print(this.n+"/"+this.d);

    }


     public static void main(String []args){
        Scanner sc= new Scanner(System.in);
        int a= sc.nextInt();
        int b= sc.nextInt();
    System.out.println("Constructor called with n = "+a+" and d = "+b);
        Rational r1 = new Rational(a,b);
        System.out.print("Result is : ");
       r1.print();
     }
}



Jun 09, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here