Advertisement
Voldemord

[Java] DodawaniaOdejmowanieSkracanieUlamkow

Mar 28th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.09 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package zadanie3;
  7.  
  8. /**
  9.  *
  10.  * @author Student
  11.  */
  12. public class Main {
  13.  
  14.     /**
  15.      * @param args the command line arguments
  16.      */
  17.     public static void main(String[] args) {
  18.         try{
  19.             Ulamek u = new Ulamek(2,6);
  20.             Ulamek b = new Ulamek(1,6);
  21.             u.addUlamek(b);
  22.             u.downUlamek();
  23.             System.out.println("Wynik = " + u.getLicznik() + "/" + u.getMianownik());
  24.             System.out.println(b);
  25.         }catch (Exception e){
  26.             System.out.println(e.getMessage());
  27.         }
  28.     }
  29.    
  30. }
  31.  
  32.  
  33.  
  34. //------------------------------------------------
  35.  
  36.  
  37. /*
  38.  * To change this license header, choose License Headers in Project Properties.
  39.  * To change this template file, choose Tools | Templates
  40.  * and open the template in the editor.
  41.  */
  42. package zadanie3;
  43.  
  44. /**
  45.  *
  46.  * @author Student
  47.  */
  48. public class Ulamek {
  49.  
  50.  
  51.     private int licznik;
  52.     private int mianownik;
  53.    
  54.    
  55.     public Ulamek(int licznik, int mianownik) throws Exception{
  56.         this.licznik = licznik;
  57.         this.mianownik = mianownik;
  58.         if(mianownik == 0)
  59.             throw new Exception("Wartosc 0 niedozwolona w mianowniku");
  60.     }
  61.    
  62.     public void downUlamek() throws Exception{
  63.         try{
  64.             Nwd n = new Nwd(this.licznik, this.mianownik);
  65.             int x = n.getNwd();
  66.             this.licznik /= x;
  67.             this.mianownik /= x;
  68.         }catch (Exception e){
  69.             throw new Exception("NWD problem",e);
  70.         }
  71.     }
  72.     public void addUlamek(Ulamek b) throws Exception{
  73.         if(mianownik == b.mianownik)
  74.             this.licznik += b.licznik;
  75.         else if(mianownik != b.mianownik){
  76.             int x = this.mianownik;
  77.             this.licznik *= b.mianownik;
  78.             this.mianownik *= b.mianownik;
  79.             b.mianownik *= x;
  80.             this.licznik += b.mianownik;
  81.         }
  82.         else
  83.             throw new Exception("Cos Sie ... zepsulo");
  84.     }
  85.    
  86.     public void subUlamek(Ulamek b) throws Exception{
  87.         if(mianownik == b.mianownik)
  88.             this.licznik -= b.licznik;
  89.         else if(mianownik != b.mianownik){
  90.             int x = this.mianownik;
  91.             this.licznik *= b.mianownik;
  92.             this.mianownik *= b.mianownik;
  93.             b.mianownik *= x;
  94.             this.licznik -= b.mianownik;
  95.         }
  96.         else
  97.             throw new Exception("Cos Sie ... zepsulo");
  98.     }
  99.    
  100.  
  101.     public int getLicznik() {
  102.         return licznik;
  103.     }
  104.  
  105.     public void setLicznik(int licznik) {
  106.         this.licznik = licznik;
  107.     }
  108.  
  109.     public int getMianownik() {
  110.         return mianownik;
  111.     }
  112.  
  113.  
  114.     public void setMianownik(int mianownik) {
  115.         this.mianownik = mianownik;
  116.     }
  117.  
  118.     @Override
  119.     public String toString() {
  120.         return licznik + "/" + mianownik;
  121.     }
  122.    
  123.    
  124. }
  125.  
  126.  
  127. //---------------------------------
  128. //NWD macie na moim paste
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement