Answer to Question #320516 in Java | JSP | JSF for liPUJN

Question #320516

fraction as an object with a numerator and denominator as key attributes, check if the fraction is positive or negative. Use instance methods such as getters, setters and constructors, your class should have functionality listed below:

A) isImproper() : returns true if the numerator is bigger than the denominator e.g 5/4

B) toMixed() : converts an improper fraction to mixed e.g. 9/4 = 4 1⁄2 C) AddFraction(): takes in another fraction and add them up as follows, a/b + c/d = (a x b) + (b x c) /bxd

D) Provide a toString() : to represent the fraction as X/Y or X/Y/Z for improper fractions 1.1. Provide a simple Class and object diagram for the above scenario

1.2. Implement class modelled above and test its functionality as specified below

Sample Run1

Enter the numerator and denominator separated by a space: 9 4

Enter the numerator and denominator separated by a space: 3 5

Output1:

9/4 is an Improper Fraction

3/5 is a Proper Fraction

9/4 PLUS 3/5 = 57/20

The addition result represented with toString : 2/17/20 


1
Expert's answer
2022-03-29T16:48:15-0400
import java.util.Scanner;

class Fraction{
    int numerator, denominator;
    int x, y, z;

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


    public void isImproper(){
        if(numerator > denominator){
            System.out.printf("%d/%d is an Improper Fraction", numerator, denominator);
            System.out.println("");
        } else {
            System.out.printf("%d/%d  is a Proper Fraction", numerator, denominator);
            System.out.println("");
        }
    }

    public void toMixed(int numeratorF, int denominatorF){
        if(numeratorF > denominatorF){
            x = numeratorF % denominatorF;
            y = denominatorF;
            z = numeratorF / denominatorF;
        }

    }


    public void AddFraction(Fraction a){
        System.out.printf("%d/%d + %d/%d = %d/%d", numerator, denominator, a.numerator, a.denominator, (numerator*a.denominator+denominator*a.numerator), denominator*a.denominator);
        System.out.println("");
        toMixed(numerator*a.denominator+denominator*a.numerator, denominator*a.denominator);
    }

    @Override
    public String toString() {
        String answ = z + "/" + x  + "/" + y;
        return answ;
    }
}



public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter the numerator and denominator separated by a space: ");
        String first = in.nextLine();
        String[] firstS = first.split(" ");

        System.out.print("Enter the numerator and denominator separated by a space: ");
        String second = in.nextLine();
        String[] secondS = second.split(" ");

        Fraction fraction1 = new Fraction(Integer.parseInt(firstS[0]), Integer.parseInt(firstS[1]));
        Fraction fraction2 = new Fraction(Integer.parseInt(secondS[0]), Integer.parseInt(secondS[1]));

        fraction1.isImproper();
        fraction2.isImproper();


        fraction1.AddFraction(fraction2);

        System.out.println(fraction1.toString());
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS