Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Write a description of class Fraction here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- public class Fraction
- {
- private int num;
- private int denom;
- public Fraction()
- {
- num = 0;
- denom = 1;
- }
- public Fraction(int n, int d)
- {
- num = n;
- denom = d;
- }
- public Fraction(Fraction other)
- {
- this.num = other.num;
- this.denom = other.denom;
- }
- public Fraction(int n)
- {
- num = n;
- denom = 1;
- }
- public int getNum()
- {
- return num;
- }
- public int getDenom()
- {
- return denom;
- }
- public void setNum(int n)
- {
- num = n;
- this.reduceFractions();
- }
- public void setDenom(int d)
- {
- denom = d;
- this.reduceFractions();
- }
- public void reduceFractions()
- {
- if( denom != 0)
- {
- int tmpN = Math.abs(num);
- int tmpD = Math.abs(denom);
- int tmp = tmpD;
- while(tmpD != 0)
- {
- tmp = tmpD;
- tmpD = tmpN % tmpD;
- tmpN = tmp;
- }
- num /=tmpN;
- denom /=tmpN;
- }
- }
- public Fraction addFractions(Fraction other)
- {
- Fraction f = new Fraction((this.num * other.denom + this.denom * other.num),(this.denom * other.denom));
- f.reduceFractions();
- return f;
- }
- public Fraction subtractFractions(Fraction other)
- {
- return this.addFractions(new Fraction(-other.num, other.denom));
- }
- public Fraction multiplyFractions(Fraction other)
- {
- Fraction bruh = new Fraction((this.num * other.num),(this.denom * other.denom));
- bruh.reduceFractions();
- return bruh;
- }
- public Fraction divideFractions(Fraction other)
- {
- return this.multiplyFractions(new Fraction(other.denom,other.num));
- }
- public String toString()
- {
- if(denom == 0)
- {
- return "undefined";
- }
- else if(denom == 1)
- {
- return num + "";
- }
- else if(num == 0)
- {
- return "0";
- }
- else if(denom == -1)
- {
- return -num + "";
- }
- else if(denom < 0)
- {
- return "(" + -num + "/" + -denom + ")";
- }
- else
- {
- return "(" + num + "/" + denom + ")";
- }
- }
- }
- /**
- * Write a description of class FractionClient here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- public class FractionClient
- {
- public static void main(String[]args)
- {
- Fraction attempt1 = new Fraction(1,2);
- Fraction attempt2 = new Fraction(3,4);
- System.out.println("The sum of 1/2 and 3/4 is " + attempt1.addFractions(attempt2));
- System.out.println("The difference of 1/2 and 3/4 is " + attempt1.subtractFractions(attempt2));
- System.out.println("The product of 1/2 and 3/4 is " + attempt1.multiplyFractions(attempt2));
- System.out.println("The quotient of 1/2 and 3/4 is " + attempt1.divideFractions(attempt2));
- Fraction fraction1 = new Fraction();
- System.out.println("Fraction1(Default) = " + fraction1);
- Fraction fraction2 = new Fraction(3,4);
- System.out.println("Fraction2 = " + fraction2);
- Fraction fraction3 = new Fraction(-1,2);
- System.out.println("Fraction3 = " + fraction3);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment