Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class called RationalNumber that represents a fraction with an integer n

ID: 3902989 • Letter: W

Question

Write a class called RationalNumber that represents a fraction with an integer numerator and denominator. A RationalNumber object should have the following methods:

// Constructs a new rational number to represent the number, we will

// assume the denominator will not be 0.

public RationalNumber(int num, int den)

// Sets the numerator to 0 and the denominator to 1

public RationalNumber()

// Returns this rational number’s denominator value.

public int getDenominator()

// Returns this rational number’s numerator value.

public int getNumerator()

// Returns a String representation of this rational number, such as "3/5". // If the rational number is "4/1" then the method should return "4".

public String toString()

Please write in Java and on a simple level

Explanation / Answer

RationalNumber.java

public class RationalNumber {

private int numerator , denominator ;

public RationalNumber(int num, int den) {

numerator = num;

denominator = den;

}

public RationalNumber() {

numerator = 0;

denominator = 1;

}

public int getDenominator() {

return denominator;

}

public int getNumerator() {

return numerator;

}

public String toString() {

if(numerator % denominator == 0){

return (numerator/denominator)+"";

}

return numerator+"/"+denominator;

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote