programación en java
TRANSCRIPT
![Page 1: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/1.jpg)
IntegrantesDaniela Abaroa, Constanza Figueroa,
Josseline Montes,Bárbara Pereira y Claudia Reyes
ProfesorJuan Soza
Fecha20 de Junio de 2011
PROGRAMACIÓN EN JAVA
![Page 2: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/2.jpg)
JAVA
¿Qué es JAVA?
Lenguaje de programación
Programa de ejecución
![Page 3: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/3.jpg)
JAVA
Usos de JAVA
Sistemas rápidos y estables.
Por ejemplo: Sonda Spirit enviada por la NASA a
Marte.
Tiene un mar de proyectos “open source”.
![Page 4: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/4.jpg)
JAVA
Máquina virtual
Multiplataforma
Compilación de un programa JAVA “bytecode”.
Bytecode interpretado por JVM’s.
JVM’s ambiente multiplataforma.
JVM’s traduce los bytecodes a código nativo.
![Page 5: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/5.jpg)
JAVA
Máquina virtual
Código JAVA.java
Compiladorjavac
Bytecode.class
Ejecutarprograma
![Page 6: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/6.jpg)
JAVA
Ediciones de JAVA
1.- Java 2 Standard Edition (J2SE)
JDK O SDK
JRE
2.- Java 2 Mobile Edition (J2ME)
CLDC
CDC
3.- Java 2 Enterprise Edition (J2EE)
![Page 7: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/7.jpg)
PROBLEMAS PROPUESTOS
1.- Escribir un programa que calcule el número de billetes
de 10.000, 5.000, 1,000, así como de monedas de 500,
100, 25, 5 y 1 pesos para desglosar una cantidad, C, de
pesos (menor de 2.147.483.647), de forma que se
necesite la menor cantidad de monedas y billetes de cada
tipo.
![Page 8: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/8.jpg)
Desarrollo del ejercicio
![Page 9: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/9.jpg)
Consideraciones:
1. Calcular el monto de billetes y monedas para un
monto entregado por pantalla.
2. El monto deberá ser menor a 2.147.483.647
3. Se debe calcular para que necesite la menor cantidad
de monedas y billetes.
![Page 10: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/10.jpg)
Reflexión:
1.- El monto se deberá ir dividiendo por el valor del
billete más grande y así sucesivamente.
Ejemplo:
$26,850 pesos en billetes de $20,000
Será 26.850 / 20.000; 1 billete de $20.000
![Page 11: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/11.jpg)
Reflexión:
2.- Deberemos validar el monto ingresado a través del
código entregado por el profesor y solicitar el monto por
teclado.
![Page 12: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/12.jpg)
Reflexión:
3.- Deberemos considerar que al tratarse de dinero, los
montos deben ser mayores a 0, y además menor a
2.147.483.647 (como señala la condición).
![Page 13: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/13.jpg)
Reflexión:
4.- Deberemos considerar la posibilidad de ingresar
montos negativos o mayores al monto señalado como
condición.
![Page 14: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/14.jpg)
package ejercicio_3;
import javax.swing.JOptionPane;
public class EJERCICIO_3 {
public static void main(String[] args) {
long cantidad , a = 0, b = 0, c= 0, d = 0,e = 0, f= 0, g= 0, h= 0, i= 0, j= 0; String cc;
PROGRAMA RESUELTO
![Page 15: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/15.jpg)
Do {
cc = JOptionPane.showInputDialog("ingrese cantidad");
}
while (!validar(cc,true));
cantidad = Long.parseLong(cc);
PROGRAMA RESUELTO
![Page 16: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/16.jpg)
if (cantidad > 0 & cantidad <2147483647) {
a = cantidad / 20000;
b = (cantidad - (a* 20000))/ 10000;
c = (cantidad -(b* 10000 + a* 20000 ))/5000;
d = (cantidad -(c*5000 + b* 10000 + a* 20000))/ 1000;
e = (cantidad -(d*1000 + c*5000 + b* 10000 + a* 20000))/500;
PROGRAMA RESUELTO
![Page 17: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/17.jpg)
f = (cantidad -(e*500 + d*1000 + c*5000 + b* 10000 + a* 20000))/100;
g = (cantidad -(f*100 + e*500 + d*1000 + c*5000 + b* 10000 + a* 20000))/50;
h = (cantidad -(g* 50 + f*100 + e*500 + d*1000 + c*5000 + b* 10000 + a* 20000))/10;
i = (cantidad -(h*10 + g* 50 + f*100 + e*500 + d*1000 + c*5000 + b* 10000 + a* 20000))/5;
j = (cantidad -(i*5 + h*10 + g* 50 + f*100 + e*500 + d*1000 + c*5000 + b* 10000 + a* 20000));
PROGRAMA RESUELTO
![Page 18: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/18.jpg)
JOptionPane.showMessageDialog(null, a + " Billetes de $20.000 \n" +
b + " Billetes de $10.000 \n" +
c + " Billetes de $5.000 \n" +
d + " Billetes de $1.000 \n" +
e + " monedas de $500 \n" +
f + " monedas de $100 \n" +
g + " monedas de $50 \n" +
h + " monedas de $10 \n" +
i + " monedas de $5 \n" +
j + " monedas de $1"); }
PROGRAMA RESUELTO
![Page 19: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/19.jpg)
else if (cantidad > 2147483647)
JOptionPane.showMessageDialog (null,"Error, valor demasiado grande");
PROGRAMA RESUELTO
![Page 20: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/20.jpg)
else {
a = (-1*cantidad) / 20000;
b = ((-1*cantidad) - (a* 20000))/ 10000;
c = ((-1*cantidad) -(b* 10000 + a* 20000 ))/5000;
d = ((-1*cantidad) -(c*5000 + b* 10000 + a* 20000))/ 1000;
PROGRAMA RESUELTO
![Page 21: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/21.jpg)
e = ((-1*cantidad) -(d*1000 + c*5000 + b* 10000 + a* 20000))/500;
f = ((-1*cantidad) -(e*500 + d*1000 + c*5000 + b* 10000 + a* 20000))/100;
g = ((-1*cantidad) -(f*100 + e*500 + d*1000 + c*5000 + b* 10000 + a* 20000))/50;
h = ((-1*cantidad) -(g* 50 + f*100 + e*500 + d*1000 + c*5000 + b* 10000 + a* 20000))/10;
i = ((-1*cantidad) -(h*10 + g* 50 + f*100 + e*500 + d*1000 + c*5000 + b* 10000 + a* 20000))/5;
j = ((-1*cantidad) -(i*5 + h*10 + g* 50 + f*100 + e*500 + d*1000 + c*5000 + b* 10000 + a* 20000));
PROGRAMA RESUELTO
![Page 22: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/22.jpg)
PROGRAMA RESUELTO
JOptionPane.showMessageDialog (null, a + " Billetes de $20.000 \n"
+
b + " Billetes de $10.000 \n" +
c + " Billetes de $5.000 \n" +
d + " Billetes de $1.000 \n" +
e + " monedas de $500 \n" +
f + " monedas de $100 \n" +
g + " monedas de $50 \n" +
h + " monedas de $10 \n" +
i + " monedas de $5 \n" +
j + " monedas de $1"); }
![Page 23: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/23.jpg)
System.exit(0);
}
public static boolean validar(String cadNum,boolean entero) {
char carDigito; boolean coma = false, signo = false;
PROGRAMA RESUELTO
![Page 24: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/24.jpg)
for (int i=0; i<cadNum.length();i++) {
carDigito = cadNum.charAt(i);
if (carDigito == '.' && entero) return false;
if (i==0 && carDigito == '-') {signo = true; continue;}
if (i==0 && carDigito == '+') {signo = true; continue;}
if (signo && carDigito == '-') return false;
PROGRAMA RESUELTO
![Page 25: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/25.jpg)
if (signo && carDigito == '+') return false;
if (i==0 && (carDigito < '0' || carDigito > '9')) return false;
if (carDigito == '.' && !coma) {coma = true; continue;}
if (carDigito == '.' && coma) return false;
if (carDigito < '0' || carDigito > '9') return false;
PROGRAMA RESUELTO
![Page 26: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/26.jpg)
}
return true;
}
}
PROGRAMA RESUELTO
![Page 27: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/27.jpg)
PROBLEMAS PROPUESTOS
2.- Implementar un programa que sea capaz de calcular el
resultado de aplicar la fórmula siguiente (n i)= n! /( i! * (n-
i)!). Se lee un número por teclado que debe ser mayor o
igual que cero. Teniendo en cuenta además que n tiene
que ser necesariamente mayor que i.
![Page 28: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/28.jpg)
Desarrollo del ejercicio
![Page 29: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/29.jpg)
Consideraciones:
1.- Se leen dos números por teclados, “n” e “i”, los
cuales deben ser mayores o iguales a cero. Además, se
debe cumplir que “n” sea mayor que “i”.
![Page 30: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/30.jpg)
Consideraciones:
2.- El ejercicio planteado corresponde a la fórmula de
combinatoria.
(𝒏𝒊 )=𝒏 !/ (𝒊 !∗ (𝒏−𝒊 )!)
![Page 31: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/31.jpg)
Consideraciones:
3.- Se deberá hacer el calculo de los factoriales antes de
la aplicación de la fórmula de combinatoria.
![Page 32: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/32.jpg)
Consideraciones:
4.- Se deberá indicar al usuario cuando ingrese valores
negativos o cuando el valor de “n” sea inferior al valor de
“i”.
![Page 33: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/33.jpg)
package ejercicio_14;
import javax.swing.JOptionPane;
public class EJERCICIO_14 {
public static void main(String[] args) {
int factorial_n = 1;
int factorial_i = 1;
int n = 1 ; int a = 1;
int i = 1 ; int b = 1;
PROGRAMA RESUELTO
![Page 34: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/34.jpg)
PROGRAMA RESUELTO
String ci;
String cn; cn = JOptionPane.showInputDialog("ingrese n");
ci = JOptionPane.showInputDialog("ingrese i"); n = Integer.parseInt(cn);
i = Integer.parseInt(ci);
![Page 35: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/35.jpg)
while (a <= n) {
factorial_n = factorial_n * a;
a = a + 1;
}
PROGRAMA RESUELTO
![Page 36: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/36.jpg)
while (b <= i) {
factorial_i = factorial_i * b;
b = b + 1;
}
PROGRAMA RESUELTO
![Page 37: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/37.jpg)
int factorial = 1;
int c = n - i ;
int d = 1;
while (d <= c) {
factorial = factorial * d;
d = d + 1;
}
PROGRAMA RESUELTO
![Page 38: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/38.jpg)
if (n>i & n>0 & i>0) {
int combinatoria;
combinatoria = factorial_n/ (factorial_i * factorial);
JOptionPane.showMessageDialog (null,"Combinatoria de
("+n +" "+ i+ ") = " + combinatoria);
}
PROGRAMA RESUELTO
![Page 39: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/39.jpg)
else if (n < i)
JOptionPane.showMessageDialog (null, "ERROR: ingrese n mayor que i.");
else
JOptionPane.showMessageDialog (null,"ERROR: ingrese valores positivos para n e i.");
System.exit(0);
}
}
PROGRAMA RESUELTO
![Page 40: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/40.jpg)
PROBLEMAS PROPUESTOS
3.- Preparar un programa que permita recibir una frase :
- Indicar la cantidad de palabras que tiene.
- Reversar la frase: (Hola Mundo odnuM aloH)
- Indicar la cantidad total de caracteres que tiene
- Indicar la letra que más se repite indicando la cantidad de
veces que se repite.
![Page 41: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/41.jpg)
Desarrollo del ejercicio
![Page 42: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/42.jpg)
Consideraciones:
1.- Debemos considerar ingresar una frase por teclado,
independiente de la extensión que tenga.
![Page 43: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/43.jpg)
Consideraciones:
2.- Debemos revertir la frase, indicar la cantidad de
letras o caracteres que posee e indicar la letra que más
se repite, señalando el número de veces que se repite.
![Page 44: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/44.jpg)
package ejercicio_25;
import javax.swing.JOptionPane;
public class EJERCICIO_25 {
public static void main(String[] args) {
String cad = new String("");
char blanco = ' ';
int cuantos = 0;
String cadInvertida = "";
PROGRAMA RESUELTO
![Page 45: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/45.jpg)
//BufferedReader entrada; string buffer
//entrada = new BufferedReader (new InputStreamReader(System.in));
PROGRAMA RESUELTO
![Page 46: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/46.jpg)
System.out.print("deme la cadena ");
cad = JOptionPane.showInputDialog("Ingrese frase");
cuantos = contar(cad);
PROGRAMA RESUELTO
![Page 47: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/47.jpg)
for (int x = cad.length () -1 ; x >= 0 ; x--)
cadInvertida = cadInvertida + cad.charAt(x);
JOptionPane.showMessageDialog(null,"* Habia "+ cuantos + " palabras en la frase \n" + "* La frase invertida : " + cadInvertida);
}
PROGRAMA RESUELTO
![Page 48: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/48.jpg)
public static int contar(String st){
char a;
int n = 1; // para contar la primera palabra
int i = st.indexOf(" ");
while (i>0) {
n++;
st = st.substring(i+1);
i = st.indexOf(" ");
}
PROGRAMA RESUELTO
![Page 49: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/49.jpg)
return n++;
}
}
PROGRAMA RESUELTO
![Page 50: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/50.jpg)
CONCLUSIÓN
JAVA Multiplataforma
Diversas aplicaciones.
Desde aplicaciones cotidianas hasta sistemas que
viajen al espacio.
De gran utilidad en nuestros estudios.
![Page 51: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/51.jpg)
CONCLUSIÓN
Ejercicio 3
Se puede aplicar a distintos cajeros.
Cajeros automáticos.
Aplicaciones en matrices.
![Page 52: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/52.jpg)
CONCLUSIÓN
Ejercicio 14
Fórmulas de ingeniería.
Resolución de problemas en forma rápida.
Evitar cálculos extensos.
![Page 53: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/53.jpg)
CONCLUSIÓN
Ejercicio 25
Contar los caracteres, como es el caso de los
“wall post” en facebook.
Como herramienta de redacción.
En blogs y twitter entre otros.
![Page 54: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/54.jpg)
CONCLUSIÓN
![Page 55: Programación en JAVA](https://reader033.vdocumento.com/reader033/viewer/2022050801/55015b6f4a7959ac638b4ec9/html5/thumbnails/55.jpg)
FIN