Está en la página 1de 8

import java.io.

*; class LeeFichero { public static void main(String [] arg) { File archivo = null; FileReader fr = null; BufferedReader br = null; try { // Apertura del fichero y creacion de BufferedReader para poder // hacer una lectura comoda (disponer del metodo readLine()). archivo = new File ("C:\\archivo.txt"); fr = new FileReader (archivo); br = new BufferedReader(fr); // Lectura del fichero String linea; while((linea=br.readLine())!=null) System.out.println(linea); } catch(Exception e){ e.printStackTrace(); }finally{ // En el finally cerramos el fichero, para asegurarnos // que se cierra tanto si todo va bien como si salta // una excepcion. try{ if( null != fr ){ fr.close(); } }catch (Exception e2){ e2.printStackTrace(); } } } }
FileReader fr; try { fr = new FileReader("administrativos.txt"); BufferedReader bf = new BufferedReader(fr); String sCadena; while ((sCadena = bf.readLine())!=null) { admin.add(sCadena); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }

// ejecutar .bat Runtime aplicacion = Runtime.getRuntime(); try{aplicacion.exec("cmd.exe /K agregar.bat"); } catch(Exception e){System.out.println(e);} //-----------------------------------------

public class Alcancia { private int m20, m50, m100, m200, m500; public Alcancia() { super(); m20 = 0; m50 = 0; m100 = 0; m200 = 0; m500 = 0; } public void agregarMoneda(int denominacion) { switch (denominacion) { case 20: this.m20=m20+1; break; case 50: this.m50=m50+1; break; case 100: this.m100=m100+1; break; case 200: this.m200=m200+1; break; case 500: this.m500=m500+1; break; default: System.out.println("Denominacin de moneda inexistente"); break;

} } public int contarMonedas(int denominacion) { switch (denominacion) { case 20: return this.m20; case 50: return this.m50; case 100: return this.m100; case 200: return this.m200; case 500: return this.m500; default: return 0; } } public String getTotalAhorro() { int total = 0; total += this.m20 * 20; total += this.m50 * 50; total += this.m100 * 100; total += this.m200 * 200; total += this.m500 * 500; return ("la cantidad total es: " +total +" pesos"); } public void vaciarAlcancia() { this.m20 = 0; this.m100 = 0; this.m50 = 0; this.m200 = 0; this.m500 = 0; } }

// La clase StringTokenizer. import java.util.*; import java.awt.*; import java.awt.event.*;

import javax.swing.*; public class Palabras extends JFrame { private JLabel indicadorEtiqueta; private JTextField entradaCampo; private JTextArea areaSalida;

// configurar GUI y manejo de eventos public Palabras() { super( "Prueba de la clase StringTokenizer" ); Container contenedor = getContentPane(); contenedor.setLayout( new FlowLayout() ); indicadorEtiqueta = new JLabel( "Escriba una oracin y oprima Intro" ); contenedor.add( indicadorEtiqueta ); entradaCampo = new JTextField( 20 ); entradaCampo.addActionListener( new ActionListener() { // clase interna annima // manejar evento de campo de texto public void actionPerformed( ActionEvent evento ) { StringTokenizer tokens = new StringTokenizer( evento.getActionCommand() ); areaSalida.setText( "Nmero de elementos: " + tokens.countTokens() + "\nLos tokens son:\n" ); while ( tokens.hasMoreTokens() ) areaSalida.append( tokens.nextToken() + "\n" );

} } // fin de clase interna annima ); // fin de la llamada a addActionListener

contenedor.add( entradaCampo ); areaSalida = new JTextArea( 10, 20 ); areaSalida.setEditable( false ); contenedor.add( new JScrollPane( areaSalida ) ); setSize( 275, 240 ); // establecer el tamao de la ventana setVisible( true ); // mostrar la ventana } // ejecutar la aplicacin public static void main( String args[] ) { Palabras aplicacion = new Palabras(); aplicacion.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); } } // fin de la clase PruebaToken

package com.gpt;

import javax.swing.JOptionPane; /* This program computes Fibonacci numbers using a recursive method. */ public class Fibonacci { public static void main(String[] args) { String input = JOptionPane.showInputDialog("Enter n: "); int n = Integer.parseInt(input); for (int i = 1; i <= n; i++) { int f = fib(i); System.out.println("fib(" + i + ") = " + f); } System.exit(0); } /**

Computes a Fibonacci number. @param n an integer @return the nth Fibonacci number */ public static int fib(int n) { if (n <= 2) return 1; else return fib(n - 1) + fib(n - 2); } } import java.util.*; public class Dado { public static void main(String[] args) { int posibilidad[]= new int[11]; for(int p=0;p<11;p++) { posibilidad[p]=0; }

for(int i=0;i<10000;i++){ Random aleatori = new Random(); int resul = 2+aleatori.nextInt(11); switch (resul){ case 2: posibilidad[0]++; break; case 3: posibilidad[1]++; break; case 4: posibilidad[2]++; break; case 5: posibilidad[3]++; break; case 6: posibilidad[4]++; break; case 7: posibilidad[5]++; break;

case 8: posibilidad[6]++; break; case 9: posibilidad[7]++; break; case 10: posibilidad[8]++; break; case 11: posibilidad[9]++; break; case 12: posibilidad[10]++; break; } } for(int j=2;j<11;j++){ System.out.print(j+"="+"\t" + posibilidad[j] + "\n"); } } } import javax.swing.JOptionPane; public class notas { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String tamVec = JOptionPane .showInputDialog("Digite la cantidad de alumnos:"); int tanVec = Integer.parseInt(tamVec); double alumno[] = new double[tanVec]; int cont = 0, in = 0, ac = 0, sa = 0, ex = 0, sum = 0; double nota = 0.0, men = 5.0, may = 0.0; for (int i = 0; i < tanVec; i++) { cont++; do { String Nota = JOptionPane .showInputDialog("Digite la nota del alumno " + cont

+ ":"); nota = Double.parseDouble(Nota); } while (nota < 0.0 || nota > 5.0); alumno[i] = nota; if (nota >= 0.0 && nota <= 1.9) { in++; if (nota < men) men = nota; } else if (nota >= 2.0 && nota <= 2.9) ac++; else if (nota >= 3.0 && nota <= 3.9) sa++; else if (nota >= 4.0 && nota <= 5.0) { ex++; if (nota > may) may = nota; } sum += nota; } double prom = sum / tanVec; System.out.print("Insuficiente: " + in + " alumnos.\n"); System.out.print("Aceptable: " + ac + " alumnos.\n"); System.out.print("Satisfactorio: " + sa + " alumnos.\n"); System.out.print("Excelente: " + ex + " alumnos.\n"); System.out.print("\nPromedio del curso: " + prom + ".\n"); System.out.print("La notas mas alta: " + may + ".\nLa nota mas baja: " + men + ".\n"); if (prom >= 3.5) System.out.print("Curso aprobado.\n"); else System.out.print("Se debe repetir.\n"); } }

También podría gustarte