Bom dia pessoal, meu nome é Júlio, estou iniciando nesta area da programação, sempre trabalhei com hardware, e agora estou mudando meu foco, bom na aula o professor já explicou e pediu para fazermos uma calculadora web.
Bem não estou aqui para pedir uma pronta, já fiz a minha porem estou com dificuldades com a logica para receber o segundo valor a ser calculado e tambem estou em duvida se estou usando herança de forma correta (extends), será que alguem ai mais experiente tem um tempo para ler meu codigo fonte e me dar uma luz?
Obrigado.
Segue os codigos:
index.jsp<%--
Document : index
Created on : 16/03/2011, 19:35:03
Author : Julio
--%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Calculadora</title>
<script language="javascript" type="text/javascript">
function adiciona(valor) {
display = document.getElementById("display");
valorAtual = display.value;
display.value = valorAtual + valor;
}
</script>
</head>
<body>
<form action="/calculadora2/calcSv" name="form1" method="post">
<table border="1" >
<td><input id="display" size="16" name="display" value="${display}" type="text"><br /></td>
<input id="valor_antigo" size="16" name="valor_antigo" value="${valor_antigo}" type="hidden">
<table border="1">
<tr><td><input onclick="adiciona('7');" type="button" value="7" /></td><td><input onclick="adiciona('8');" type="button" value="8" /></td><td><input type="button" onclick="adiciona('9');" value="9" /></td><td><input type="submit" name="operacao" value="/" /></td></tr>
<tr><td><input onclick="adiciona('4');" type="button" value="4" /></td><td><input onclick="adiciona('5');" type="button" value="5" /></td><td><input type="button" onclick="adiciona('6');" value="6" /></td><td><input type="submit" name="operacao" value="*" /></td></tr>
<tr><td><input onclick="adiciona('1');" type="button" value="1" /></td><td><input onclick="adiciona('2');" type="button" value="2" /></td><td><input type="button" onclick="adiciona('3');" value="3" /></td><td><input type="submit" name="operacao" value="-" /></td></tr>
<tr><td ><input type="submit" style="background-color:#FF0000" name="operacao" value="C" /></td><td><input onclick="adiciona('0');" type="button" value="0" /></td><td><input type="submit" name="operacao" value="+" /></td><td><input type="submit" name="igual" value="=" /></td></tr>
</table>
</table>
</form>
</body>
</html>
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package controle;
import Modelo.calcVo;
import Modelo.divideDao;
import Modelo.multiplicaDao;
import Modelo.somaDao;
import Modelo.subtraiDao;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author Julio
*/
@WebServlet(name = "calcSv", urlPatterns = {"/calcSv"})
public class calcSv extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
RequestDispatcher view = request.getRequestDispatcher("index.jsp");/*envia para o jsp*/
String valor = request.getParameter("display");
String valor_antigo = request.getParameter("valor_antigo");
int numero1 = 0;
int numero2 = 0;
if(valor_antigo != null && valor_antigo != "") {
numero1 = Integer.parseInt(valor_antigo);
}
if(valor != null && valor != "") {
numero2 = Integer.parseInt(valor);
}
calcVo calc = new calcVo();
calc.setValor1(numero1);
calc.setValor2(numero2);
String opcao = request.getParameter("operacao");
String op = request.getParameter("igual");
somaDao soma = new somaDao();
subtraiDao subtrai = new subtraiDao();
multiplicaDao multiplica = new multiplicaDao();
divideDao divide = new divideDao();
int resultado = 0;
if (calc.getValor1() != 0 || calc.getValor2() != 0) {
if ("+".equals(opcao)) {
resultado = soma.soma();
}
if ("-".equals(opcao)) {
resultado = subtrai.subtrai();
}
if ("/".equals(opcao)) {
resultado = divide.divide();
}
if ("*".equals(opcao)) {
resultado = multiplica.multiplica();
}
if ("=".equals(op)){
}
}
request.setAttribute("display", resultado);
view.forward(request, response);
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Modelo;
/**
*
* @author Julio
*/
public class calcVo {
int valor1;
int valor2;
int resultado;
/**
* @return the valor1
*/
public int getValor1() {
return valor1;
}
/**
* @param valor1 the valor1 to set
*/
public void setValor1(int valor1) {
this.valor1 = valor1;
}
/**
* @return the valor2
*/
public int getValor2() {
return valor2;
}
/**
* @param valor2 the valor2 to set
*/
public void setValor2(int valor2) {
this.valor2 = valor2;
}
/**
* @return the resultado
*/
public int getResultado() {
return resultado;
}
/**
* @param resultado the resultado to set
*/
public void setResultado(int resultado) {
this.resultado = resultado;
}
}
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Modelo;
/**
*
* @author Julio
*/
public class somaDao extends calcVo{
public int soma() {
resultado = (valor1 + valor2);
return resultado;
}
}
E ainda tem as classes subtrai, divide e multiplica, porem sao iguais a soma, mudando apenas o sinal.