Question

java code

Write a class called FractionObject that represents a fraction with an integer numerator and denominator as fields. A Fractio

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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 )

//-------------------------------------------------------

The numberator of fl is5 Denominator cannot be 0 The denominator of f2 isi the decimal value of f3 is 0.16666667

//------------------------------------------------------

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;

    }

}

Add a comment
Know the answer?
Add Answer to:
java code Write a class called FractionObject that represents a fraction with an integer numerator and...
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
  • Write a class called RationalNumber that represents a fraction with an integer numerator and denominator. A...

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

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

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

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

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

  • Refer to this header file: // Fraction class // This class represents a fraction a /...

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

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

  • Lab 1.java only Goal: This lab will give you experience with defining and using classes and...

    Lab 1.java only Goal: This lab will give you experience with defining and using classes and fields, and with conditionals and recursive functions. Getting Started --------------- Read the Fraction.java class into a text editor and compile it filling in the command javac -g Fraction.java. The program should compile without errors. In a shell window, run the program using "java Fraction". The program should run, although it will print fractions in a non-reduced form, like 12/20. Part I: Constructors (1 point)...

  • Design a class named Fraction. This class is used to represent a ratio of two integers,...

    Design a class named Fraction. This class is used to represent a ratio of two integers, such as 6 / 9. Include accessors and mutators that allow the user to get and set the numerator and the denominator. Also include a member method that returns the value of the numerator divided by the denominator as double (for example, 0.666…). Include an additional member method that returns the value of the fraction reduced to lowest terms as string. For example, instead...

  • IN JAVA PLEASE HELP! ALSO PLEASE ADD COMMENTS Summary Build two classes (Fraction and Fraction Counter)...

    IN JAVA PLEASE HELP! ALSO PLEASE ADD COMMENTS Summary Build two classes (Fraction and Fraction Counter) and a Driver for use in counting the number of unique fractions read from a text file. We'll use the ArrayList class to store our list of unique Fraction Counters. Rather than designing a monolithic chunk of code in main like we did in the previous homework, we'll practice distributing our code into containers (called classes) that you will design specifically to tackle this...

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