Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Name: Matthew Nguyen
- //Date: 11/18/19
- //Purpose: Do stuff with fractions like add, sub, multiply or divide them
- public class Fraction
- {
- private int num; //Makes instance variable num
- private int denom;//Makes instance variable denom
- private Fraction answer; //Makes instance variable answer
- public Fraction(int n, int d)
- {
- num = n; //Sets n equal to num
- denom = d;//Sets d equal to denom
- }
- public String toString() //toString to print out the objects
- {
- String fraction;
- if(denom == 0)//Undefined checker
- {
- fraction = "undefined";
- }
- else if(num == 0)//Checks if numerator is 0
- {
- fraction = "0";
- }
- else if(denom == 1)//Checks if denominator is 1
- {
- fraction = ""+num;
- }
- else if(denom == -1 && num<0){
- fraction = ""+(-1*num);
- }
- else if(num < 0 && denom < 0)//check if both parts of fraction are negative
- {
- fraction = ""+(-1*num)+"/"+(-1*denom);
- }
- else
- {
- fraction = ""+num+"/"+denom;
- }
- return fraction;
- }
- public int getNum()
- {
- return num;//returns int num when called
- }
- public int getDen()
- {
- return denom;//returns int denom when called
- }
- public boolean equals(Object other)
- {
- Fraction temp = (Fraction) other; //Temp fraction to test if the things are equal
- int otherNum = temp.getNum(); //temp num and denominator to testif they are equal
- int otherDenom = temp.getDen();
- if((num*otherDenom) == (otherNum*denom))//cross multiply and if both sides are equal, then the fractions are equal
- {
- return true;
- }
- else//if they are not equal, a false is returned
- {
- return false;
- }
- }
- private Fraction reduce()
- {
- int answerNum;
- int answerDenom;
- int gcf = 1;
- for(int i = 1;i<=Math.min(Math.abs(num),Math.abs(denom));i++)
- {
- if((num%i==0) && (denom%i==0))
- {
- gcf = i;
- }
- }
- answerNum = getNum()/gcf;
- answerDenom= getDen()/gcf;
- Fraction reduced = new Fraction(answerNum, answerDenom);
- return reduced;
- }
- public static Fraction reduced(Fraction reduced)
- {
- return reduced.reduce();
- }
- public Fraction add(Fraction other)//method to add fractions
- {
- Fraction answer;
- int n = this.getNum()*other.getDen()+other.getNum()*this.getDen();//Function to produce numerator in instance of addition
- int d = this.getDen()*other.getDen(); //Function to produce denominator
- answer = new Fraction(n,d); //Creates a fraction with the new numerator and denominator when two fracs are added
- return reduced(answer); //Returns new fraction
- }
- public Fraction sub(Fraction other)//method to subtract fractions
- {
- Fraction answer;
- int n = this.getNum()*other.getDen()-other.getNum()*this.getDen(); //Function to produce numerator in instance of subtraction
- int d = this.getDen()*other.getDen(); //Function to produce denominator
- answer = new Fraction(n,d); //Creates a fraction with the new numerator and denominator when two fracs are subtracted
- return reduced(answer); //Returns new fraction
- }
- public Fraction mult(Fraction other)//method to multiply fractions
- {
- Fraction answer;
- int n = this.getNum()*other.getNum();
- int d = this.getDen()*other.getDen();
- answer = new Fraction (n,d);
- return reduced(answer); //Returns new fraction
- }
- public Fraction div(Fraction other)//method to divide fractions
- {
- int n = this.getNum()*other.getDen();
- int d = this.getDen()*other.getNum();
- Fraction answer;
- answer = new Fraction (n,d);
- Fraction undefined;
- undefined = new Fraction (0,0);
- if(d==0)
- {
- return undefined;
- }
- else
- {
- return reduced(answer); //Returns new fraction
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement