I have a question in class that is due tomorrow. I need help trying to figure ou
ID: 3542983 • Letter: I
Question
I have a question in class that is due tomorrow. I need help trying to figure out where I'm going wrong and completeing it.
Question:
Define a class called Fraction. This class is used to represent a ratio of two integers. Include mutotor methods that allow the user to set the numberator and the denominator. Also include a method that returns the value of numerator divided by denominator as a double. Include an additional method that outputs the value of the fraction reduced to lowest terms (e.g, instead of outputting 20/60, the method should output 1/3). This will require finding the greatest common divisor for the numerator and denominator, then dividing both by that number. Embed your class in a test program.
This is what I have so far....
package fraction;
public class Fraction {
private int numerator;
private int denominator;
//blank constructor
public void Fraction()
{
numerator = 0;
denominator = 1;
}
// constructor method
public void Fraction(int n, int d)
{
numerator = n;
denominator = d;
{
//mutator method
public void setNumerator(int n)
{
numerator = n;
}
public void setDenominator(int d)
{
denominator = d;
}
public void displayAsRatio()
{
System.out.println(numerator + ":" + denominator); //ratio uses a colon
}
}
}
}
Explanation / Answer
import java.math.*;
public class Fraction {
int d, n;
//public String toString();
Fraction (int n, int d) {
// numerator = n; denominator = d;
getFraction(n + "/" + d);
}
public Fraction() {
}
public static void main(String[] args) {
}
// Converts fraction to a string format n/d
public String displayFraction(){
String fraction = (n + "/" + d);
return fraction;
}
// mutator
public String setFraction(int newFraction) {
return newFraction.toString();
}
}
//public class fraction {
//private double n ();
//public void setNumerator(double newNumberator){
// n = newNumerator;
//}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.