Programming Tutorials

Handling Fractions in Java

By: Paawan Chaudhary in Java Tutorials on 2012-09-19  

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);
	}
}





Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Java )

Latest Articles (in Java)