AVA Write a program that works with fractions I have part of

AVA :Write a program that works with fractions.
I have part of the template below.

Write a program that works with fractions. You are first to implement three methods, each to perform a different calculation on a pair of fractions: add(addFraction implementation is already given as a sample), subtract, multiply, and divide. For each of these methods, you are supplied two fractions as arguments, each a two-element array (the numerator is at index o, the denominator is at index 1), and you are to retun a resulting, simplified fraction as a new two-element array (again, with the numerator at index o, and denominator at index 1). You have been provided an add method as an example. You must compute the resulting fraction using fraction-based math (working with numerators and denominators) - do not convert the fractions to double values dike 1.5), do the math, and convert back to a fraction. You have been provided a method to simplify a fraction using the gcd method a previous last lab. As a reminder for fraction arithmetic... Once the operation methods are complete and pass the JUnit tests, now focus your attention on the main method. You first need to input the two fractions from the keyboard (numerator then denominator for each you can assume integers) as well as one of the four valid operations (+, -, *, /). Then validate the inputs: make sure a valid operation was input, make sure neither of the denominators are zero, and make sure that the numerator of the second fraction isn\'t zero if the operation is division (error messages have been provided for each of these situations). Finallv, compute the result of the operation and output the answer. Note that if the denominator of the answer is 1, you should just output the numerator (this includes if the answer is o). Here are two example runs of the program: Enter the numerator for the first fraction: 1 Enter the denominator for the first fraction: 2 Enter the numerator for the secondfraction: -4 Enter the denominator for the second fraction: 8 Enter the operation (+, -, *, /): Enter the numerator for the first fraction: 7 Enter the denominator for the first fraction: 8 Enter the numerator for the second fraction: 1

Solution

ackage com.gigal.fractionexercise.app;

import java.util.Scanner;

import com.gigal.fractionexercise.helper.Messages;

import com.gigal.fractionexercise.model.Division;

import com.gigal.fractionexercise.model.Fraction;

import com.gigal.fractionexercise.model.Multiplication;

import com.gigal.fractionexercise.model.Subtraction;

import com.gigal.fractionexercise.model.Addition;

public class FractionsApp {

private static Scanner keyboard = new Scanner(System.in);

public static void main(String args[]) {

    Fraction fraction1 = new Fraction(); // first fraction

    Fraction fraction2 = new Fraction(); // second fraction

    // Display application header

    Messages.displayHeader();

    // get user inputs for fraction one and validate them

    do {

        System.out.println(\"Enter values for fration one\");

        Messages.insertNumerator();

        try {

            fraction1.setNumerator(keyboard.nextInt()); // get user input

        } catch (Exception e) {

            Messages.inputError(e); // display error

            return;

        }

        Messages.inputDenominator();

        try {

            fraction1.setDenominator(keyboard.nextInt()); // get user input

        } catch (Exception e) {

            Messages.inputError(e);

            return;

        }

        if (fraction1.getDenominator() == 0) { // check for x/0 error

            Messages.DenominatorCannotBeZero();

        }

    } while (fraction1.getDenominator() == 0);

    // Display fraction one

    System.out.print(\"Fraction one : \");

    fraction1.display();

    Messages.newLine();

    // get user inputs for fraction two and validate them

    do {

        System.out.println(\"Enter values for fration two\");

        Messages.insertNumerator();

        try {

            fraction2.setNumerator(keyboard.nextInt()); // get user input

        } catch (Exception e) {

            Messages.inputError(e);

            return;

        }

        Messages.inputDenominator();

        try {

            fraction2.setDenominator(keyboard.nextInt()); // get user input

        } catch (Exception e) {

            Messages.inputError(e);

            return;

        }

        if (fraction2.getDenominator() == 0) { // check for x/0 error

            Messages.DenominatorCannotBeZero();

        }

    } while (fraction2.getDenominator() == 0);

    // Display fraction two

    System.out.print(\"Fraction two : \");

    fraction2.display();

    Messages.newLine();

    // Addition

    Addition addition = new Addition(fraction1, fraction2);

    addition.display();

    // Subtraction

    Subtraction subtraction = new Subtraction(fraction1, fraction2);

    subtraction.display();

    // Multiplication

    Multiplication multiplication = new Multiplication(fraction1, fraction2);

    multiplication.display();

    // Division

    Division division = new Division(fraction1, fraction2);

    division.display();

    // Display application footer

    Messages.displayFooter();

}

}

Messages.java

package com.gigal.fractionexercise.helper;

import com.gigal.fractionexercise.model.Fraction;

/*
* This class is used to give meaningful messages to user
* So whenever we want to change a message we don\'t want to check whole app but this class
*/

public class Messages {

// This message is use to display the program header
public static void displayHeader() {
   System.out.println(\"_________________________________________________________\");
   System.out.println(\"         Fraction App - a Gigal Blog Production\");
   System.out.println(\"_________________________________________________________\");
    newLine();
}

// This message is use to display the program footer
public static void displayFooter() {
    newLine();
   System.out.println(\" ----------- Thank you for using Fraction App -----------\");
   System.out.println(\"_________________________________________________________\");
    newLine();
}

// This message is use to tell user to input value for Numerator in a fraction
public static void insertNumerator() {
   System.out.print(\"Numerator : \");
}

// This message is use to tell user to input a value for denominator
public static void inputDenominator() {
   System.out.print(\"Denominator   : \");
}

/ This method is used to get line of space
public static void newLine() {
   System.out.println(\"\ \");
}

// This message is used when user input something miss match
public static void inputError(Exception e) {
    newLine();
   System.out.println(\"Input Error: \" + e.toString());
   System.out.println(\"Closing application ...\");
   System.out.println(\"Fraction app is closed.\");
    displayFooter();
}

// This message is used when user input 0 for Denominator in a fraction
public static void DenominatorCannotBeZero() {
   System.out.println(\"Input Error: Denominator Cannot be zero\");
    newLine();
}

// This message is used to display answers
public static void displayAnswer(String operation, String operator,
       Fraction fraction1, Fraction fraction2, Fraction answer) {
   System.out.print(operation + \" : \");
    fraction1.display();
   System.out.print(\" \" + operator + \" \");
    fraction2.display();
   System.out.print(\" = \");
    answer.display();
    newLine();
}

}

Addition.java

package com.gigal.fractionexercise.model;

/*
* This class models addition
*/

import com.gigal.fractionexercise.helper.Messages;

public class Addition {

private Fraction fraction1;
private Fraction fraction2;
private Fraction answer;

// Constructor
public Addition(Fraction fraction1, Fraction fraction2) {
   this.fraction1 = fraction1;
   this.fraction2 = fraction2;
   this.answer = new Fraction();
   Calculate();
}

// perform the calculation
public void Calculate() {
    answer.setNumerator((fraction1.getNumerator() * fraction2.getDenominator())
           + (fraction2.getNumerator() * fraction1.getDenominator()));
    answer.setDenominator(fraction1.getDenominator() * fraction2.getDenominator());
}

// display the answer
public void display() {
   Messages.displayAnswer(\"Addition\", \"+\", fraction1, fraction2, answer);
}

}

Division.java

package com.gigal.fractionexercise.model;

/*
* This class models division
*/

import com.gigal.fractionexercise.helper.Messages;

public class Division {

private Fraction fraction1;
private Fraction fraction2;
private Fraction answer;

// Constructor
public Division(Fraction fraction1, Fraction fraction2) {
   this.fraction1 = fraction1;
   this.fraction2 = fraction2;
   this.answer = new Fraction();
   Calculate();
}

// perform the calculation
public void Calculate() {
    answer.setNumerator(fraction1.getNumerator() * fraction2.getDenominator());
    answer.setDenominator(fraction1.getDenominator() * fraction2.getNumerator());
}

public void display() {
   // Check for the divide by zero error
   if (fraction2.getNumerator() == 0) {
       System.out.println(\"Division : Cannot divide by zero!\");
       return;
   } else {
       // display the answer
       Messages.displayAnswer(\"Division\", \"/\", fraction1, fraction2,
                answer);
   }

}

}

Fraction.java

package com.gigal.fractionexercise.model;

/*
* This class models the fraction
*/

public class Fraction {

private int Numerator; // x
private int Denominator; // y

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

// This method is used to display fractions
// Some kind of processing also
public void display() {

   // 0/y and x/1 types
   if (Numerator == 0 || Denominator == 1) {
       System.out.print(Numerator);
   }

   // -x/-y and x/-y types
   else {
if ((Numerator < 0 && Denominator < 0) || (Numerator > 0 && Denominator < 0)) {
           Numerator *= -1;
           Denominator *= -1;
       }

       // x/x type
       if (Numerator == Denominator) {
            System.out.print(Numerator);
           return;
       }

       System.out.print(this.Numerator + \"/\" + this.Denominator);
   }

}

}

Multiplication.java

package com.gigal.fractionexercise.model;

/*
* This class models multiplication
*/

import com.gigal.fractionexercise.helper.Messages;

public class Multiplication {

private Fraction fraction1;
private Fraction fraction2;
private Fraction answer;

// Constructor
public Multiplication(Fraction fraction1, Fraction fraction2) {
   this.fraction1 = fraction1;
   this.fraction2 = fraction2;
   this.answer = new Fraction();
   Calculate();
}

// perform the calculation
public void Calculate() {
    answer.setNumerator(fraction1.getNumerator() * fraction2.getNumerator());
    answer.setDenominator(fraction1.getDenominator() * fraction2.getDenominator());
}

// display the answer
public void display() {
   Messages.displayAnswer(\"Multiplication\", \"*\", fraction1, fraction2, answer);
}

}

Subtraction.java

package com.gigal.fractionexercise.model;

/*
* This class models subtraction
*/

import com.gigal.fractionexercise.helper.Messages;

public class Subtraction {

private Fraction fraction1;
private Fraction fraction2;
private Fraction answer;

// Constructor
public Subtraction(Fraction fraction1, Fraction fraction2) {
   this.fraction1 = fraction1;
   this.fraction2 = fraction2;
   this.answer = new Fraction();
   Calculate();
}

// perform the calculation
public void Calculate() {
    answer.setNumerator((fraction1.getNumerator() * fraction2.getDenominator()) - (fraction2.getNumerator() * fraction1.getDenominator()));
    answer.setDenominator(fraction1.getDenominator() * fraction2.getDenominator());
}

// display the answer
public void display() {
   Messages.displayAnswer(\"Subtraction\", \"-\", fraction1, fraction2, answer);
}

}

AVA :Write a program that works with fractions. I have part of the template below. Write a program that works with fractions. You are first to implement three m
AVA :Write a program that works with fractions. I have part of the template below. Write a program that works with fractions. You are first to implement three m
AVA :Write a program that works with fractions. I have part of the template below. Write a program that works with fractions. You are first to implement three m
AVA :Write a program that works with fractions. I have part of the template below. Write a program that works with fractions. You are first to implement three m
AVA :Write a program that works with fractions. I have part of the template below. Write a program that works with fractions. You are first to implement three m
AVA :Write a program that works with fractions. I have part of the template below. Write a program that works with fractions. You are first to implement three m
AVA :Write a program that works with fractions. I have part of the template below. Write a program that works with fractions. You are first to implement three m
AVA :Write a program that works with fractions. I have part of the template below. Write a program that works with fractions. You are first to implement three m

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site