Validated For :
- Non numeric inputs
- x/0 type fractions
- 0/0 and 0/y type fractions
- x/1 and x/x type fractions
- -x/-y and x/-y type fractions
IDE used : Eclipse (Kepler)
Download source from GitHub (Eclipse project)
Join code review
http://codereview.stackexchange.com/questions/43084/java-fraction-calculator
Screenshots
Image 1 |
Image 2 |
Image 3 |
Image 4 |
Source
Folder Structure
FractionExercise
|- src
|-com
|- gigal
|- fractionexercise
|- app
| |- FractionsApp.java
|
|- helper
| |- Messages.java
|
|- model
|- Addition.java
|- Division.java
|- Fraction.java
|- Multiplication.java
|- Subtraction.java
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); } }
} |
---|
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); } }
|
---|
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); } } |
---|
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); } } |
---|
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); } } } |
---|
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("\n"); } // 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(); } } |
---|
FractionsApp.java
package com.gigal.fractionexercise.app; /* * Fraction Exercise * Author - Gigal */ 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(); } } |
---|
Calculator is very useful for Trigonometry, Calculus, and advanced mathematics subjects. There are also other calculator that are best for specific purpose such as fraction calculator for solving fraction problems. Scientific calculator is also best for Chemistry, Physics, and engineering subjects.
ReplyDelete