Skip to content
Snippets Groups Projects
Commit 3ecd7030 authored by RADELLAH Badr's avatar RADELLAH Badr
Browse files

Ex4 - Q2

parent 66e6183d
No related branches found
No related tags found
No related merge requests found
package formula;
public class Product implements Formula {
private Formula x;
private Formula y;
private Formula[] formulas;
public Product(Formula x, Formula y) {
this.x = x;
this.y = y;
// Constructeur prenant un tableau de formules
public Product(Formula... formulas) {
this.formulas = formulas;
}
@Override
public String asString() {
return "(" + x.asString() + "*" + y.asString() + ")";
StringBuilder sb = new StringBuilder("(");
for (int i = 0; i < formulas.length; i++) {
sb.append(formulas[i].asString());
if (i < formulas.length - 1) sb.append("*"); // Ajouter un "*" entre les formules
}
sb.append(")");
return sb.toString();
}
@Override
public double asValue() {
return x.asValue() * y.asValue();
double product = 1;
for (Formula formula : formulas) {
product *= formula.asValue();
}
return product;
}
}
package formula;
public class Sum implements Formula {
private Formula x;
private Formula y;
private Formula[] formulas;
public Sum(Formula x, Formula y) {
this.x = x;
this.y = y;
// Constructeur prenant un tableau de formules
public Sum(Formula... formulas) {
this.formulas = formulas;
}
@Override
public String asString() {
return "(" + x.asString() + "+" + y.asString() + ")";
StringBuilder sb = new StringBuilder("(");
for (int i = 0; i < formulas.length; i++) {
sb.append(formulas[i].asString());
if (i < formulas.length - 1) sb.append("+"); // Ajouter un "+" entre les formules
}
sb.append(")");
return sb.toString();
}
@Override
public double asValue() {
return x.asValue() + y.asValue();
double sum = 0;
for (Formula formula : formulas) {
sum += formula.asValue();
}
return sum;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment