Para exercitar a utilização do padrão de projeto Composite, desenvolva uma interpretador de expressões aritméticas.
A expressão aritmética (Component) deverá ter um método para resolver a expressão, retornando o seu valor numérico.
As constantes numéricas (Leaf) retornarão o seu próprio valor, por exemplo, a contante numérica 5 retornará o valor 5.
As operações (Composite) retornarão o valor da operação, no formato operando operador operando.
/**
* Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
* Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
*
* Permission is granted to copy, distribute and/or modify this document
* under the terms of the GNU Free Documentation License, Version 1.3
* or any later version published by the Free Software Foundation; with
* no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
* A copy of the license is included in the section entitled "GNU
* Free Documentation License".
*/
package com.ybadoo.tutoriais.poo;
/**
* Component
*/
public interface Expressao
{
/**
* Resolver a expressao aritmetica
*
* @return resultado da expressao aritmetica
*/
public int resolver();
}
/**
* Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
* Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
*
* Permission is granted to copy, distribute and/or modify this document
* under the terms of the GNU Free Documentation License, Version 1.3
* or any later version published by the Free Software Foundation; with
* no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
* A copy of the license is included in the section entitled "GNU
* Free Documentation License".
*/
package com.ybadoo.tutoriais.poo;
/**
* Leaf
*/
public class Constante implements Expressao
{
/**
* Valor da constante numerica
*/
private int valor;
/**
* Construtor para inicializar o valor da constante numerica
*
* @param valor valor da constante numerica
*/
public Constante(int valor)
{
this.valor = valor;
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.Expressao#resolver()
*/
public int resolver()
{
return this.valor;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString()
{
return new Integer(this.valor).toString();
}
}
/**
* Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
* Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
*
* Permission is granted to copy, distribute and/or modify this document
* under the terms of the GNU Free Documentation License, Version 1.3
* or any later version published by the Free Software Foundation; with
* no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
* A copy of the license is included in the section entitled "GNU
* Free Documentation License".
*/
package com.ybadoo.tutoriais.poo;
/**
* Composite
*/
public class Operacao implements Expressao
{
/**
* Operador da operacao
*/
private char operador;
/**
* Primeiro operando da operacao
*/
private Expressao operandoA;
/**
* Segundo operando da operacao
*/
private Expressao operandoB;
/**
* Construtor para inicializar a operacao
*
* @param operandoA primeiro operando da operacao
* @param operador operador da operacao
* @param operandoB segundo operando da operacao
*/
public Operacao(Expressao operandoA, char operador, Expressao operandoB)
{
this.operandoA = operandoA;
this.operador = operador;
this.operandoB = operandoB;
}
/* (non-Javadoc)
* @see com.ybadoo.tutoriais.poo.Expressao#resolver()
*/
public int resolver()
{
if(this.operador == '+')
{
return this.operandoA.resolver() + this.operandoB.resolver();
}
else if(this.operador == '-')
{
return this.operandoA.resolver() - this.operandoB.resolver();
}
else if(this.operador == '*')
{
return this.operandoA.resolver() * this.operandoB.resolver();
}
else if(this.operador == '/')
{
return this.operandoA.resolver() / this.operandoB.resolver();
}
return 0;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString()
{
return "(" + this.operandoA + " " + this.operador + " " + this.operandoB + ")";
}
}
/**
* Copyright (C) 2009/2024 - Cristiano Lehrer (cristiano@ybadoo.com.br)
* Ybadoo - Solucoes em Software Livre (www.ybadoo.com.br)
*
* Permission is granted to copy, distribute and/or modify this document
* under the terms of the GNU Free Documentation License, Version 1.3
* or any later version published by the Free Software Foundation; with
* no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
* A copy of the license is included in the section entitled "GNU
* Free Documentation License".
*/
package com.ybadoo.tutoriais.poo;
/**
* Client
*/
public class Calculadora
{
/**
* Metodo principal da linguagem de programacao Java
*
* @param args argumentos da linha de comando (nao utilizado)
*/
public static void main(String[] args)
{
Operacao op1 = new Operacao(new Constante(5), '+', new Constante(7));
System.out.println(op1 + " = " + op1.resolver()); // (5 + 7) = 12
Operacao op2 = new Operacao(new Constante(3), '*', op1);
System.out.println(op2 + " = " + op2.resolver()); // (3 * (5 + 7)) = 36
Operacao op3 = new Operacao(op2, '/', new Constante(4));
System.out.println(op3 + " = " + op3.resolver()); // ((3 * (5 + 7)) / 4) = 9
}
}