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

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

  • When we test your Fraction.java we will use the given FractionTester.java file. Your Fraction.java file -MUST-...

    When we test your Fraction.java we will use the given FractionTester.java file. Your Fraction.java file -MUST- work perfectly with the supplied FractionTester.java file. You may NOT modify the FractionTester to make your Fraction class work right. Starter Fraction File: Fraction.java ADD METHODS TO THIS FILE. HAND IN THIS FILE ONLY. Given/Completed Tester File: FractionTester.java DO NOT MODIFY. DO NOT HAND IN. You are to add the following methods to the given Fraction.java file public Fraction add( Fraction other) returns a...

  • If you have already answered this question, please do not repost your old solutions, you will be thumbs downed. I'm...

    If you have already answered this question, please do not repost your old solutions, you will be thumbs downed. I'm looking for NEW solutions only! Frac.h: // a Fraction object holds one Fraction number, one fraction #ifndef FRAC_H #define FRAC_H #include <iostream> using namespace std; //Creaing a Fraction class class Fraction { public: Fraction(int = 0, int = 1); // Function Declarations which performs operations on Fraction class Fraction add(const Fraction &); Fraction subtract(const Fraction& a); Fraction multiply(const Fraction& a);...

  • Question 34 (2 points) For the add method in Fraction class, fill the following blank. public...

    Question 34 (2 points) 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 } new Fraction(num, denom) O num/denom (double)num/(double)denom (int)num/denom Previous Page Next Page

  • Java Assignment Calculator with methods. My code appears below what I need to correct. What I...

    Java Assignment Calculator with methods. My code appears below what I need to correct. What I need to correct is if the user attempts to divide a number by 0, the divide() method is supposed to return Double.NaN, but your divide() method doesn't do this. Instead it does this:     public static double divide(double operand1, double operand2) {         return operand1 / operand2;     } The random method is supposed to return a double within a lower limit and...

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