Question

Declare a class that represents a fraction. Each fraction will be represented as two Integer values: numerator and denominato
using java 8
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program code in java:

//class definition
public class Fraction {
    //variable declaration
    int numerator, denominator;
  
    //constructor definition
    public Fraction(int numerator, int denominator) {
        this.numerator = numerator;
        this.denominator = denominator;
    }
    //method to add two fraction number
    void add(Fraction f){
        //calculating the common factor
        int den=hcf(this.denominator,f.denominator);
        //calculating the new denominator
        den = (this.denominator*f.denominator) / den;
        this.numerator = (this.numerator)*(den/this.denominator) + (f.numerator)*(den/f.denominator);
        this.denominator=den;
    }
    //method to add two fraction number
    void subtract(Fraction f){
        //calculating the common factor
        int den=hcf(this.denominator,f.denominator);
        //calculating the new denominator
        den = (this.denominator*f.denominator) / den;
        this.numerator = (this.numerator)*(den/this.denominator) - (f.numerator)*(den/f.denominator);
        this.denominator=den;
    }
    //method to add two fraction number
    void multiply(Fraction f){
        //calculating new numerator and denominator by multiplying
        this.numerator = this.numerator * f.numerator;
        this.denominator=this.denominator * f.denominator;
    }
    //method to add two fraction number
    void divide(Fraction f){
        //calculating new numerator by multiplying with denominator and vice versa
        this.numerator = this.numerator * f.denominator;
        this.denominator=this.denominator * f.numerator;
    }
    //method to calculate the common factor
    int hcf(int den1,int den2){
        //recursive function to find the common factor
        if (den1 == 0)
            return den2;
        return hcf(den2%den1, den1);
    }
  
    //function to simplify the fraction
    void simplify(){
        // Finding hcf of both terms
        int cf = hcf(this.numerator,this.denominator);
        // simplifying numerator and denominator
        this.numerator = this.numerator/cf;
        this.denominator = this.denominator/cf;
    }
    //function to format the output
    @Override
    public String toString() {
        simplify();
        return numerator + "/" + denominator ;
    }
  
    //main method definition
    public static void main(String[] args) {
        //creating object of the fraction
        Fraction f1=new Fraction(2, 5);
        Fraction f2=new Fraction(3, 8);
        Fraction f3=new Fraction(4, 7);
        Fraction f4=new Fraction(18, 21);
        //calling function to add two fraction number
        f1.add(f2);
        //printing the result
        System.err.println(f1);
        //calling function to subtract two fraction number
        f2.subtract(f3);
        //printing the result
        System.err.println(f2);
        //calling function to multiply two fraction number
        f2.multiply(f3);
        //printing the result
        System.err.println(f2);
        //calling function to divide two fraction number
        f3.divide(f4);
        //printing the result
        System.err.println(f3);
    }
}


Output:

Output - January (run) X run: 31/40 -11/56 11/-98 2/3 BUILD SUCCESSFUL (total time: 0 seconds)

Add a comment
Know the answer?
Add Answer to:
using java 8 Declare a class that represents a fraction. Each fraction will be represented as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with the following Java code Consider a class Fraction of fractions. Each fraction...

    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...

  • java only no c++ Write a Fraction class whose objects will represent fractions. You should provide...

    java only no c++ Write a Fraction class whose objects will represent fractions. You should provide the following class methods: Two constructors, a parameter-less constructor that assigns the value 0 to the Fraction, and a constructor that takes two parameters. The first parameter will represent the initial numerator of the Fraction, and the second parameter will represent the initial denominator of the Fraction. Arithmetic operations that add, subtract, multiply, and divide Fractions. These should be implemented as value returning methods...

  • java code Write a class called FractionObject that represents a fraction with an integer numerator and...

    java code Write a class called FractionObject that represents a fraction with an integer numerator and denominator as fields. A Fraction Object object should have the following methods: A constructor with int numerator, int denominator as parameters - Constructs a new fraction object to represent the ratio (numerator/denominator). The denominator cannot be 0, so output a message if O is passed and set the numerator and denominator to 1. A constructor with no parameters - Constructs a new FractionObject to...

  • Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int...

    Header file for the Rational class: #ifndef RATIONAL_H #define RATIONAL_H class Rational { public: Rational( int = 0, int = 1 ); // default constructor Rational addition( const Rational & ) const; // function addition Rational subtraction( const Rational & ) const; // function subtraction Rational multiplication( const Rational & ) const; // function multi. Rational division( const Rational & ) const; // function division void printRational () const; // print rational format void printRationalAsDouble() const; // print rational as...

  • Need help with Java for Fraction exercise

    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...

  • C++ Create a Rational Number (fractions) class like the one in Exercise 9.6 of the textbook....

    C++ Create a Rational Number (fractions) class like the one in Exercise 9.6 of the textbook. Provide the following capabilities: Create a constructor that prevents a 0 denominator in a fraction, reduces or simplifies fractions (by dividing the numerator and the denominator by their greatest common divisor) that are not in reduced form, and avoids negative denominators. Overload the addition, subtraction, multiplication, and division operators for this class. Overload the relational and equality operators for this class. Provide a function...

  • Please use Java language. Thanks in advance. HOME WORK due 09. 18.2019 (Rational Numbers) Create a...

    Please use Java language. Thanks in advance. HOME WORK due 09. 18.2019 (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to 2...

  • In C# programming. Create a fractions class that represents fractions in the form a/b Your class...

    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...

  • (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to...

    (Rational Numbers) Create a class called Rational for performing arithmetic with fractions. Write a program to test your class. Use integer variables to represent the private instance variables of the class- the numerator and the denominator. Provide a constructor that enables an object of this class to be initialized when it's declared. The constructor should store the fraction in reduced form. The fraction 2/4 is equivalent to h and would be stored in the object as 1 in the numerator...

  • Using Python 3.7 and higher, implement function egypt(numerator, denominator) that uses the greedy strategy to determine...

    Using Python 3.7 and higher, implement function egypt(numerator, denominator) that uses the greedy strategy to determine a set of distinct Egyptian fractions that sum to numerator/denominator. You cannot use any sort of division, including the floor and ceiling methods, nor the division, remainder, or modulus operators. 1. Implement function egypt(numerator, denominator) that uses the greedy strategy presented in slides 7 and 8 of Lecture 30 (April 13) to determine a set of distinct (i.e. all different) Egyptian fractions that sum...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT