Handling Fractions in Java

By: Paawan Chaudhary Viewed: 154548 times  Printer Friendly Format    


This Java program defines a class Fraction having data members numerator and denominator. Initializes three objects using different constructors and displays its fractional value.

import java.lang.*;
import java.io.*;
class Fraction
{
	double numerator,denominator,fraction;
	Fraction (int a, double b)
	{
		numerator=a;
		denominator=b;
		fraction=numerator/denominator;
		System.out.println ("Fraction1 = "+fraction);
	}
	Fraction (int x, int y)
	{
		numerator=x;
		denominator=y;
		fraction=numerator/denominator;
		System.out.println ("Fraction2 = "+fraction);
	}
	Fraction(double m, double n)
	{
		numerator=m;
		denominator=n;
		fraction=numerator/denominator;
		System.out.println ("Fraction3 = "+fraction);
	}
}
class q3Fraction
{
	public static void main(String args[])
	{
		Fraction f = new Fraction(12,12.3);
		Fraction f2 = new Fraction(10,12);
		Fraction f3 = new Fraction(10.5,13.5);
	}
}


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial