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;
- }
- public void setDenom(int d)
- {
- denom = d;
- }
- public Fraction addFractions(Fraction other)
- {
- return new Fraction((this.num * other.denom + this.denom * other.num),(this.denom * other.denom));
- /*
- * Write reduce method
- * do Fraction f = new Fraction(bruhburh)
- * then do f.reduce()
- */
- }
- public Fraction subtractFractions(Fraction other)
- {
- return this.addFractions(new Fraction(-other.num, other.denom));
- }
- public Fraction multiplyFractions(Fraction other)
- {
- return new Fraction((this.num * other.num),(this.denom * other.denom));
- }
- 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();
- Fraction attempt2 = new Fraction(3,4);
- Fraction attempt3 = new Fraction(5);
- Fraction attempt4 = new Fraction(attempt2);
- Fraction attempt5 = new Fraction(2,0);
- Fraction attempt6 = new Fraction(7,-1);
- Fraction attempt7 = new Fraction(6,-4);
- Fraction attempt8 = new Fraction(1,2);
- System.out.println("My first attempt has the following points: " + attempt1);
- System.out.println("My second attempt has the following points: " + attempt2);
- System.out.println("My third attempt has the following points: " + attempt3);
- System.out.println("My fourth attempt has the following points: " + attempt4);
- System.out.println("My fifth attempt has the following points: " + attempt5);
- System.out.println("My sixth attempt has the following points: " + attempt6);
- System.out.println("My seventh attempt has the following points: " + attempt7);
- System.out.println("My eighth attempt has the following points: " + attempt8);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment