Internet Technologies
main
main
  • Introduction
  • Contents
  • Practical List
  • HTML & CSS
    • Q1
    • Q2
    • Q3
    • Q4
    • Q5
  • JAVA Programs
    • Q6
    • Q7
    • Q8
    • Q9
    • Q10
    • Q11
    • Q12
  • JAVASCRIPT
    • Q13
    • Q14
    • Q15
  • JDBC
    • Q16
    • Q17
  • JSP
    • Side Note
    • Q18
    • Q19
    • Q20
    • Q21
    • Q22
    • Q23
  • End
Powered by GitBook
On this page
  • Create a class called Fraction that can be used to represent the ratio of two integers. Include appropriate constructors and methods. If the denominator becomes zero, throw and handle an exception.
  • Code
  • Output
  1. JAVA Programs

Q11

Question 11

PreviousQ10NextQ12

Last updated 4 years ago

Create a class called Fraction that can be used to represent the ratio of two integers. Include appropriate constructors and methods. If the denominator becomes zero, throw and handle an exception.

Code Insights

Created java_6.java, a new java class file and imported java.util.Scanner package to take user input, this program takes two numbers as input and tries to simplify the fraction as much and as simple it is possible, also throws error on division by zero.

Code

/**
 *Solution for Ans. 11
 * @author Jatin Kamboj 8647
 */
import java.util.Scanner;
class Frac {
    private int numerator;             // Fraction numerator
    private int denominator;           // Fraction denominator

    /*-----------------------------------------------------------------
     * constructor
     * If fraction is negative,
     * put negative number in numerator
     */
    public Frac(int num, int denom) {
        numerator = (denom < 0 ? -num : num);
        denominator = (denom < 0 ? -denom : denom);

    }

    /*-----------------------------------------------------------------
     * setNumerator
     * numerator is set to be the given parameter
     */
    public void setNumerator(int num) {
        numerator = num;

    }

    /*-----------------------------------------------------------------
     * getNumerator
     * return numerator
     */
    public int getNumerator() {
        return numerator;
    }

    /*-----------------------------------------------------------------
     * setDenominator
     * denominator is set to be the
     * given parameter (zero is ignored),
     * if denominator is negative,
     * numerator is adjusted
     */
    public void setDenominator(int denom) {
        try {
            if (denom > 0) {
                denominator = denom;

            }
            if (denom < 0) {
                numerator = -numerator;
                denominator = -denom;

            }

        } catch (ArithmeticException e) {
            System.out.println(e.getMessage() +
                    " Exception Condition occurred" +
                    " [Division by zero]");

        }
    }

    /*-----------------------------------------------------------------
     * getDenominator
     * return denominator
     */
    public int getDenominator() {
        return denominator;
    }

    /*-----------------------------------------------------------------
     * reduce
     * reduce Fraction to lowest terms
     * by finding largest common denominator
     * and dividing it out
     */
    public void reduce() {
        // find the larger of the numerator and denominator
        try {
            int x = 1 / denominator;

        } catch (ArithmeticException e) {
            System.out.println(e.getMessage() +
                    " Exception Condition occurred" +
                    " [Division by zero]");

        }


        int n = numerator,
                d = denominator,
                largest;

        if (numerator < 0) {
            n = -numerator;
        }
        if (n > d) {
            largest = n;
        } else {
            largest = d;
        }

        // find the largest number
        // that divide the numerator and
        // denominator evenly
        int gcd = 0;
        for (int i = largest; i >= 2; i--) {
            if (numerator % i == 0 &&
                    denominator % i == 0) {
                gcd = i;
                break;
            }
        }

        // divide the largest common denominator
        // out of numerator, denominator
        if (gcd != 0) {
            numerator /= gcd;
            denominator /= gcd;
        }
    }
}

public class java_6 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int choice = 0;
        System.out.println("\nThis program will depict" +
                " simplest ratio of fractions and " +
                "catch zero division error");

        while (choice <= 5) {
            System.out.print("\nEnter Numerator: ");
            int num = sc.nextInt();
            System.out.print("Enter Denominator: ");
            int den = sc.nextInt();
            Frac f = new Frac(num, den);
            f.setDenominator(den);
            f.setNumerator(num);
            f.reduce();
            choice = choice + 1;
            System.out.println("The Ratio => " +
                    f.getNumerator() +
                    " : " +
                    f.getDenominator());
        }
    }
}

Output

Try or Test The Corresponding Code Here

The above notebook runs on cloud, make sure to run the first cell of the notebook to set-up JDK and JRE on the cloud machine.

Browse Source Code
Google Colaboratory
Live Demo java_6.java
Logo
4KB
java_6.java
Download java_6.java
Image-1/2 output for java_6.java
Image-2/2 output for java_6.java