Question

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 that return a Fraction object. They should be named addedTo, subtract, multipliedBy, and dividedBy.

A boolean operation named isEqualTo that compares two Fraction objects for equality.

An output operation named print that displays the value of a Fraction object on the screen in the form numerator/denominator.

Your class should have exactly two private data members, one to represent the numerator of the fraction being represented, and one to represent the denominator of the fraction being represented.

When adding or subtracting Fractions, remember that you must first find the common denominator. The easy way to do this is to multiply the denominators together and use that product as the common denominator.

I am providing a client program for you below. You should copy and paste this into a file and use it as your client program. The output that should be produced when the provided client program is run with your class is also given below, so that you can check your results. Since you are not writing the client program, you are not required to include comments in it.

I strongly suggest that you design your class incrementally. For example, you should first implement only the constructors and the output method, and then test what you have so far. Once this code has been thoroughly debugged, you should add additional class methods, testing each one thoroughly as it is added. You might do this by creating your own client program to test the code at each stage; however, it would probably be better to use the provided client program and comment out code that relates to class methods that you have not yet implemented.

As you can see from the sample output given below, you are not required to change improper fractions into mixed numbers for printing. Just print it as an improper fraction. You are, however, required to reduce fractions, as illustrated in the sample output. Make sure that your class will reduce ANY fraction, not just the fractions that are tested in the provided client program. Fractions should not be simply reduced upon output, they should be stored in reduced form at all times. In other words, you should ensure that all fraction objects are reduced before the end of any class method. You are also not required to deal with negative numbers, either in the numerator or the denominator.

You must create your own algorithm for reducing fractions. Don't look up an already existing algorithm for reducing fractions or finding GCF. The point here is to have you practice solving the problem on your own. In particular, don't use Euclid's algorithm. Don't worry too much about efficiency, just create something of your own that works correctly on ANY fraction.

Here is the client program. It must be in a separate file from your class.

public class ChangeThisClassName {
    public static void main(String[] args) {
        Fraction f1 = new Fraction(9,8);
        Fraction f2 = new Fraction(2,3);
        Fraction result = new Fraction();

        System.out.print("The result starts off at ");
        result.print();
        System.out.println();

        System.out.print("The product of ");
        f1.print();
        System.out.print(" and ");
        f2.print();
        System.out.print(" is ");
        result = f1.multipliedBy(f2);
        result.print();
        System.out.println();

        System.out.print("The quotient of ");
        f1.print();
        System.out.print(" and ");
        f2.print();
        System.out.print(" is ");
        result = f1.dividedBy(f2);
        result.print();
        System.out.println();

        System.out.print("The sum of ");
        f1.print();
        System.out.print(" and ");
        f2.print();
        System.out.print(" is ");
        result = f1.addedTo(f2);
        result.print();
        System.out.println();

        System.out.print("The difference of ");
        f1.print();
        System.out.print(" and ");
        f2.print();
        System.out.print(" is ");
        result = f1.subtract(f2);
        result.print();
        System.out.println();

        if (f1.isEqualTo(f2)){
            System.out.println("The two Fractions are equal.");
        } else {
            System.out.println("The two Fractions are not equal.");
        }
        
        Fraction f3 = new Fraction(12, 8);
        Fraction f4 = new Fraction(202, 303);
        result = f3.multipliedBy(f4);
        System.out.print("The product of ");
        f3.print();
        System.out.print(" and ");
        f4.print();
        System.out.print(" is ");
        result.print();
        System.out.println();
    }
}

This client should produce the output shown here:

The result starts off at 0/1
The product of 9/8 and 2/3 is 3/4
The quotient of 9/8 and 2/3 is 27/16
The sum of 9/8 and 2/3 is 43/24
The difference of 9/8 and 2/3 is 11/24
The two fractions are not equal.
The product of 3/2 and 2/3 is 1/1

You may not change the client program in any way, except that you may change the name of the main class. Changing the client program in any other way will result in a grade of 0 on the assignment.

