Guest User

Untitled

a guest
Jan 22nd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1. //Jag tror jag deklarerar fel, i konstruktor 4 får jag ett fel om att den inte finner symbol.
  2. public class Rational{
  3.     public Rational(int a, int b){
  4.         if(b==0){
  5.             throw new IllegalStateException("Rational.Rational(int,int): zero denominator");
  6.         }else{
  7.             int storsta_divisor = gcd(a,b);
  8.             a = a / storsta_divisor;
  9.             b = b / storsta_divisor;
  10.         }  
  11.     }//konstruktor_1
  12.    
  13.     public Rational(){
  14.         this(0,1);
  15.     }//konstruktor_2
  16.    
  17.     public Rational(int a){
  18.         this(a,1);
  19.     }//konstruktor_3
  20.    
  21.     public Rational(Rational other){
  22.         this.a = other.a;
  23.         this.b = other.b;
  24.     }//konstruktor_4
  25.    
  26.     public Rational clone(){
  27.         Rational clone = new Rational(a,b);
  28.     }//Rational clone
  29.    
  30.     public boolean equals(Rational other){
  31.         double x, y;
  32.        
  33.         x = this.a /this.b;
  34.         y = other.a /other.b;
  35.        
  36.         if(x == y){
  37.             return true;
  38.         }else{
  39.             return false;
  40.         }
  41.     }//konstruktor_6
  42.    
  43.     public int getNumerator(){
  44.         return a;
  45.     }//getNumerator
  46.    
  47.     public int getDenominator(){
  48.         return b;
  49.     }//getDenominator
  50.          
  51.     public static int gcd(int a, int b){
  52.       int taljare, namnare, kvot;
  53.       int rest;
  54.        
  55.       if (a == 0 && b == 0){
  56.          throw new ArithmeticException("Rational.gcd(0,0) is undefined");
  57.       }
  58.        
  59.         a = Math.abs(a);
  60.       b = Math.abs(b);
  61.  
  62.         if(a>b){
  63.         taljare = a;
  64.           namnare = b;
  65.       }else{
  66.          taljare = b;
  67.          namnare = a;
  68.         }
  69.         if(namnare==0){
  70.             return taljare;
  71.         }  
  72.         rest = taljare % namnare;      
  73.  
  74.         while(rest!=0){    
  75.             taljare = namnare;
  76.             namnare = rest;
  77.             rest = taljare % namnare;  
  78.         }
  79.         return namnare;
  80.        
  81.    }//gcd
  82. }//class
Add Comment
Please, Sign In to add comment