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
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...
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...
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...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String...
in java need uml diagram import java.util.ArrayList; import java.util.*; public class TodoList { String date=""; String work=""; boolean completed=false; boolean important=false; public TodoList(String a,String b,boolean c,boolean d){ this.date=a; this.work=b; this.completed=c; this.important=d; } public boolean isCompleted(){ return this.completed; } public boolean isImportant(){ return this.important; } public String getDate(){ return this.date; } public String getTask(){ return this.work; } } class Main{ public static void main(String[] args) { ArrayList<TodoList> t1=new ArrayList<TodoList>(); TodoList t2=null; Scanner s=new Scanner(System.in); int a; String b="",c=""; boolean d,e; char...
1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class...
1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class called Student which implements Duty. Class Student adds 1 data field, id, and 2 methods, getId and setId, along with a 1-argument constructor. The duty of a Student is to study 40 hours a week. b. Write a class called Professor which implements Duty. Class Professor adds 1 data field, name, and 2 methods, getName and setName, along with a 1-argument constructor. The duty...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational...
Cpp Task: Create a class called Mixed. Objects of type Mixed will store and manage rational numbers in a mixed number format (integer part and a fraction part). Details and Requirements Your class must allow for storage of rational numbers in a mixed number format. Remember that a mixed number consists of an integer part and a fraction part (like 3 1/2 – “three and one-half”). The Mixed class must allow for both positive and negative mixed number values. A...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with...
Java Program Implement a class called AnimalTrainer. Include the following data types in your class with the default values denoted by dataType name : defaultValue - String animal : empty string - int lapsRan : 0 - boolean resting : false - boolean eating : false - double energy : 100.00 For the animal property implement both getter/setter methods. For all other properties implement ONLY a getter method Now implement the following constructors: 1. Constructor 1 – accepts a String...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT