Advertisement
Guest User

Potencia

a guest
Jan 21st, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.18 KB | None | 0 0
  1. package org.ip.sesion10;
  2.  
  3. public class Potencia implements Comparable<Object> {
  4.  
  5.     private int base;
  6.     private int exponente;
  7.  
  8.     public Potencia() {
  9.         this.base = 0;
  10.         this.exponente = 0;
  11.     }
  12.  
  13.     public Potencia(int base, int exponente) {
  14.         this.base = base;
  15.         if (exponente < 0)
  16.             this.exponente = -exponente;
  17.         else
  18.             this.exponente = exponente;
  19.     }
  20.  
  21.     public Potencia(Potencia potencia) {
  22.         this.base = potencia.getBase();
  23.         if (potencia.getExponente() < 0)
  24.             this.exponente = -potencia.getExponente();
  25.         else
  26.             this.exponente = potencia.getExponente();
  27.     }
  28.  
  29.     public int getBase() {
  30.         return base;
  31.     }
  32.  
  33.     public void setBase(int base) {
  34.         this.base = base;
  35.     }
  36.  
  37.     public int getExponente() {
  38.         return exponente;
  39.     }
  40.  
  41.     public void setExponente(int exponente) {
  42.         this.exponente = exponente;
  43.     }
  44.  
  45.     public double getValor() {
  46.         if (exponente == 0)
  47.             return 1.0;
  48.         else
  49.             return Math.pow(base, exponente);
  50.     }
  51.  
  52.     @Override
  53.     public String toString() {
  54.         return "(" + base + ")^{" + exponente + "}";
  55.     }
  56.  
  57.     public Potencia producto(Potencia potencia) {
  58.         if (this.base != potencia.getBase())
  59.             throw new RuntimeException("Las potencias no tienen la misma base");
  60.         int nuevoExponente = this.exponente + potencia.getExponente();
  61.         return new Potencia(this.base, nuevoExponente);
  62.     }
  63.  
  64.     public Potencia elevarPotencia(int exponente) {
  65.         if (exponente < 0)
  66.             throw new RuntimeException("El exponente no puede ser negativo");
  67.         int nuevoExponente = this.exponente + exponente;
  68.         return new Potencia(this.base, nuevoExponente);
  69.     }
  70.  
  71.     @Override
  72.     public boolean equals(Object obj) {
  73.         if (this == obj)
  74.             return true;
  75.         if (obj == null)
  76.             return false;
  77.         if (getClass() != obj.getClass())
  78.             return false;
  79.         if (!(obj instanceof Potencia))
  80.             return false;
  81.  
  82.         Potencia otro =(Potencia) obj;
  83.  
  84.         if (base != otro.getBase())
  85.             return false;
  86.         if (exponente != otro.getExponente())
  87.             return false;
  88.         return true;
  89.     }
  90.  
  91.     public int compareTo(Object o) {
  92.         Potencia otro = (Potencia)o;
  93.  
  94.         double valor = getValor();
  95.         double valorOtro = otro.getValor();
  96.  
  97.         if (valor > valorOtro)
  98.             return 1;
  99.         else if (valor < valorOtro)
  100.             return -1;
  101.         else
  102.             return 0;
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement