a/b x c/dy
Let's design a class and explore more on object reference:
A fraction with two private fields: a for representing numerator and b for representing denominator.
Define a constructor Fraction() accept two integers to represent the numerator and denominator.
Define a public instance method named printString() which returns a String in form of "a/b".
Define a public instance method named multiplyBy() which accepts one argument of class type Fraction, calculates the multiplication between self and the receiving Fraction, and returns the resultant Fraction.
Define a private class method named simplify() which accepts one argument of type Fraction, reduce the referenced number to its simplest form e.g. 3/6 shall be reduced to 1/2. This method returns no value. Hints: how can you reuse GCD implementation to reduce a fraction?
Note to reduce the fraction in creating the fraction too!
You may assume the input numerator is not the same as the denominator, while the denominator cannot be zero.
Sample Run 1
a? b? c? d?
1 2 3 4
1/2 x 3/4 = 3/8
Sample Run 2
a? b? c? d?
1 2 2 3
1/2 x 2/3 = 1/3
Sample Run 3
a? b? c? d?
1 2 2 4
1/2 x 1/2 = 1/4
The given codes are:
import java.util.Scanner;
// let's complete the class Fraction here
class Fraction {
// define your fields, constructor and more instance methods
here
// YOUR CODE HERE
}
class Main {
public static void main(String[] args) {
// Do not touch the following lines
//
Scanner keyin = new Scanner(System.in);
System.out.println("a? b? c? d?");
int a, b, c, d;
a = keyin.nextInt();
b = keyin.nextInt();
c = keyin.nextInt();
d = keyin.nextInt();
Fraction r, q, result;
r = new Fraction(a, b);
q = new Fraction(c, d);
System.out.print(r.printString() + " x " + q.printString() + " =
");
result = r.multiplyBy(q);
System.out.println( result.printString() );
// The End
}
}
Thanks for the question. Below is the code you will be needing. Let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please leave a +ve feedback : ) Let me know for any help with any other questions. Thank You! =========================================================================== import java.util.Scanner; // let's complete the class Fraction here class Fraction { // define your fields, constructor and more instance methods here // YOUR CODE HERE private int a; private int b; public Fraction(int a, int b) { this.a = a; this.b = b; simplify(this); } public String printString() { return a + "/" + b; } public Fraction multiplyBy(Fraction fraction) { return new Fraction(a * fraction.a , b * fraction.b); } private void simplify(Fraction f){ int divide = 2; while (divide <= f.a && divide <= f.b) { if (f.a % divide == 0 && f.b % divide == 0) { f.a /= divide; f.b /= divide; divide = 2; } else { divide++; } } } } ============================================================== class Main { public static void main(String[] args) { // Do not touch the following lines // Scanner keyin = new Scanner(System.in); System.out.println("a? b? c? d?"); int a, b, c, d; a = keyin.nextInt(); b = keyin.nextInt(); c = keyin.nextInt(); d = keyin.nextInt(); Fraction r, q, result; r = new Fraction(a, b); q = new Fraction(c, d); System.out.print(r.printString() + " x " + q.printString() + " = "); result = r.multiplyBy(q); System.out.println(result.printString()); // The End } }
==============================================================
Get Answers For Free
Most questions answered within 1 hours.