Created
November 20, 2011 21:28
-
-
Save rodrigomanhaes/1380960 to your computer and use it in GitHub Desktop.
Um diálogo sobre orientação a objetos (códigos do post http://programacaoradical.blogspot.com/2010/11/blog-post.html)
This file contains 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
public class Fatorial { | |
public long fatorial(long n) { | |
if (n == 0) | |
return 1; | |
return n * fatorial(n - 1); | |
} | |
} |
This file contains 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
public class Numero { | |
public long fatorial(long n) { | |
if (n == 0) | |
return 1; | |
return n * fatorial(n - 1); | |
} | |
} |
This file contains 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
public class Numero { | |
private long valor; | |
public Numero(long valor) { | |
this.valor = valor; | |
} | |
public long fatorial(long n) { | |
if (n == 0) | |
return 1; | |
return n * fatorial(n - 1); | |
} | |
} |
This file contains 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
public class Numero { | |
private long valor; | |
public Numero(long valor) { | |
this.valor = valor; | |
} | |
public long fatorial() { | |
if (valor == 0) | |
return 1; | |
return valor * fatorial(...); // e agora #comofas??? | |
} | |
} |
This file contains 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
public class Numero { | |
private long valor; | |
public Numero(long valor) { | |
this.valor = valor; | |
} | |
public long fatorial() { | |
if (valor == 0) | |
return 1; | |
Numero valorMenosUm = new Numero(valor - 1); | |
return valor * valorMenosUm.fatorial(); | |
} | |
} |
This file contains 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
import java.math.BigInteger; | |
import static java.math.BigInteger.*; | |
public class Numero { | |
private BigInteger valor; | |
public Numero(long valor) { | |
this.valor = new BigInteger(String.valueOf(valor)); | |
} | |
public Numero(BigInteger valor) { | |
this.valor = valor; | |
} | |
public BigInteger fatorial() { | |
if (valor == ZERO) | |
return ONE; | |
Numero valorMenosUm = new Numero(valor.subtract(ONE)); | |
return valor.multiply(new Numero(valorMenosUm).fatorial()); | |
} | |
} |
This file contains 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
import java.math.BigInteger; | |
public class Numero { | |
private BigInteger valor; | |
public Numero(long valor) { | |
this.valor = new BigInteger(String.valueOf(valor)); | |
} | |
public Numero(BigInteger valor) { | |
this.valor = valor; | |
} | |
public Numero fatorial() { | |
if (this.equals(new Numero(0))) | |
return new Numero(1); | |
return this.multiply(this.subtract(new Numero(1)).fatorial()); | |
} | |
public Numero subtract(Numero outro) { | |
return new Numero(this.valor.subtract(outro.valor)); | |
} | |
public Numero multiply(Numero outro) { | |
return new Numero(this.valor.multiply(outro.valor)); | |
} | |
public boolean equals(Numero outro) { | |
return this.valor.equals(outro.valor); | |
} | |
} |
This file contains 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
Numero numero = new Numero(3); | |
numero.fatorial(5); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment