Created
September 20, 2022 07:53
-
-
Save ProfAndreaPollini/ffc101dc7fd6366cd34b4faf86a48c0f to your computer and use it in GitHub Desktop.
Frazioni e loro stampa: lezione 20-09-2022
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license | |
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template | |
*/ | |
package frazioni; | |
/** | |
* | |
* @author 15367519 | |
*/ | |
class Frazione { | |
private int num; | |
private int den; | |
Frazione(int num, int den) { | |
this.num = num; | |
this.den = den; | |
} | |
public Frazione reciproco() { | |
return new Frazione(den,num); | |
} | |
@Override | |
public String toString() { | |
//return "Frazione{" + num + "/" + den + '}'; | |
return String.format("Frazione{%d/%d}", num,den); | |
} | |
public int getNum() { | |
return num; | |
} | |
public void setNum(int num) { | |
this.num = num; | |
} | |
public int getDen() { | |
return den; | |
} | |
public void setDen(int den) { | |
this.den = den; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license | |
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template | |
*/ | |
package frazioni; | |
/** | |
* | |
* @author 15367519 | |
*/ | |
public class Frazioni { | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
// TODO code application logic here | |
//var printer =new StampatoreDiFrazioni(); | |
Frazione f1 = new Frazione(156622,33333); | |
System.out.println(f1); | |
System.out.println(f1.reciproco()); | |
StampatoreDiFrazioni.stampa(f1); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license | |
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template | |
*/ | |
package frazioni; | |
/** | |
* | |
* @author 15367519 | |
*/ | |
public class StampatoreDiFrazioni { | |
public StampatoreDiFrazioni() { | |
} | |
public static void stampa(Frazione f) { | |
var snum = String.valueOf(f.getNum()); | |
var sden = String.valueOf(f.getDen()); | |
var n = Math.max(snum.length(),sden.length()); | |
var linea = ""; | |
for(int i = 0; i<n;i++) { | |
linea += "-"; | |
} | |
System.out.println(f.getNum()); | |
System.out.println(linea); | |
System.out.println(f.getDen()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment