Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

ECLIPSE: Develop a Fractionclass that is capable of doing arithmetic with fractions. Fractions are held in lowest terms, that is, you should divide out any

ECLIPSE: Develop a Fractionclass that is capable of doing arithmetic with fractions. Fractions are held in lowest terms, that is, you should divide out any common multiple of the numerator and denominator. The gcd()method will help with this. You should complete the implementations of all the class methods (including the constructors). Also create a TestFractionclass and paste in the code shown below. Your Fractionclass should produce the correct results when used in TestFraction.

public class Fraction {

int numerator;

int denominator;

Fraction() { // numerator = denominator = 1

// add code here

}

Fraction(int n, int d) {

// add code here

}

// greatest common divisor:

int gcd(int a, int b) {

if (b == 0)

return (a);

else

return (gcd(b, a % b));

}

public String toString() {

// add code here

}

String toDecimal() {

// add code here

}

Fraction add(Fraction f) {

// add code here

}

}

public class TestFraction {

public static void main(String[] args) {

Fraction f1 = new Fraction();

Fraction f2 = new Fraction(1, 3);

Fraction f3 = new Fraction(3, 6);

System.out.println("f1 = " + f1);

System.out.println("f2 = " + f2);

System.out.println("f3 = " + f3);

System.out.println("f1 + f2 = " + f1.add(f2));

System.out.println("f2 in decimal is: " + f2.toDecimal());

}

}

With my classes, this prints

f1 = 1/1

f2 = 1/3

f3 = 1/2

f1 + f2 = 4/3

f2 in decimal is: 0.33333334

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Logics For Databases And Information Systems

Authors: Jan Chomicki ,Gunter Saake

1st Edition

1461375827, 978-1461375821

More Books

Students also viewed these Databases questions

Question

107 MA ammeter 56 resistor ? V voltmeter

Answered: 1 week ago

Question

Generally If Drug A is an inducer of Drug B , Drug B levels will

Answered: 1 week ago