You do not need to submit your client file. Execute your program and copy/paste the output into the bottom of your Fraction.java file, making it into a comment. Use the Assignment Submission link to submit the file. When you submit your assignment there will be a text field in which you can add a note to me (called a "comment", but don't confuse it with a Java comment). In this "comments" section of the submission page let me know whether the program works as required.

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

//Fraction.java

public class Fraction {

   private int numerator;

   private int denominator;

   Fraction(int n, int d) {

       numerator = n;

       denominator = d;

   }

   public Fraction() {

       numerator = 0;

       denominator = 1;

   }

   public String toString() {

       return (numerator + "/" + denominator);

   }

   public double toDecimal() {

       double num = numerator;

       double den = denominator;

       return (num / den);

   }

   public String reduce() {

       int g = gcd(numerator, denominator);

       numerator = numerator / g;

       denominator = denominator / g;

       return toString();

   }

   public void invert() {

       int g = numerator;

       numerator = denominator;

       denominator = g;

   }

   int gcd(int p, int q) {

       if (p == 0)

           return q;

       return gcd(q % p, p);

   }

  

   public Fraction addedTo(Fraction a) {

       int num = this.numerator * a.denominator + this.denominator*a.numerator;

       int den = this.denominator*a.denominator;

      

       Fraction r = new Fraction(num, den);

       r.reduce();

       return r;

   }

  

   public Fraction subtract(Fraction a) {

       int num = this.numerator * a.denominator - this.denominator*a.numerator;

       int den = this.denominator*a.denominator;

      

       Fraction r = new Fraction(num, den);

       r.reduce();

       return r;

   }

  

   public Fraction multipliedBy(Fraction a) {

       int num = this.numerator *a.numerator;

       int den = this.denominator*a.denominator;

      

       Fraction r = new Fraction(num, den);

       r.reduce();

       return r;

   }

  

   public Fraction dividedBy(Fraction a) {

       int num = this.numerator * a.denominator;

       int den = this.denominator*a.numerator;

      

       Fraction r = new Fraction(num, den);

       r.reduce();

       return r;

   }

  

   public void print() {

       this.reduce();

       System.out.print(this.numerator + "/" + this.denominator);

   }

  

   public boolean isEqualTo(Fraction f) {

       return this.numerator==f.numerator && this.denominator==f.denominator;

   }

}

----------------------------------------------------------------------------------------------------------------------------------------


public class FractionTest {

public static void main(String[] args) {

Fraction f1 = new Fraction(9,8);

Fraction f2 = new Fraction(2,3);

Fraction result = new Fraction();

System.out.print("The result starts off at ");

result.print();

System.out.println();

System.out.print("The product of ");

f1.print();

System.out.print(" and ");

f2.print();

System.out.print(" is ");

result = f1.multipliedBy(f2);

result.print();

System.out.println();

System.out.print("The quotient of ");

f1.print();

System.out.print(" and ");

f2.print();

System.out.print(" is ");

result = f1.dividedBy(f2);

result.print();

System.out.println();

System.out.print("The sum of ");

f1.print();

System.out.print(" and ");

f2.print();

System.out.print(" is ");

result = f1.addedTo(f2);

result.print();

System.out.println();

System.out.print("The difference of ");

f1.print();

System.out.print(" and ");

f2.print();

System.out.print(" is ");

result = f1.subtract(f2);

result.print();

System.out.println();

if (f1.isEqualTo(f2)){

System.out.println("The two Fractions are equal.");

} else {

System.out.println("The two Fractions are not equal.");

}

  

Fraction f3 = new Fraction(12, 8);

Fraction f4 = new Fraction(202, 303);

result = f3.multipliedBy(f4);

System.out.print("The product of ");

f3.print();

System.out.print(" and ");

f4.print();

System.out.print(" is ");

result.print();

System.out.println();

}

}
--------------------------------------------------------------------------------------------------------------
See Output
4 class Fraction [ private int numerator; private int denominator; Fraction(int n, int d) 5 denominator d; 7 8e numerator-n; 10 12 public Fraction(O numerator - 0; denominator - 1; 14 15 16 17 A 18public String toStringO) f 19 return (numerator + / + denominator); 20 E Console 3 <terminated> FractionTest [Java Application] /Library/Java/JavaVirtualMachines/jdk1 .8.0-18 1.jdk/Contents/Home/bi The result starts off at 0/1 The product of 9/8 and 2/3 is 3/4 The quotient of 9/8 and 2/3 is 27/16 The sum of 9/8 and 2/3 is 43/24 The difference of 9/8 and 2/3 is 11/24 The two Fractions are not equal The product of 3/2 and 2/3 is 1/1

Thanks, PLEASE UPVOTE, iTS DONE IN java

Add a comment
Know the answer?
Add Answer to:
java only no c++ Write a Fraction class whose objects will represent fractions. You should provide...
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
  • C++ For this assignment you will be building on the Original Fraction class you began last...

    C++ For this assignment you will be building on the Original Fraction class you began last week. You'll be making four major changes to the class. [15 points] Delete your set() function. Add two constructors, a default 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. Since Fractions cannot have...

  • This is a C++ probelm, I am really struggling with that...... Can anyone help me?? Write...

    This is a C++ probelm, I am really struggling with that...... Can anyone help me?? Write a fraction class whose objects will represent fractions. For this assignment you aren't required to reduce your fractions. You should provide the following member functions: A set() operation that takes two integer arguments, a numerator and a denominator, and sets the calling object accordingly. Arithmetic operations that add, subtract, multiply, and divide fractions. These should be implemented as value returning functions that return a...

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

  • In this assignment you are required to complete a class for fractions. It stores the numerator...

    In this assignment you are required to complete a class for fractions. It stores the numerator and the denominator, with the lowest terms. It is able to communicate with iostreams by the operator << and >>. For more details, read the header file. //fraction.h #ifndef FRACTION_H #define FRACTION_H #include <iostream> class fraction { private: int _numerator, _denominator; int gcd(const int &, const int &) const; // If you don't need this method, just ignore it. void simp(); // To get...

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

  • You have been developing a Fraction class for Teacher’s Pet Software that contains several fields and...

    You have been developing a Fraction class for Teacher’s Pet Software that contains several fields and functions. a. Add four arithmetic operators, +, -, *, and /. Remember that to add or subtract two Fractions, you first must convert them to Fractions with a common denominator. You multiply two Fractions by multiplying the numerators and multiplying the denominators. You divide two Fractions by inverting the second Fraction, then multiplying. After any arithmetic operation, be sure the Fraction is in proper...

  • C++ Question Your class should support the following operations on Fraction objects: • Construction of a...

    C++ Question Your class should support the following operations on Fraction objects: • Construction of a Fraction from two, one, or zero integer arguments. If two arguments, they are assumed to be the numerator and denominator, just one is assumed to be a whole number, and zero arguments creates a zero Fraction. Use default parameters so that you only need a single function to implement all three of these constructors. You should check to make sure that the denominator is...

  • 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++ CODE /* This is program project 2 on page 695. * Before you begin the...

    C++ CODE /* This is program project 2 on page 695. * Before you begin the project, please read the project description * on page 695 first. * * Author: Your Name * Version: Dates */ #include <iostream> #include <cmath> #include <cassert> using namespace std; class Fraction { public: // constructor Fraction(int a, int b); // generate a fraction which is a/b Fraction(int a); // generate a fraction which is a/1 Fraction(); // generate a fraction which is 0/1. i.e...

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