REVISION: 0
OBJETIVO:
MATERIAL/EQUIPO Libreta u hojas blancas de papel bond Lpiz PC Lenguaje de Programacin, IDE Eclipse METODOLOGIA Y DESARROLLO 1. Analice las clases File, StringTokenizer, BufferedReader, PrintWriter de Java y genere una tabla con los mtodos que se emplean en la clase Text.java que se proporciona en los siguientes puntos
3. Capture, compile y ejecute el siguiente ejemplo a. Inicie Eclipse en el Menu File / New / Java Project cree el proyecto con el nombre AOD y clic en el botn Finish, se deber obtener como la imagen que aparece a la derecha
Clase j035.java
package uni2; import java.io.*; class j035 { public static void main (String [] args) throws IOException int count;
double total = 0; double number; BufferedReader in = Text.open(System.in); BufferedReader fin = Text.open("Fichero.txt"); System.out.println("****** Suma de numeros desde un fichero ******"); Text.prompt ("Cuantos numeros hay?"); count = Text.readInt(in); for (int i = 1; i <= count; i++){ number = Text.readDouble(fin); total += number; } System.out.println("Ya es suficiente, gracias."); System.out.println("El total es "+total); } }
Clase Text.java esta se debe de cargar en el mismo proyecto que la clase j035
package uni2; import java.io.*; import java.util.*; import java.text.*; class Text { public Text () {}; private static StringTokenizer T; private static String S; public static BufferedReader open (InputStream in) { return new BufferedReader(new InputStreamReader(in)); } public static BufferedReader open (String filename) throws FileNotFoundException { return new BufferedReader (new FileReader (filename)); } public static PrintWriter create (String filename) throws IOException { return new PrintWriter (new FileWriter (filename)); } public static void prompt (String s) { System.out.print(s + " "); System.out.flush(); } public static int readInt (BufferedReader in) throws IOException { if (T==null) refresh(in); while (true) { try { return Integer.parseInt(T.nextToken());
} catch (NoSuchElementException e1) { refresh (in); } catch (NumberFormatException e2) { System.out.println("Error in number, try again."); } } } public static char readChar (BufferedReader in) throws IOException { if (T==null) refresh(in); while (true) { try { return T.nextToken().trim().charAt(0); } catch (NoSuchElementException e1) { refresh (in); } } } public static double readDouble (BufferedReader in) throws IOException { if (T==null) refresh(in); while (true) { try { String item = T.nextToken(); return Double.valueOf(item.trim()).doubleValue(); } catch (NoSuchElementException e1) { refresh (in); } catch (NumberFormatException e2) { System.out.println("Error in number, try again."); } } } public static String readString (BufferedReader in) throws IOException { if (T==null) refresh (in); while (true) { try { return T.nextToken(); } catch (NoSuchElementException e1)
{ refresh (in); } } } private static void refresh (BufferedReader in) throws IOException { S = in.readLine (); if (S==null) throw new EOFException(); T = new StringTokenizer (S); } // Metodos Escritura private static DecimalFormat N = new DecimalFormat(); private static final String spaces = "
";
public static String writeDouble (double number, int align, int frac) { N.setGroupingUsed(false); N.setMaximumFractionDigits(frac); N.setMinimumFractionDigits(frac); String num = N.format(number); if (num.length() < align) num = spaces.substring(0,align-num.length()) + num; return num; } public static String writeInt (int number, int align) { N.setGroupingUsed(false); N.setMaximumFractionDigits(0); String num = N.format(number); if (num.length() < align) num = spaces.substring(0,align-num.length()) + num; return num; } }
4. Realice el reporte de la prctica utilizando la seccin de RESULTADOS incluido en el manual de prcticas. La salida deber ser similar a la siguiente imagen
CUESTIONARIO Cul es la salida del fichero j035.java? Qu mtodos de la clase Text permiten abrir el fichero texto y que tipo de dato retorna? Explica la funcionalidad de los mtodos readDouble readInt que se emplean en la clase j035? RESULTADOS
NOTAS Y SUGERENCIAS Compare sus notas con algunos conceptos buscados en la internet, as como su clasificacin BIBLIOGRAFIA