Archivo de la etiqueta: Programación

Ejercicios de aplicación de métodos con valor de retorno en Java

EJEMPLO DE EXAMEN:

Diseño de GUI:

GUI1

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»);
}

}

Ejercicios de Java en Eclipse Luna. Vol II

EJERCICIO: «Programa Golosina»

Una tienda ha puesto en oferta la venta de galletas en packs de 12 unidades a los precios dados en la siguiente tabla:

Golosina Precio del pack
Cream Cracker S/. 9.0
Chomp S/. 7.4
Pícaras S/. 8.6
Doña Pepa S/. 10.6

Como oferta, la tienda ofrece un porcentaje de descuento sobre el importe de la compra de acuerdo a la siguiente tabla:

Cantidad de packs Descuento
< 5 3%
≥ 5 y < 10 7%
≥ 10 y < 15 11%
≥ 15 15%

Adicionalmente, la tienda obsequia caramelos de acuerdo a la siguiente tabla:

Importe a pagar Caramelos
≥ 150 3 por cada pack
< 150 2 por cada pack

Diseñe un programa que determine el importe de la compra, el importe del descuento, el importe a pagar y la cantidad de caramelos de obsequio conociendo el tipo de golosina y la cantidad de packs de una compra.

 

RESOLUCIÓN:

Seudocódigo Código JAVA
Inicio//Declaracion de variablesEntero marcagolosina,  cant, regaloReal ic,id,ip,//Entrada de datos 

Leer marcagolosina,cant

 

//Inicio del Switch Calculo del IC

Cuando(marcagolosina)

{

case 0:ic=cant*9.0;terminar

caseo1:ic=cant*7.4;terminar

case2:ic=cant*8.6;terminar

default:ic=cant*10.6

}

Fin

 

INICIO DEL IF ENCADENADO (Calculo del ID)

si (cant<5)

{

Id=0.03*ic;

}

sino (cant>=5)

{

si (cant<10)

{

Id=0.07*ic;

}

sino(cant>=10)

{

si(cant<15)

{

Id=0.11*ic

}

sino(cant>=15)

{

Id=0.15*ic

}

}

}

 

CÓDIGO PARA EL INPUESTO A PAGAR

ip=ic-id

si (ic>=150)

{

regalo=3*cant

}

sino

{

regalo=2*cant

}

 

 

Imprimir ic,id,ip

FIN

 

//BOTON PROCESAR//DEFINIR VARIABLESint marcagolosina,cant,regalo;double ic,id,ip;//ENTRADA DE DATOS

marcagolosina=cboGolosinas.getSelectedIndex();

cant=Integer.parseInt(txtCantidad.getText());

//SWITCH

switch(marcagolosina){

case 0:ic=cant*9.0;break;

case 1:ic=cant*7.4;break;

case 2:ic=cant*8.6;break;

default:ic=cant*10.6;

}
INICIO DEL IF ENCADENADO (Calculo del ID)

//INICIO DEL IF ENCADENADO

if(cant<5)

{

id=0.03*ic;

}

else

{

if(cant<10)

{

id=0.07*ic;

}

else

{

if(cant<15)

{

id=0.11*ic;

}

else

{

id=0.15*ic;

}

}

}

//CODIGO FINAL

ip=ic-id;

if(ic>=150)

{

regalo=3*cant;

}

else

{

regalo=2*cant;

}

CÓDIGO PARA EL INPUESTO A PAGAR

ip=ic-id;

if(ic>=150)

{

regalo=3*cant;

}

else

{

regalo=2*cant;

}

 

//SALIDA DE DATOS

txtS.setText(«»);

txtS.append(«Impuesto de compra: «+ic+»\n»);

txtS.append(«Impuesto del descuento: «+id+»\n»);

txtS.append(«Impuesto a pagar: «+ip+»\n»);

}

}