Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Dylan Morrison Assignment #4
- import java.util.Scanner;
- /*
- * This class of the program is meant to ask for a user input
- * of two fractions which, then have math operations performed
- * on them such as adding, subtracting, multiplying, and dividing.
- * The answers will also be in the most simplified form.
- */
- public class Driver {
- public static void main(String args[]){
- //Uses Scanner to create the first fraction
- Scanner input = new Scanner(System.in);
- System.out.print("Please enter fraction #1:");
- String f1 = input.nextLine();
- //Uses Scanner to create the second fraction
- System.out.print("Please enter fraction #2:");
- String f2 = input.nextLine();
- String[] arrayf1 = f1.split("/");
- int f1num = Integer.parseInt(arrayf1[0]);
- int f1den = Integer.parseInt(arrayf1[1]);
- String [] arrayf2 = f2.split("/");
- int f2num = Integer.parseInt(arrayf2[0]);
- int f2den = Integer.parseInt(arrayf2[1]);
- Fraction num1 = new Fraction(f1num, f1den);
- Fraction num2 = new Fraction(f2num, f2den);
- Fraction result1 = num1.add(num2);
- Fraction result2 = result1.red();
- System.out.println("The addition of the two fractions =" +
- result2.getNum() + "/" + result2.getDen());
- Fraction result3 = num1.sub(num2);
- Fraction result4 = result3.red();
- System.out.println("The subtraction of fraction1 from fraction2 ="+
- result4.getNum() + "/" + result4.getDen());
- Fraction result5 = num1.mplier(num2);
- Fraction result6 = result5.red();
- System.out.println("The multiplication of the two fractions ="+
- result6.getNum() + "/" + result6.getDen());
- Fraction result7 = num1.div(num2);
- Fraction result8 = result7.red();
- System.out.println("The division of fraction1 by fraction 2 = "+
- result8.getNum() + "/" + result8.getDen());
- }
- }
- public class Fraction {
- public int numerator, denominator;
- public Fraction(int a, int b){
- numerator = a;
- denominator = b;
- }
- public int getNum(){
- return numerator;
- }
- public int getDen(){
- return denominator;
- }
- public Fraction add(Fraction num2){
- int a = numerator * num2.getDen() + (denominator * num2.getNum());
- int b = denominator * num2.getDen();
- Fraction added = new Fraction(a, b);
- return added;
- }
- public Fraction sub(Fraction num2){
- int a = denominator * num2.getNum() - (numerator * num2.getDen());
- int b = denominator * num2.getDen();
- Fraction minus = new Fraction(a, b);
- return minus;
- }
- public Fraction mplier(Fraction num2){
- int a = numerator * num2.getNum();
- int b = denominator * num2.getDen();
- Fraction end = new Fraction(a, b);
- return end;
- }
- public Fraction div(Fraction num2){
- int a = numerator * num2.getDen();
- int b = denominator * num2.getNum();
- Fraction result = new Fraction(a, b);
- return result;
- }
- //reduces the fractions
- public Fraction red(){
- int x = numerator;
- int y = denominator;
- while (y != 0){
- int var = y;
- y = x % y;
- x = var;
- }
- int newnum = (numerator/x);
- int newden = (denominator/x);
- if(newden < 0){
- newnum = -newnum;
- newden = -newden;
- }
- Fraction reduced = new Fraction(newnum, newden);
- return reduced;
- }
- public static void main(String args[]){
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement