In: Computer Science
Objectives:
1. Create classes to model objects
Instructions:
1. Create a new folder named Lab6 and save all your files for this lab into this new folder.
2. You can use any IDE (such as SciTE, NetBeans or Eclipse) or any online site (such as repl.it or onlinegdb.com) to develop your Java programs
3. All your programs must have good internal documentation. For information on internal documentation, refer to the lab guide.
Problem : The Fraction class (Filename: TestFraction.java)
Design a class named Fraction. This class is used to represent a ratio of two integers, such as 6 / 9. Include accessors and mutators that allow the user to get and set the numerator and the denominator. Also include a member method that returns the value of the numerator divided by the denominator as double (for example, 0.666…). Include an additional member method that returns the value of the fraction reduced to lowest terms as string. For example, instead of returning 6 / 9, the function should return 2 / 3 as a string. This will require finding the greatest common divisor for the numerator and the denominator, and then dividing both by that number. Include a private method that returns the greatest common divisor of two integers.
Write a test program that tests several examples of fractions and displays the results.
Program code:
class Fraction
{
int numerator,denominator;
double res;
int
getNumerator()
//Method to get Numerator
{
return numerator;
}
void setNumerator(int
numerator)
//Method to set Numerator
{
this.numerator=numerator;
}
int
getDenominator()
//Method to get Denominator
{
return denominator;
}
void setDenominator(int
denominator)
//Method to set Denominator
{
this.denominator=denominator;
}
double
result()
//Method to return the fractional value in double datatype.
{
res=(double)numerator/(double)denominator;
return res;
}
String
reducedFraction()
//Method to
return the reduced fraction after
numerator,denominator
divisible by gcd;
{
int gcd=gcd(numerator,denominator);
int reduced_numerator=numerator/gcd;
int reduced_denominator=denominator/gcd;
String
reduced_Fraction=reduced_numerator+"/"+reduced_denominator;
return reduced_Fraction;
}
private static int gcd(int numerator,int
denominator)
//Method to get gcd of numerator and denominator;
{
int dividend=numerator;
int divisor=denominator;
int rem;
int gcd;
while(true)
{
rem=dividend%divisor;
if(rem==0)
{
gcd=divisor;
break;
}
else
{
dividend=divisor;
divisor=rem;
}
}
return gcd;
}
}
class Main
{
public static void main(String []
args)
//main method
{
Fraction obj=new Fraction();
obj.setNumerator(6);
obj.setDenominator(9);
System.out.println("\nNumerator is : "+obj.getNumerator());
System.out.println("Denominator is : "+obj.getDenominator());
System.out.println("Fraction
is:"+obj.getNumerator()+"/"+obj.getDenominator());
System.out.println(obj.getNumerator()+" / "+obj.getDenominator()+"
= " + obj.result());
System.out.println("Reduced Fraction
is:"+obj.reducedFraction());
}
}
Output: