Rational Number
Rational Number
/**
* Write a description of class RationalNumber here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class RationalNumber
{
int p;//This variable stores the numeric value of the numerator
int q;//This variable stores the numeric value of the denominator
//This is a default constructor that assigns the value to the instance variables
public RationalNumber()
{
int p = 0;
int q = 1;
}
//This is a parameterised constructor that assigns the value to the instance variables
public RationalNumber(int p , int q)
{
this.p = p;
if(q >= 1)
this.q = q;
else
this.q = 1;
}
//This function returns the decimal value of the given rational number
float decimalValue()
{
float f = ((float)p) / q;
System.out.print(f);
return f;
/**
* Output:
* 0.6666667
*/
}
//This function simplifies the rational number by the gcd of the numerator and
denominator
void simplify()
{
int a = Math.min(p , q);
/**
* Output:
* 2/3
*/
}