| 1 | package com.hcarralero.toljava; |
|---|
| 2 | |
|---|
| 3 | /** |
|---|
| 4 | * Representa a un término en un Polyn de TOL. |
|---|
| 5 | * |
|---|
| 6 | * @author Humberto Andrés Carralero López (hcarralero@gmail.com) |
|---|
| 7 | * @version 1.0 |
|---|
| 8 | */ |
|---|
| 9 | public class TOLPolynTerm { |
|---|
| 10 | |
|---|
| 11 | /** |
|---|
| 12 | * Tipo de variable del término (B/F). |
|---|
| 13 | */ |
|---|
| 14 | protected int var; |
|---|
| 15 | |
|---|
| 16 | /** |
|---|
| 17 | * Obtiene el tipo de variable del término. |
|---|
| 18 | * |
|---|
| 19 | * @return B(0) o F(1), según sea lo almacenado en el término. |
|---|
| 20 | */ |
|---|
| 21 | public int getVar() { |
|---|
| 22 | return var; |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | /** |
|---|
| 26 | * Establece el tipo de variable del término. |
|---|
| 27 | * |
|---|
| 28 | * @param var el tipo de la variable (B o F). |
|---|
| 29 | */ |
|---|
| 30 | public void setVar(int var) { |
|---|
| 31 | if (var != 0) { |
|---|
| 32 | this.var = 1; |
|---|
| 33 | } else { |
|---|
| 34 | this.var = 0; |
|---|
| 35 | } |
|---|
| 36 | } |
|---|
| 37 | /** |
|---|
| 38 | * Coeficiente del término. |
|---|
| 39 | */ |
|---|
| 40 | protected double coef; |
|---|
| 41 | |
|---|
| 42 | /** |
|---|
| 43 | * Obtiene el coeficiente del término. |
|---|
| 44 | * |
|---|
| 45 | * @return el coeficiente del término. |
|---|
| 46 | */ |
|---|
| 47 | public double getCoef() { |
|---|
| 48 | return coef; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | /** |
|---|
| 52 | * Establece el coeficiente del término. |
|---|
| 53 | * |
|---|
| 54 | * @param coef coeficiente del término. |
|---|
| 55 | */ |
|---|
| 56 | public void setCoef(double coef) { |
|---|
| 57 | this.coef = coef; |
|---|
| 58 | } |
|---|
| 59 | /** |
|---|
| 60 | * Grado del término. |
|---|
| 61 | */ |
|---|
| 62 | protected int grade; |
|---|
| 63 | |
|---|
| 64 | /** |
|---|
| 65 | * Obtiene el grado del término. |
|---|
| 66 | * |
|---|
| 67 | * @return el grado del término. |
|---|
| 68 | */ |
|---|
| 69 | public int getGrade() { |
|---|
| 70 | return grade; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | /** |
|---|
| 74 | * Establece el grado del término. |
|---|
| 75 | * |
|---|
| 76 | * @param grade el grado del término. |
|---|
| 77 | */ |
|---|
| 78 | public void setGrade(int grade) { |
|---|
| 79 | this.grade = grade; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | /** |
|---|
| 83 | * Contruye un <code>TOLPolynTerm</code> (término de un polinomio) usando los datos pasados por parámetro. |
|---|
| 84 | * |
|---|
| 85 | * @param var tipo de variable (B/F) del término. |
|---|
| 86 | * @param coef coeficiente del término. |
|---|
| 87 | * @param grade grado del término. |
|---|
| 88 | */ |
|---|
| 89 | public TOLPolynTerm(int var, double coef, int grade) { |
|---|
| 90 | if (var == 0) { |
|---|
| 91 | this.var = TOLPolyn.B; |
|---|
| 92 | } else { |
|---|
| 93 | this.var = TOLPolyn.F; |
|---|
| 94 | } |
|---|
| 95 | this.coef = coef; |
|---|
| 96 | this.grade = grade; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | /** |
|---|
| 100 | * Compara a este objeto con otro. |
|---|
| 101 | * |
|---|
| 102 | * @param obj el objeto con el que se comparará. |
|---|
| 103 | * @return <code>true</code> si el objeto pasado como parámetro es de tipo <code>TOLPolynTerm</code> |
|---|
| 104 | * y tiene valores idénticos para variable, coeficiente y grado. |
|---|
| 105 | */ |
|---|
| 106 | @Override |
|---|
| 107 | public boolean equals(Object obj) { |
|---|
| 108 | if (obj == null) { |
|---|
| 109 | return false; |
|---|
| 110 | } |
|---|
| 111 | if (getClass() != obj.getClass()) { |
|---|
| 112 | return false; |
|---|
| 113 | } |
|---|
| 114 | final TOLPolynTerm other = (TOLPolynTerm) obj; |
|---|
| 115 | if (this.var != other.var) { |
|---|
| 116 | return false; |
|---|
| 117 | } |
|---|
| 118 | if (this.coef != other.coef) { |
|---|
| 119 | return false; |
|---|
| 120 | } |
|---|
| 121 | if (this.grade != other.grade) { |
|---|
| 122 | return false; |
|---|
| 123 | } |
|---|
| 124 | return true; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | /** |
|---|
| 128 | * Calcula el código hash asignado al objeto. |
|---|
| 129 | * |
|---|
| 130 | * @return el código hash asignado al objeto. |
|---|
| 131 | */ |
|---|
| 132 | @Override |
|---|
| 133 | public int hashCode() { |
|---|
| 134 | int hash = 5; |
|---|
| 135 | hash = 11 * hash + this.var; |
|---|
| 136 | hash = 11 * hash + (int) (Double.doubleToLongBits(this.coef) ^ (Double.doubleToLongBits(this.coef) >>> 32)); |
|---|
| 137 | hash = 11 * hash + this.grade; |
|---|
| 138 | return hash; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | /** |
|---|
| 142 | * Obtiene una representación como cadena de texto del término del polinomio |
|---|
| 143 | * usando la sintaxis de TOL. |
|---|
| 144 | * |
|---|
| 145 | * @return la representación como cadena de texto del término de polinomio. |
|---|
| 146 | */ |
|---|
| 147 | @Override |
|---|
| 148 | public String toString() { |
|---|
| 149 | String res = ""; |
|---|
| 150 | if (this.coef != 0) { |
|---|
| 151 | if (this.coef == -1 && this.grade != 0) { |
|---|
| 152 | res += "-"; |
|---|
| 153 | } else { |
|---|
| 154 | if (this.coef != 1 || this.grade == 0) { |
|---|
| 155 | res += this.coef; |
|---|
| 156 | } |
|---|
| 157 | } |
|---|
| 158 | if (this.grade > 0) { |
|---|
| 159 | if (this.coef != -1 && this.coef != 1) { |
|---|
| 160 | res += "*"; |
|---|
| 161 | } |
|---|
| 162 | if (this.var == TOLPolyn.B) { |
|---|
| 163 | res += "B"; |
|---|
| 164 | } else { |
|---|
| 165 | res += "F"; |
|---|
| 166 | } |
|---|
| 167 | if (this.grade > 1) { |
|---|
| 168 | res += "^" + grade; |
|---|
| 169 | } |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | return res; |
|---|
| 173 | } |
|---|
| 174 | } |
|---|