Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.ip.sesion10;
- public class Potencia implements Comparable<Object> {
- private int base;
- private int exponente;
- public Potencia() {
- this.base = 0;
- this.exponente = 0;
- }
- public Potencia(int base, int exponente) {
- this.base = base;
- if (exponente < 0)
- this.exponente = -exponente;
- else
- this.exponente = exponente;
- }
- public Potencia(Potencia potencia) {
- this.base = potencia.getBase();
- if (potencia.getExponente() < 0)
- this.exponente = -potencia.getExponente();
- else
- this.exponente = potencia.getExponente();
- }
- public int getBase() {
- return base;
- }
- public void setBase(int base) {
- this.base = base;
- }
- public int getExponente() {
- return exponente;
- }
- public void setExponente(int exponente) {
- this.exponente = exponente;
- }
- public double getValor() {
- if (exponente == 0)
- return 1.0;
- else
- return Math.pow(base, exponente);
- }
- @Override
- public String toString() {
- return "(" + base + ")^{" + exponente + "}";
- }
- public Potencia producto(Potencia potencia) {
- if (this.base != potencia.getBase())
- throw new RuntimeException("Las potencias no tienen la misma base");
- int nuevoExponente = this.exponente + potencia.getExponente();
- return new Potencia(this.base, nuevoExponente);
- }
- public Potencia elevarPotencia(int exponente) {
- if (exponente < 0)
- throw new RuntimeException("El exponente no puede ser negativo");
- int nuevoExponente = this.exponente + exponente;
- return new Potencia(this.base, nuevoExponente);
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj)
- return true;
- if (obj == null)
- return false;
- if (getClass() != obj.getClass())
- return false;
- if (!(obj instanceof Potencia))
- return false;
- Potencia otro =(Potencia) obj;
- if (base != otro.getBase())
- return false;
- if (exponente != otro.getExponente())
- return false;
- return true;
- }
- public int compareTo(Object o) {
- Potencia otro = (Potencia)o;
- double valor = getValor();
- double valorOtro = otro.getValor();
- if (valor > valorOtro)
- return 1;
- else if (valor < valorOtro)
- return -1;
- else
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement