Question

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 of returning 6 / 9, the function should return 2 / 3 as a string. This will require finding the greatest common divisor for the numerator and the denominator, and then dividing both by that number. Include a private method that returns the greatest common divisor of two integers. Write a test program that tests several examples of fractions and displays the results. java is programming language,

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

Please find the answer below.
Please do comments in case of any issue. Also, don't forget to rate the question. Thank You So Much.

Fraction.java

package c14;

public class Fraction {
   private int numerator;
   private int denominator;
   Fraction(int num,int den){
       numerator = num;
       denominator = den;
   }

   public void showFractionForm(){
       int partA = numerator/denominator;
       int partB = numerator%denominator;
       if(partA!=0){
           System.out.println(partA+"("+partB+"/"+denominator+")");
       }else{
           System.out.println(partB+"/"+denominator);
       }
   }

   public void determineLowestTerm(){
       int gcd=0;

       if(numerator==0){
           denominator = 1;
           System.out.println(numerator+"/"+denominator);
           return;
       }
       for (int i = 1; i <= numerator; i ++)
       {
           if (numerator%i == 0 && denominator%i == 0)
           {
               gcd = i;
           }
       }

       numerator = numerator / gcd;
       denominator = denominator / gcd;
       System.out.println(numerator+"/"+denominator);
   }

   public void determineDecimalEquivalent(){
       double value = numerator*1.0/denominator;
       System.out.println(value);
   }
  

   public static void main(String[] args) {
       Fraction f1 = new Fraction(6, 9);
       System.out.println("Fraction(6, 9).....\n");
       System.out.println("Fraction form is ");
       f1.showFractionForm();
       System.out.println("\nLowest term is ");
       f1.determineLowestTerm();
       System.out.println("\nDecimal equivalent is ");
       f1.determineDecimalEquivalent();
   }

}

- Console x ** Ek <terminated> Fraction (3) [Java Application] C:\P Fraction (6, 9).... ominator) Fraction form is 6/9 is 0)

Add a comment
Know the answer?
Add Answer to:
Design a class named Fraction. This class is used to represent a ratio of two integers,...
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
  • Submission Instruction Complete the following C++ programs. The assignment contains only one file with all different...

    Submission Instruction Complete the following C++ programs. The assignment contains only one file with all different class and functions from problem 1. The main function calls different functions as instructed in the problem description. Submit the CPP file during submission Problem 1. Define a class for a type called Fraction. This class is used to represent a ratio of two integers. Include mutator functions that allow the user to set the numerator and the denominator (one for each data). Also...

  • Create a new class named Fraction that models fractions, such as 2/3 or 45/9. Implement the...

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

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

  • Consider a class Fraction of fractions. Each fraction is signed and has a numerator and a...

    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 fractions are equal, and convert a fraction to...

  • Give answer according to fill in the blanks. 1.Define a class called Fraction . This class...

    Give answer according to fill in the blanks. 1.Define a class called Fraction . This class is used to represent a ratio of two integers.Include mutator methods that allow the user to set the numerator and the denominator. Also include a method that returns the value of numerator divided by denominator as a double . Include an additional method that outputs the value of the fraction reduced to lowest terms (e.g., instead of outputting 20/60, the method should output 1/3)....

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

  • Construct a class named Fractions containing two integer data members named num and denom, used to...

    Construct a class named Fractions containing two integer data members named num and denom, used to store the numerator and denominator of a fraction having the form num/denom. Your class should include a default constructor that initializes num and denom to 1 if there's no user initialization, and it must prohibit a 0 denominator value. In addition, create member functions for displaying an object's data values and overloaded operator functions for adding, subtracting, multiplying, and dividing two Fraction objects, as...

  • Create a fraction class. This will have two attributes, a numerator and a denominator, both int....

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

  • Rational Number *In Java* A rational number is one that can be expressed as the ratio...

    Rational Number *In Java* A rational number is one that can be expressed as the ratio of two integers, i.e., a number that can be expressed using a fraction whose numerator and denominator are integers. Examples of rational numbers are 1/2, 3/4 and 2/1. Rational numbers are thus no more than the fractions you've been familiar with since grade school. Rational numbers can be negated, inverted, added, subtracted, multiplied, and divided in the usual manner: The inverse, or reciprocal of...

  • This in in C# There are two classes, class Fraction and class FractionDemo The user needs...

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

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