please drop a comment if there's an issue. the code is divided into two class- 1) FractionObject (the required class), 2) Main (the class that demonstrates )
//-------------------------------------------------------

//------------------------------------------------------
public class Main {
public static void main(String[] args) {
// creates an object with numberator=5, and denominater=2
FractionObject f1 = new FractionObject(5, 2);
System.out.println("The numberator of f1 is" + f1.getNumerator());
// creates an object with numberator=3, and denominater=0
FractionObject f2 = new FractionObject(3, 0);
System.out.println("The denominator of f2 is" + f2.getDenominator());
// creates an object with numberator=1, and denominater=6
FractionObject f3 = new FractionObject(1, 6);
System.out.println("the decimal value of f3 is " + f3.getDecimal());
}
}
// the required class
class FractionObject {
// member variables
int numerator;
int denominator;
// the required constructor that takes 2 parameters
FractionObject(int numerator, int denominator) {
// checks if denominator==0
if (denominator == 0) {
System.out.println("Denominator cannot be 0");
// sets the values to 1
this.numerator = 1;
this.denominator = 1;
} else {
this.numerator = numerator;
this.denominator = denominator;
}
}
FractionObject() {
this.numerator = 1;
this.denominator = 0;
}
int getNumerator() {
return numerator;
}
int getDenominator() {
return denominator;
}
// returns the decimal value of the fraction
float getDecimal() {
// calcultes the decimal value
float decimal = this.numerator / (float) this.denominator;
return decimal;
}
}
java code Write a class called FractionObject that represents a fraction with an integer numerator and...
Write a class called RationalNumber that represents a fraction with an integer numerator and denominator. A RationalNumber object should have the following methods: public RationalNumber(int numerator, int denominator) Constructs a new rational number to represent the ratio (numerator/denominator). The denominator cannot be 0, so throw an IllegalArgumentException if 0 is passed. public RationalNumber() Constructs a new rational number to represent the ratio (0/1). public int getDenominator() Returns this rational number’s denominator value; for example, if the ratio is (3/5), returns...
I need help with the following Java code Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a denominator that are integers. Your class should be able to add, subtract, multiply, and divide two fractions. These methods should have a fraction as a parameter and should return the result of the operation as a fraction. The class should also be able to find the reciprocal of a fraction, compare two fractions, decide whether two...
In C++ Fix any errors you had with HW5(Fraction class). Implement a function(s) to help with Fraction addition. \**************Homework 5 code*****************************/ #include<iostream> using namespace std; class Fraction { private: int wholeNumber, numerator, denominator; public: //get methods int getWholeNumber() { return wholeNumber; } int getNumerator() { return numerator; } int getDenominator() { return denominator; } Fraction()// default constructor { int w,n,d; cout<<"\nEnter whole number : "; cin>>w; cout<<"\nEnter numerator : "; cin>>n; cout<<"\nEnter denominator : "; cin>>d; while(d == 0)...
Rational will be our parent class that I included to this post, Implement a sub-class MixedRational. This class should Implement not limited to: 1) a Constructor with a mathematically proper whole, numerator and denominator values as parameters. 2) You will override the: toString, add, subtract, multiply, and divide methods. You may need to implement some additional methods, enabling utilization of methods from rational. I have included a MixedRational class with the method headers that I used to meet these expectations....
Create a new class named Fraction that models fractions, such as 2/3 or 45/9. Implement the following API: Constructor: Fraction(int numerator, int denominator) . String toString() so instances of the class can be printed. The format used above is sufficient. Fraction simplify() This method returns a new Fraction that is arithmetically equal to that given, but for which the numerator and denominator have no common factors. Create a testing program, TestFraction, that prompts the user to provide the numerator and...
Add another public method called add to your Fraction class. This method adds another fraction to the ‘calling object’. Thus, the method will take a Fraction class object as a parameter, add this parameter fraction to the calling object (fraction), and return a Fraction object as a result. HINT: We can use cross multiplication to determine the numerator of the resultant Fraction. The denominator of the resultant Fraction is simply the multiplication of the denominators of the two other Fractions.Add...
Create a fraction class. This will have two attributes, a numerator and a denominator, both int. This class will have constructors accessors and mutators (for the attributes) a toString method which will allow us to print the fraction in the form 3/4 an Add method so we can add two fractions a subtract method (subtracts one fraction from the other) a multiply method (multiply two fractions) a divide method (divides one fraction by the other) DO NOT (for now) worry...
Refer to this header file: // Fraction class // This class represents a fraction a / b class Fraction { public: // Constructors Fraction(); // sets numerator to 1 and denominator to 1 Fraction(int num, int denom); // Setters void setNumerator(int num); void setDenominator(int denom); // getters int getNumerator()const {return num;} int getDenominator()const {return denom;} double getDecimal(){return static_cast<double> num / denom;} private: int num, denom; }; 1.Write the code for the non-member overloaded << operator that will display all of...
In C# programming. Create a fractions class that represents fractions in the form a/b Your class should implement the following members: int Numerator int Denominator Fraction(int numerator, int denominator) creates a new Fraction double ToDecimal() returns the fraction as a double Fraction Add(Fraction f) adds the fraction to the one passed in and simplifies the result Fraction Multiply(Fraction f) multiplies the fraction by the one passed in and simplifies the result Fraction Simplify() simplifies the...
This in in C# There are two classes, class Fraction and class FractionDemo The user needs to be able to input a value for the numerator and the denominator Create a Fraction class with private fields that hold a positive int numerator and a positive int denominator. In addition, create Properties for each field with the set mutator such that the numerator is greater than or equal to 0 and the denominator is greater than 0 (illegal values should be...