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

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

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

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

  • C++ Fraction calculator Need help with code, cant use "using namespace std" Objectives Create and use...

    C++ Fraction calculator Need help with code, cant use "using namespace std" Objectives Create and use functions. Use a custom library and namespace. Use reference variables as parameters to return values from functions. Create a robust user interface with input error checking. Use exceptions to indicate errors. Resources kishio.h kishio.cpp Assignment requirements You will need eight methods (including main). One is given to you. Their requirements are specified below: menu: The menu function takes no arguments, but returns a char...

  • I have provided a main method. Please add your fraction class. You need constructors, add, subtract,...

    I have provided a main method. Please add your fraction class. You need constructors, add, subtract, multiply, and divide methods, and a toString method. Your toString method needs to return the numerator followed by / followed by the denominator - NO spaces. DO NOT make any attempt to reduce the fractions (we shall do that later). Please add comments throughout. import java.util.Scanner; public class H4 { public static class Fraction { } public static void main(String[] args) { Fraction f=...

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

  • C++ I do not understand this problem. 14THE GREATEST COMMON DENOMINATOR PROBLEM Write a function named...

    C++ I do not understand this problem. 14THE GREATEST COMMON DENOMINATOR PROBLEM Write a function named god() that accepts two integers as input parameters and returns the greatest common divisor of the two numbers. The greatest common divisor (GCD) of two integers, a and b, is the largest integer that is a factor of both a and b. The GCD of any number and 1 is 1, and the GCD of any number and o is that number. One efficient...

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

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