The Program ? Define a class called Fraction used to represent a ratio of two in
ID: 3567344 • Letter: T
Question
The Program
?
Define a class called Fraction used to represent a ratio of two integers.
?
The class should have one no-argument constructor that initializes the numerator of the fraction to 0 and the denominator to 1.
?
The class should also have a parameterized constructor that takes two integers as arguments: numerator and denominator, which will be used to initialize the numerator and denominator variables of the Fraction object.
?
Include accessor and mutator methods: getNumerator, setNumerator, getDenominator, setDenominator that allow the user to get and set the numerator and the denominator.
?
Include a method getValue that returns a double containing the decimal value of the fraction. (numerator divided by denominator)
?
Include a method getAuthor that returns a String containing your first and last name.
?
The program to be submitted for this minilab will actually only be the Fraction Class and its methods. It should not contain a main method.
?In order to test your Class, you will need to create a test program (with a main method) that creates Fraction objects using the new keyword and tests the various methods
?Do not submit your test program
Explanation / Answer
Here you go :)
//Replace the first and last names with your name.
public class Fraction
{
private int numerator,denomenator;
public Fraction()
{
this.numerator=0;
this.denomenator=1;
}
public Fraction(int numerator,int denomenator)
{
this.numerator=numerator;
this.denomenator=denomenator;
}
public int getNumerator() {
return this.numerator;
}
public int getDenomenator() {
return this.denomenator;
}
public void setNumerator(int numerator) {
this.numerator = numerator;
}
public void setDenomenator(int denomenator) {
this.denomenator = denomenator;
}
public double getValue()
{
return (double)this.numerator/this.denomenator;
}
public String getAuthor()
{
return "First Last name";
}
}
//Driver class
public class FractionDriver
{
public static void main(String[] args)
{
Fraction f1=new Fraction();
f1.setNumerator(10);
f1.setDenomenator(100);
System.out.println(f1.getValue());
Fraction f2=new Fraction(100,10);
System.out.println(f2.getValue());
System.out.println(f2.getAuthor());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.