Question

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= new Fraction();
//test default constructor
System.out.println(f);
//test mutators
f.setNumerator(4);
f.setDenominator(5);
//test accessors
System.out.println("numerator is " + f.getNumerator() + " and denominator is " + f.getDenominator());
System.out.println(f);
//test constructor
Fraction g= new Fraction(8,19);
System.out.println(g);
Scanner keyboard = new Scanner (System.in);
//get the data for the next fraction and construct it
int n1=keyboard.nextInt();
int d1=keyboard.nextInt();
Fraction one=new Fraction (n1,d1);
//get the data for the next fraction and construct it
n1=keyboard.nextInt();
d1=keyboard.nextInt();
Fraction two=new Fraction (n1,d1);
//test methods for add, subtract, multiply, divide
System.out.println(one + " + " + two + " = " + one.add(two));
System.out.println(one + " - " + two + " = " + one.subtract(two));
System.out.println(one + " * " + two + " = " + one.multiply(two));
System.out.println(one + " divided by " + two + " = " + one.divide(two));
  
}
  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class H4 {

    public static class Fraction {
        private int numerator;
        private int denominator;

        public Fraction() {
            numerator = 0;
            denominator = 1;
        }

        public Fraction(int numerator, int denominator) {
            this.numerator = numerator;
            this.denominator = denominator;
        }

        public int getNumerator() {
            return numerator;
        }

        public void setNumerator(int numerator) {
            this.numerator = numerator;
        }

        public int getDenominator() {
            return denominator;
        }

        public void setDenominator(int denominator) {
            this.denominator = denominator;
        }

        public Fraction add(Fraction rhs) {
            return new Fraction(numerator * rhs.denominator + rhs.numerator * denominator, denominator * rhs.denominator);
        }

        public Fraction subtract(Fraction rhs) {
            return new Fraction(numerator * rhs.denominator - rhs.numerator * denominator, denominator * rhs.denominator);
        }

        public Fraction multiply(Fraction rhs) {
            return new Fraction(numerator * rhs.numerator, denominator * rhs.denominator);
        }

        public Fraction divide(Fraction rhs) {
            return new Fraction(numerator * rhs.denominator, rhs.numerator * denominator);
        }

        @Override
        public String toString() {
            return numerator + "/" + denominator;
        }
    }

    public static void main(String[] args) {
        Fraction f = new Fraction();
//test default constructor
        System.out.println(f);
//test mutators
        f.setNumerator(4);
        f.setDenominator(5);
//test accessors
        System.out.println("numerator is " + f.getNumerator() + " and denominator is " + f.getDenominator());
        System.out.println(f);
//test constructor
        Fraction g = new Fraction(8, 19);
        System.out.println(g);
        Scanner keyboard = new Scanner(System.in);
//get the data for the next fraction and construct it
        int n1 = keyboard.nextInt();
        int d1 = keyboard.nextInt();
        Fraction one = new Fraction(n1, d1);
//get the data for the next fraction and construct it
        n1 = keyboard.nextInt();
        d1 = keyboard.nextInt();
        Fraction two = new Fraction(n1, d1);
//test methods for add, subtract, multiply, divide
        System.out.println(one + " + " + two + " = " + one.add(two));
        System.out.println(one + " - " + two + " = " + one.subtract(two));
        System.out.println(one + " * " + two + " = " + one.multiply(two));
        System.out.println(one + " divided by " + two + " = " + one.divide(two));
    }
}

0/1 numerator is 4 and denominator is 5 4/5 8/19 2/5 + 7/9 = 53/45 2/5 - 7/9 = -17/45 2/5 * 7/9 = 14/45 2/5 divided by 7/9 =

Add a comment
Know the answer?
Add Answer to:
I have provided a main method. Please add your fraction class. You need constructors, add, subtract,...
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
  • Java Write a Fraction class that implements these methods:  add ─ This method receives a...

    Java Write a Fraction class that implements these methods:  add ─ This method receives a Fraction parameter and adds the parameter fraction to the calling object fraction.  multiply ─ This method receives a Fraction parameter and multiplies the parameter fraction by the calling object fraction.  divide ─ This method receives a Fraction parameter and divides the parameter fraction by the calling object fraction.  print ─ This method prints the fraction using fraction notation (1/4, 21/14, etc.)...

  • Create a fraction class. This will have two attributes, a numerator and a denominator, both int....

    Create a fraction class. This will have two attributes, a numerator and a denominator, both int. This class will have constructors accessors and mutators (for the attributes) a toString method which will allow us to print the fraction in the form 3/4 an Add method so we can add two fractions a subtract method (subtracts one fraction from the other) a multiply method (multiply two fractions) a divide method (divides one fraction by the other) DO NOT (for now) worry...

  • Rational will be our parent class that I included to this post, Implement a sub-class MixedRational....

    Rational will be our parent class that I included to this post, Implement a sub-class MixedRational. This class should Implement not limited to: 1) a Constructor with a mathematically proper whole, numerator and denominator values as parameters. 2) You will override the: toString, add, subtract, multiply, and divide methods. You may need to implement some additional methods, enabling utilization of methods from rational. I have included a MixedRational class with the method headers that I used to meet these expectations....

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

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

  • This in in C# There are two classes, class Fraction and class FractionDemo The user needs...

    This in in C# There are two classes, class Fraction and class FractionDemo The user needs to be able to input a value for the numerator and the denominator Create a Fraction class with private fields that hold a positive int numerator and a positive int denominator. In addition, create Properties for each field with the set mutator such that the numerator is greater than or equal to 0 and the denominator is greater than 0 (illegal values should be...

  • Define four static methods, named add, subtract, multiply, and divide. Each of these methods should be...

    Define four static methods, named add, subtract, multiply, and divide. Each of these methods should be defined to take two Fraction arguments and return a new Fraction. For now, though, each of these methods should return null. Define a constructor that takes two int parameters, corresponding to the numerator and denominator of a fraction. For now, the constructor should do nothing with these parameters; the constructor body should be empty. You may think this odd, but the reasons will be...

  • For the add method in Fraction class, fill the following blank. public Fraction add(Fraction f) {...

    For the add method in Fraction class, fill the following blank. public Fraction add(Fraction f) { int num = numerator * f.getDenominator() + f.getNumerator() * denominator; int denom = denominator * f.getDenominator(); return ww new Fraction(num, denom) num/denom (double)num/(double)denom (int)num/denom

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

  • The main method of your calculator program has started to get a little messy. In this...

    The main method of your calculator program has started to get a little messy. In this assignment, you will clean it up some by moving some of your code into new methods. Methods allow you to organize your code, avoid repetition, and make aspects of your code easier to modify. While the calculator program is very simple, this assignment attempts to show you how larger, real world programs are structured. As a new programmer in a job, you will likely...

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