EJEMPLO DE EXAMEN:
Diseño de GUI:
DATOS DEL EJERCICIO:
| Producto | Precio |
| P 0P 1
P 2 |
20.017.5
25.0 |
Segunda parte:
| Cantidad | Caramelos |
| <5>=5 y <10
>=10 |
23
2 por cada unidad |
VARIABLES:
pro, ipag, can, car;
DESARROLLO DEL PROBLEMA:
protected void actionPerformedBtnProcesar(ActionEvent arg0) {
//CÓDIGO PARA EL BOTÓN PROCESAR
//DECLARO VARIABLES LOCALES
int pro,can,car;
double ipag;
// LLAMO MIS MÉTODOS
pro=getProducto();
can=getCantidad();
ipag=calcularImportePagar(pro,can);
car=calcularCaramelos(can);
mostrarReporte(ipag,car);
}
//DETERMINO MIS MÉTODOS
//PARA METODO 1
int getProducto(){
return cboProducto.getSelectedIndex();
}
//PARA MÉTODO 2
int getCantidad(){
return Integer.parseInt(txtCantidad.getText());
}
//PARA MÉTODO 3
double calcularImportePagar(int p, int c){
switch(p){
case 0: return 20.0*c;
case 1: return 17.5*c;
default: return 25.0*c;
}
}
//PARA MÉTODO 4
int calcularCaramelos(int c){
if(c<5)
return 2;
else
if (c<10)
return 3;
else
return 2*c;
}
// PARA MÉTODO 5
void mostrarReporte (double ip, int ca)
{
txtS.setText(«Importe a pagar: «+ip+»\n»);
txtS.append(«Caramelos a recibir:» +ca+»\n»);
//ACÁ TERMINA
}
}
EJERCICIO 2:
Una tienda vende camisas a los siguientes precios:
| MARCA | PRECIO |
| Adan
Abercrombie Elegant |
S/.35.0
S/.47.5 S/.60.0 |
Como incentivo la tienda regala 2 lapiceros por cada unidad adquirida para todas las compras.
Dadas la marca y la cantidad de camisas diseñe un programa que determine el importe a pagar y los lapiceros de obsequio.
Considere todas las variables como locales y use los siguientes métodos.
actionPerformed BtnProcesar
getMarca
getCantidad
calcularImportePagar
calcularLapiceros
mostrarReporte
VARIABLES
marca (entero)
Cant (entero)
Ipag (real)
Regalo (entero)
GUI:
CÓDIGO:
protected void actionPerformedBtnProcesar(ActionEvent e) {
//MI CÓDIGO PARA EL BOTÓN PROCESAR
int marca,cant,regalo;
double ipag;
//LLAMO MIS MÉTODOS
marca=getMarca();
cant=getCantidad();
ipag=calcularImportePagar(marca,cant);
regalo=calcularLapiceros(cant);
mostrarReporte(ipag,regalo);
}
// DEFINIR MÉTODOS
//MÉTODO 1:
int getMarca{
return cboMarca.getSelectedIndex();
}
//MÉTODO 2:
int getCantidad{
return Integer.parseInt(txtCantidad.getText());
}
//MÉTODO 3:
double calcularImportePagar(int m, int c){
switch(m){
case 0:return 35.0*c;
case 1:return 47.5*0*c;
default: return 60.0*c;
}
}
//METODO 4:
int calcularLapiceros(int c){
return 2*c;
}
// MOSTRAR RESULTADOS:
void mostrarReporte(double ip, int re){
txtS.setText(«El importe a pagar es: «+ip+»\n»);
txtS.append(«Te corresponden esta cantidad de lapiceros:»+re+»\n»);
}
}

