Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Jag tror jag deklarerar fel, i konstruktor 4 får jag ett fel om att den inte finner symbol.
- public class Rational{
- public Rational(int a, int b){
- if(b==0){
- throw new IllegalStateException("Rational.Rational(int,int): zero denominator");
- }else{
- int storsta_divisor = gcd(a,b);
- a = a / storsta_divisor;
- b = b / storsta_divisor;
- }
- }//konstruktor_1
- public Rational(){
- this(0,1);
- }//konstruktor_2
- public Rational(int a){
- this(a,1);
- }//konstruktor_3
- public Rational(Rational other){
- this.a = other.a;
- this.b = other.b;
- }//konstruktor_4
- public Rational clone(){
- Rational clone = new Rational(a,b);
- }//Rational clone
- public boolean equals(Rational other){
- double x, y;
- x = this.a /this.b;
- y = other.a /other.b;
- if(x == y){
- return true;
- }else{
- return false;
- }
- }//konstruktor_6
- public int getNumerator(){
- return a;
- }//getNumerator
- public int getDenominator(){
- return b;
- }//getDenominator
- public static int gcd(int a, int b){
- int taljare, namnare, kvot;
- int rest;
- if (a == 0 && b == 0){
- throw new ArithmeticException("Rational.gcd(0,0) is undefined");
- }
- a = Math.abs(a);
- b = Math.abs(b);
- if(a>b){
- taljare = a;
- namnare = b;
- }else{
- taljare = b;
- namnare = a;
- }
- if(namnare==0){
- return taljare;
- }
- rest = taljare % namnare;
- while(rest!=0){
- taljare = namnare;
- namnare = rest;
- rest = taljare % namnare;
- }
- return namnare;
- }//gcd
- }//class
Add Comment
Please, Sign In to add comment