Created
October 14, 2016 23:38
-
-
Save seth10/7fdc2814f51da889faf8623b37e53145 to your computer and use it in GitHub Desktop.
Creates a basic fraction with a numerator and denominator field.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Fraction.java - Version 1.01 - 6/7/2013 | |
| _____ _ _ _____ _ | |
| / ___| | | | | |_ _| | | | |
| \ `--. ___| |_| |__ | | ___ _ __ ___ _ __ ___ | |__ __ _ _ _ _ __ ___ | |
| `--. \/ _ \ __| '_ \ | |/ _ \ '_ \ / _ \ '_ ` _ \| '_ \ / _` | | | | '_ ` _ \ | |
| /\__/ / __/ |_| | | | | | __/ | | | __/ | | | | | |_) | (_| | |_| | | | | | | | |
| \____/ \___|\__|_| |_| \_/\___|_| |_|\___|_| |_| |_|_.__/ \__,_|\__,_|_| |_| |_| | |
| Creates a basic fraction with a numerator and denominator field. | |
| VERSION CONTROL | |
| 1.00: Initial release | |
| */ | |
| public class Fraction { | |
| int num; //field: numerator | |
| int den; //field: denominator | |
| public Fraction(){ //null constructor | |
| num=1; | |
| den=1; | |
| } | |
| public Fraction(int numIn, int denIn){ | |
| num=numIn; | |
| den=denIn; | |
| } | |
| public int getNum(){ | |
| return num; | |
| } | |
| public int getDen(){ | |
| return den; | |
| } | |
| public boolean isZero(){ | |
| if(den==0||num==0) | |
| return true; | |
| else | |
| return false; | |
| } | |
| public void print(){ | |
| System.out.print(num+"/"+den); | |
| } | |
| public void println(){ | |
| System.out.println(num+"/"+den); | |
| } | |
| public void simplify(){ | |
| if(num!=0){ | |
| if(num%den==0){ | |
| num/=den; | |
| den=1; | |
| } | |
| if(den%num==0){ | |
| den/=num; | |
| num=1; | |
| } | |
| } | |
| } | |
| public void add (Fraction that){ | |
| num=this.num*that.den+that.num*this.den; | |
| den=this.den*that.den; | |
| this.simplify(); | |
| } | |
| public void sub (Fraction that){ | |
| num=this.num*that.den-that.num*this.den; | |
| den=this.den*that.den; | |
| this.simplify(); | |
| } | |
| public void mult (Fraction that){ | |
| num=this.num*that.num; | |
| den=this.den*that.den; | |
| this.simplify(); | |
| } | |
| public void div (Fraction that){ | |
| num=this.num*den; | |
| den=this.den*num; | |
| this.simplify(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment