Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package compitojavauno;
- /**
- *
- * @author Aleksandar Djokic
- */
- public class Razionale {
- private int numeratore;
- private int denominatore;
- public Razionale(int numeratore, int denominatore){
- this.numeratore=numeratore;
- this.denominatore=denominatore;
- }
- public int getNum(){
- return numeratore;
- }
- public int getDen(){
- return denominatore;
- }
- public static Razionale plus(Razionale primo, Razionale secondo)
- {
- Razionale risultato = new Razionale(0,0);
- /*a/b + c/d = (ad + bc)/(bd)*/
- risultato.numeratore=(primo.numeratore*secondo.denominatore)+(primo.denominatore*secondo.numeratore);
- risultato.denominatore=primo.denominatore*secondo.denominatore;
- return risultato;
- }
- public static Razionale times(Razionale primo, Razionale secondo){
- Razionale prodotto = new Razionale(0,0);
- /*a/b ∗ c/d = (ac)/(bd)*/
- prodotto.numeratore=primo.numeratore*secondo.numeratore;
- prodotto.denominatore=primo.denominatore*secondo.denominatore;
- return prodotto;
- }
- }
- //MAIN
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package compitojavauno;
- /**
- *
- * @author Aleksandar Djokic
- */
- public class CompitoJavaUno {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- Razionale somma = new Razionale(0,0);
- Razionale n1= new Razionale(4,4);
- Razionale n2=new Razionale(3,4);
- somma=Razionale.plus(n1,n2);
- System.out.println("Il risultato e'-> numeratore "+somma.getNum() +" denominatore->"+somma.getDen());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement