Fractional numbers (java)

This easy program is a good practice for java classes and packages.
It is about creating, printing fractional numbers, extending and reducing them and simple fractions calculations (addition, subtraction, multiplication, division).


This program consists of  three main parts:

  • part (A): creating and printing fractions
  • part (B): extending and reducing fractions
  • part (C): simple fractions calculations.

package structure:



📢RecommendedPlease try to implement  the game on your own first, before looking at the code down below.


____________________________________________________

🎁Code:


1. Fraction class:

package fractionalNumbers.model;

public class Fraction {
    
    //PART (A)
        
    private int n = 0// numerator
    private int d = 1// denominator.
    
    public Fraction(int nint d) {
        this.n = n;
        this.d = d;
        this.reduce();
    }
    
    public Fraction() {
        this(01);
    }
    
    public void setN(int n) {
        this.n = n;
        this.reduce();
    }
    
    public int getN() {
        return this.n;
    }
    
    public void setD(int d) {
        this.d = d;
        this.reduce();
    }
    
    public int getD() {
        return this.d;
    }
    
    @Override
    public String toString() {
        return String.format("%d/%d"this.n, this.d);
    }
    
    //PART (B)
    private void reduce() {
        // choose the smallest number from the numerator and denominator. because it could be the greatest common factor.
        int gCF = this.n > this.d? this.d: this.n; // gCF = greatest common factor
        // if the numerator and denominator are divisible by gCF then it is the correct gCF
        // else we need to figure out how big the gCf is and it must be smaller than the numerator and denominator.
        for(;gCF > 1; gCF--)
            if(this.n % gCF == 0 && this.d % gCF == 0){
                this.n /= gCF;
                this.d /= gCF;
            }
        
    }
    
    public void extend(int x) {
        this.n *= x;
        this.d *= x;
    }
    
    //part(C) 
    public Fraction plus(Fraction f) {
        Fraction sum = new Fraction();
        sum.d = this.d * f.d;  //getting the common Denominator
        sum.n = (sum.d/this.d)*this.n + (sum.d/f.d)*f.n;
        sum.reduce();
        return sum;
    }
    
    public Fraction minus(Fraction f) {
        Fraction subtraction = new Fraction();
        subtraction.d = this.d * f.d;  //getting the common Denominator
        subtraction.n = (subtraction.d/this.d)*this.n - (subtraction.d/f.d)*f.n;
        subtraction.reduce();
        return subtraction;        
    }
    
    public Fraction times(Fraction f) {
        Fraction multiplication = new Fraction();
        multiplication.n = this.n * f.n;
        multiplication.d = this.d * f.d;
        multiplication.reduce();
        return multiplication;
    }
    
    public Fraction over(Fraction f) {
        //division is multiplying the first fraction with the reciprocal of the second fraction.
        Fraction reciprocal = new Fraction(f.d, f.n);
        Fraction division = this.times(reciprocal);
        division.reduce();
        return division;        
    }
    
}

2. Main class:



package fractionalNumbers;

import fractionalNumbers.model.Fraction;
import static java.lang.System.out;

class Main{
    
    public static void main(String[] args) {
        //part (A) 
        out.println("part (A): creating and printing fractions:");
        Fraction f1 = new Fraction(12);
        Fraction f2 = new Fraction(94);
        
        out.println(f1);
        out.println(f2);
        
        //part(B) 
        out.println("part (B): extending and reducing fractions:");
        Fraction f3 = new Fraction(125);
        out.println(f3);
        f3.setD(4); // number will be reduced automatically 12/4 ==> 3/1
        out.println(f3);
        f3.extend(3); // 3/1 ==> 9/3
        out.println(f3);
        
        //part(C) 
        out.println("part (C): simple fractions calculations.");
        Fraction f4 = new Fraction(23);
        Fraction f5 = new Fraction(12);
        out.println("simple fractions calculations with 2/3 and 1/2");
        
        Fraction sum = f4.plus(f5);
        out.println("sum = " + sum);
        Fraction subtraction = f4.minus(f5);
        out.println("subtraction = " + subtraction);
        Fraction multiplication = f4.times(f5);
        out.println("multiplication = " + multiplication);
        Fraction division = f4.over(f5);
        out.println("division = " + division);
        
    }



Output:

part (A): creating and printing fractions: 1/2 9/4 part (B): extending and reducing fractions: 12/5 3/1 9/3 part (C): simple fractions calculations. simple fractions calculations with 2/3 and 1/2 sum = 7/6 subtraction = 1/6 multiplication = 1/3 division = 4/3

_____________________________________________________




Comments