Question

a/b x c/dy Let's design a class and explore more on object reference: A fraction with...

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

Homework Answers

Answer #1
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
    }
}

==============================================================

Know the answer?
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
Similar Questions
With this class: public class myFunction { public int x = 7; public int y =...
With this class: public class myFunction { public int x = 7; public int y = 3; } 1. What are the class variables? 2. What are the instance variables? 3. What is the output from the following code: myFunction a = new myFunction(); myFunction b = new myFunction(); a.y = 5; b.y = 6; a.x = 1; b.x = 2; System.out.println("a.y = " + a.y); System.out.println("b.y = " + b.y); System.out.println("a.x = " + a.x); System.out.println("b.x = " +...
C# programming Create a class called QuadraticEq that has a custom constructor that takes in a,...
C# programming Create a class called QuadraticEq that has a custom constructor that takes in a, b and c as the quadratic equation coefficients. Implement also the following: A readonly property double Discriminant that represents discriminant to the equation A method double[] GetRealRoots() that returns an array of the real roots of the equation; size can be 2, 1 or 0 Please pay attention to encapsulation concerns. Use your created class in the following way inside a Main method to...
1. when you define a class, if you do not explicitely extend another class, your class...
1. when you define a class, if you do not explicitely extend another class, your class is an extentsion of the _________ class a. object b. super c. public d. abstract 2.The object class ______ method coverts an object into a string that contains information about the object a. equal() b. setType() c. toString() d. speak() 3.The onject class equals() method returns a(n) ________ value indicating thether the object are equal a. int b. equals c. null d. boolean
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
c++ program. can you please do commenting. Question 1 [25]. (The Account Class) Design a class...
c++ program. can you please do commenting. Question 1 [25]. (The Account Class) Design a class named Account that contains (keep the data fields private): a) An int data field named id for the account. b) A double data field named balance for the account. c) A double data field named annualInterestRate that stores the current interest rate. d) A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. e) The accessor and mutator...
C program fractions.c that does the following: 1. The program starts by making the user enter...
C program fractions.c that does the following: 1. The program starts by making the user enter a non-negative integer number a, called the first numerator. If the user enters a negative number, the program repeats the entry. 2. The program then makes the user enter a positive integer number b, called the first denominator. If the user enters a non-positive number, the program repeats the entry. 3. The program then makes the user enter a non-negative integer number c, called...
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int...
import java.util.Scanner; public class InRange { private int min; private int max; public InRange(int initialMin, int initialMax) { min = initialMin; max = initialMax; } // You need to write two instance methods: // 1.) A method named inRange, which takes an int. // This returns true if the int is within the given range // (inclusive), else false. // // 2.) A method named outOfRange which takes an int. // This returns false if the int is within the...
You have four identical conducting spheres: A, B, C, and D. In each scenario below, sphere...
You have four identical conducting spheres: A, B, C, and D. In each scenario below, sphere A starts with a charge of Q, while B, C, and D start out with no net charge, and then the spheres are touched to each other and separated in the order described. Any spheres not in contact are held very far away. For each scenario, give the final charge of sphere B as a whole number fraction of Q. For example, if you...
Java programming. 1) What, if anything, is wrong with the following Java code? void test(String x)...
Java programming. 1) What, if anything, is wrong with the following Java code? void test(String x) { switch (x) { case 1: break; case 2: break; case 0: break; default: break; case 4: break; } } Select one: a)Each case section must end with a break statement. b)The case label 0 must precede case label 1. c)There is nothing wrong with the Java code. d)The default label must be the last label in the switch statement. e)The variable x does...
public class Point { int x; int y; public Point(int initialX, int initialY){ x = initialX;...
public class Point { int x; int y; public Point(int initialX, int initialY){ x = initialX; y= initialY; } public boolean equals (Object o){ if (o instanceof Point){ Point other = (Point)o; return (x == other.x && y == other.y); }else{ return false; } } } We haev defined "equals" method for our class using "instanceof". We define and use instances (or objects) of this class in the following scenarios. In each case, specify what is the output. (hint: there...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT