Advertisement
AleksandarArkan

J- Razionale

Sep 3rd, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.16 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 compitojavauno;
  7.  
  8. /**
  9.  *
  10.  * @author Aleksandar Djokic
  11.  */
  12. public class Razionale {
  13.    
  14.     private int numeratore;
  15.     private int denominatore;
  16.    
  17.     public Razionale(int numeratore, int denominatore){
  18.         this.numeratore=numeratore;
  19.         this.denominatore=denominatore;
  20.     }
  21.    
  22.     public int getNum(){
  23.         return numeratore;
  24.     }
  25.    
  26.     public int getDen(){
  27.         return denominatore;
  28.     }
  29.    
  30.    
  31.     public static Razionale plus(Razionale primo, Razionale secondo)  
  32.     {
  33.         Razionale risultato = new Razionale(0,0);
  34.         /*a/b + c/d = (ad + bc)/(bd)*/
  35.         risultato.numeratore=(primo.numeratore*secondo.denominatore)+(primo.denominatore*secondo.numeratore);
  36.         risultato.denominatore=primo.denominatore*secondo.denominatore;
  37.        
  38.         return risultato;
  39.     }
  40.    
  41.     public static Razionale times(Razionale primo, Razionale secondo){
  42.         Razionale prodotto = new Razionale(0,0);
  43.        
  44.         /*a/b ∗ c/d = (ac)/(bd)*/
  45.        
  46.         prodotto.numeratore=primo.numeratore*secondo.numeratore;
  47.         prodotto.denominatore=primo.denominatore*secondo.denominatore;
  48.        
  49.         return prodotto;
  50.        
  51.     }
  52. }
  53.  
  54.  
  55. //MAIN
  56.  
  57. /*
  58.  * To change this license header, choose License Headers in Project Properties.
  59.  * To change this template file, choose Tools | Templates
  60.  * and open the template in the editor.
  61.  */
  62. package compitojavauno;
  63.  
  64. /**
  65.  *
  66.  * @author Aleksandar Djokic
  67.  */
  68. public class CompitoJavaUno {
  69.  
  70.     /**
  71.      * @param args the command line arguments
  72.      */
  73.     public static void main(String[] args) {
  74.         // TODO code application logic here
  75.         Razionale somma = new Razionale(0,0);
  76.         Razionale n1= new Razionale(4,4);
  77.         Razionale n2=new Razionale(3,4);
  78.         somma=Razionale.plus(n1,n2);
  79.         System.out.println("Il risultato e'-> numeratore "+somma.getNum() +" denominatore->"+somma.getDen());
  80.        
  81.        
  82.     }
  83.    
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement