-
-
Save parzibyte/32a8b525153e209355fd8642eb0a26be to your computer and use it in GitHub Desktop.
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
/* | |
____ _____ _ _ _ | |
| _ \ | __ \ (_) | | | | |
| |_) |_ _ | |__) |_ _ _ __ _____| |__ _ _| |_ ___ | |
| _ <| | | | | ___/ _` | '__|_ / | '_ \| | | | __/ _ \ | |
| |_) | |_| | | | | (_| | | / /| | |_) | |_| | || __/ | |
|____/ \__, | |_| \__,_|_| /___|_|_.__/ \__, |\__\___| | |
__/ | __/ | | |
|___/ |___/ | |
____________________________________ | |
/ Si necesitas ayuda, contáctame en \ | |
\ https://parzibyte.me / | |
------------------------------------ | |
\ ^__^ | |
\ (oo)\_______ | |
(__)\ )\/\ | |
||----w | | |
|| || | |
Creado por Parzibyte (https://parzibyte.me). | |
------------------------------------------------------------------------------------------------ | |
| IMPORTANTE | | |
Si vas a borrar este encabezado, considera: | |
Seguirme: https://parzibyte.me/blog/sigueme/ | |
Y compartir mi blog con tus amigos | |
También tengo canal de YouTube: https://www.youtube.com/channel/UCroP4BTWjfM0CkGB6AFUoBg?sub_confirmation=1 | |
Twitter: https://twitter.com/parzibyte | |
Facebook: https://facebook.com/parzibyte.fanpage | |
Instagram: https://instagram.com/parzibyte | |
Hacer una donación vía PayPal: https://paypal.me/LuisCabreraBenito | |
------------------------------------------------------------------------------------------------ | |
*/ | |
package me.parzibyte; | |
import java.util.Objects; | |
class Fraccion { | |
private int numerador, denominador; | |
// Constructores | |
public Fraccion() { | |
this.numerador = 1; | |
this.denominador = 1; | |
} | |
public Fraccion(int numerador) { | |
this.numerador = numerador; | |
this.denominador = 1; | |
} | |
public Fraccion(int numerador, int denominador) { | |
this.numerador = numerador; | |
// Llamar al setter para que se haga la validación del denominador | |
this.setDenominador(denominador); | |
} | |
public Fraccion(Fraccion f) { | |
this.numerador = f.getNumerador(); | |
this.denominador = f.getDenominador(); | |
} | |
// Getters y setters | |
public int getNumerador() { | |
return this.numerador; | |
} | |
public int getDenominador() { | |
return this.denominador; | |
} | |
public void setNumerador(int numerador) { | |
this.numerador = numerador; | |
} | |
public void setDenominador(int denominador) { | |
if (denominador == 0) { | |
throw new IllegalArgumentException("Denominador debe ser distinto de 0"); | |
} | |
this.denominador = denominador; | |
} | |
// Métodos ayudantes | |
private int mcm(int a, int b) { | |
return (a * b) / this.maximoComunDivisor(a, b); | |
} | |
public int maximoComunDivisor(int a, int b) { | |
int temporal;//Para no perder b | |
while (b != 0) { | |
temporal = b; | |
b = a % b; | |
a = temporal; | |
} | |
return a; | |
} | |
// Métodos principales | |
public Fraccion suma(Fraccion otra) { | |
int mcm = this.mcm(this.getDenominador(), otra.getDenominador()); | |
int diferenciaFraccionActual = mcm / this.getDenominador(); | |
int diferenciaOtraFraccion = mcm / otra.getDenominador(); | |
Fraccion resultado = new Fraccion(); | |
resultado.setNumerador( | |
(diferenciaFraccionActual * this.getNumerador()) + (diferenciaOtraFraccion * otra.getNumerador())); | |
resultado.setDenominador(mcm); | |
return resultado; | |
} | |
public Fraccion resta(Fraccion otra) { | |
int mcm = this.mcm(this.getDenominador(), otra.getDenominador()); | |
int diferenciaFraccionActual = mcm / this.getDenominador(); | |
int diferenciaOtraFraccion = mcm / otra.getDenominador(); | |
Fraccion resultado = new Fraccion(); | |
resultado.setNumerador((diferenciaFraccionActual * this.getNumerador()) - (diferenciaOtraFraccion * otra.getNumerador())); | |
resultado.setDenominador(mcm); | |
return resultado; | |
} | |
public Fraccion producto(Fraccion otra) { | |
return new Fraccion(this.getNumerador() * otra.getNumerador(), this.getDenominador() * otra.getDenominador()); | |
} | |
public Fraccion cociente(Fraccion otra) { | |
return new Fraccion(this.getNumerador() * otra.getDenominador(), this.getDenominador() * otra.getNumerador()); | |
} | |
public Fraccion inversa() { | |
return new Fraccion(this.getDenominador(), this.getNumerador()); | |
} | |
public Fraccion potencia(int exponente) { | |
return new Fraccion((int) Math.pow(this.getNumerador(), exponente), (int) Math.pow(this.getDenominador(), exponente)); | |
} | |
public Fraccion simplifica() { | |
int mcd = this.maximoComunDivisor(this.getNumerador(), this.getDenominador()); | |
return new Fraccion(this.getNumerador() / mcd, this.getDenominador() / mcd); | |
} | |
// Heredados | |
@Override | |
public String toString() { | |
return String.valueOf(this.getNumerador()).concat("/").concat(String.valueOf(this.getDenominador())); | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) return true; | |
if (o == null || getClass() != o.getClass()) return false; | |
Fraccion fraccion = (Fraccion) o; | |
return numerador == fraccion.numerador && denominador == fraccion.denominador; | |
} | |
@Override | |
public int hashCode() { | |
return Objects.hash(numerador, denominador); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment