Monday, June 27, 2005

A) Explique el siguiente error:

char c = "A"; /* define and inicialize c */

La variable c es un char por lo tanto se le asigna con 'A' y no con "A".

D) Lee un archivo que contenga los datos de un estudiante

/*
* Created on 06/25/2005
*
* Lee un archivo que contenga los datos de un estudiante,
* nombre(1-25), apellido(26-50), telefono(51-62), matricula(63-72)
* y que permita ordenar y buscar por un registro en particular
*/
package estructura.examen;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Comparator;
import java.util.Vector;

/**
* @author vns
*
*/
public class ArchivoEst {

private LinkedList datalist;

/**
*
*/
public ArchivoEst() {
super();
}

/**
* @param dataArray
*/
public ArchivoEst(LinkedList data) {
this.datalist = data;
}

/**
* @return Returns the datalist.
*/
public LinkedList getDatalist() {
return datalist;
}
/**
* @param datalist The datalist to set.
*/
public void setDatalist(LinkedList datalist) {
this.datalist = datalist;
}

/**
* Devuelve null en caso de error en la lectura
*
* @param string
* @return ArchivoEst
*/
public static ArchivoEst ReadFile(String file) {
BufferedReader fin = null;
LinkedList data = new LinkedList();
String line = null;
ArchivoEst archivo = null;

try {
fin = new BufferedReader( new FileReader(file) );

while ( (line = fin.readLine()) != null)
data.insertAtBack( new Estudiante(
line.substring(0,24),line.substring(25,49),
line.substring(50,61),line.substring(62,71) )
);
archivo = new ArchivoEst(data);
} catch (FileNotFoundException e) {
System.err.println("Archivo no encontrado");
e.printStackTrace();

} catch (IOException e) {
System.err.println("Error en la lectura");
e.printStackTrace();

} catch (Exception e) {
System.err.println("Error general");
e.printStackTrace();

}finally{
try {
fin.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return archivo;
}

/**
* @param sample
* @param Field
*/
public void sort(Object sample, String Field) {
datalist.sort(sample, Field);
}

/**
* @param field
* @return
*/
public Object[] find(String field) {
return datalist.find(field);
}
/**
* @return
*/
public boolean isEmpty() {
return datalist.isEmpty();
}

/**
*
*/
public void print() {
Nodo nodo = datalist.getFirst();

if (!datalist.isEmpty())
while (nodo != null){
((Estudiante)nodo.getData()).toString();

nodo = nodo.getNext();
}
}

}
class Estudiante {
private String nombre;
private String apellido;
private int telefono;
private int matricula;

/**
*
*/
public Estudiante() {
}


/**
* @param nombre
* @param apellido
* @param telefono
* @param matricula
*/
public Estudiante(String nombre, String apellido, String telefono,
String matricula) {
super();
this.nombre = nombre;
this.apellido = apellido;
this.telefono = Integer.parseInt(telefono);
this.matricula = Integer.parseInt(matricula);
}

/**
* @return Returns the apellido.
*/
public String getApellido() {
return apellido;
}
/**
* @param apellido The apellido to set.
*/
public void setApellido(String apellido) {
this.apellido = apellido;
}
/**
* @return Returns the matricula.
*/
public int getMatricula() {
return matricula;
}
/**
* @param matricula The matricula to set.
*/
public void setMatricula(int matricula) {
this.matricula = matricula;
}
/**
* @return Returns the nombre.
*/
public String getNombre() {
return nombre;
}
/**
* @param nombre The nombre to set.
*/
public void setNombre(String nombre) {
this.nombre = nombre;
}
/**
* @return Returns the telefono.
*/
public int getTelefono() {
return telefono;
}
/**
* @param telefono The telefono to set.
*/
public void setTelefono(int telefono) {
this.telefono = telefono;
}
/*
* Display the persona
*/
public String toString() {

return nombre + " " + apellido + " " + telefono + " " + matricula;
}

}
class Nodo{

private Object data;
private Nodo next;

/**
* @param object
*/
public Nodo(Object object) {
this(object, null);
}

/**
* @param data
* @param next
*/
public Nodo(Object data, Nodo next) {
super();
this.data = data;
this.next = next;
}

/**
* @return Returns the data.
*/
public Object getData() {
return data;
}
/**
* @param data The data to set.
*/
public void setData(Object data) {
this.data = data;
}
/**
* @return Returns the next.
*/
public Nodo getNext() {
return next;
}
/**
* @param next The next to set.
*/
public void setNext(Nodo next) {
this.next = next;
}
}
class LinkedList{
private Nodo head = null;
private Nodo tail = null;

public boolean isEmpty(){
return head == null;
}

public Nodo getFirst() {
return head;
}

public void insertAtFront(Object nodo){
if ( isEmpty() ) {
head = tail = new Nodo(nodo, null);
} else {
head = new Nodo(nodo, head);
}
}

public void insertAtBack(Object nodo){
if ( isEmpty() ) {
head = tail = new Nodo(nodo, null);
} else {
tail.setNext( new Nodo(nodo, null) );
tail = tail.getNext();
}
}

//Ordena por un campo especifico
public void sort(Object sample, String field){
FieldComparator fc = new FieldComparator(sample, field);
Nodo nodo = head;

if (isEmpty()) {
return;
}

if (fc.isValid())
while ( nodo != null ) {
if ( ( fc.compare(nodo, nodo.getNext()) ) <= -1 ) {
swap(nodo, nodo.getNext());
}

nodo = nodo.getNext();
}

}

/**
* @param nodo
* @param next
*/
private void swap(Nodo nodo, Nodo next) {
Object temp = nodo.getData();
nodo.setData(next.getData());
next.setData(temp);
}

//Busca segun un campo especifico
/**
*
* @return Object[]. Return null when is empty or is no valid the field
*/
public Object[] find(String field){
Object[] objects = null;
Nodo nodo = head;
FieldComparator fc = new FieldComparator(new Estudiante(), field);
int i = 0;

if (isEmpty()) {
return null;
}

if( fc.isValid() )
while ( nodo != null ) {

if ( (fc.compare(nodo, field) ) == 0 ) {
objects[i] = nodo.getData();
}
i++;
nodo = nodo.getNext();
}

return objects;
}

/*
*
*/
public void print() {
// TODO Auto-generated method stub
}
}

class FieldComparator implements Comparator{

private Class c;
private Field[] fields;
private Vector fieldVec;
private String sortBy;
boolean fieldMatch = true;

/**
*
*/
public FieldComparator() {
super();
}
/**
* @param sample
* @param field
*/
public FieldComparator(Object sample, String field) {
this.c = sample.getClass();
this.sortBy = field;
fieldVec = new Vector();
fields = c.getFields();

for (int i = 0; i < fields.length; i++) {
fieldVec.add( fields[i].getName() );
}

fieldMatch = fieldVec.contains( getSortBy() );
}

/**
* @return Returns the sortBy.
*/
public String getSortBy() {
return sortBy;
}
/**
* @param sortBy The sortBy to set.
*/
public void setSortBy(String sortBy) {
this.sortBy = sortBy;
}

public boolean isValid(){
return fieldMatch;
}

/**
* You should set the field to compared, set SortBY or in the constructor
*
* @param the objects to compared, arg0 and arg1
*
* @return the value 0 if the argument Object is equal to this Object;
* a value less than 0 if this Object is lexicographically less than the Object argument;
* and a value greater than 0 if this Object is lexicographically greater than the Object argument.
*/
public int compare(Object arg0, Object arg1) {
String field0 = getfield(arg0, getSortBy() );
String field1 = getfield(arg1, getSortBy() );

return field0.compareTo( field1 );
}

public int compare(Object arg0, String field) {
String field0 = getfield(arg0, getSortBy() );

return field0.compareTo( field );
}

/**
* @param arg0
* @param sortBy2
* @return
*/
private String getfield(Object arg0, String sortBy) {
String data = null;

try {

Field field = c.getField( sortBy );
data = (String)field.get( arg0 );

} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
System.err.println("Campo no encontrado");
e.printStackTrace();
} catch (SecurityException e) {
System.err.println("Error en seguridad");
e.printStackTrace();
}

return data;
}

}

Sunday, June 26, 2005

H) Convierte de números Arábicos a Romanos

/*
* Created on 06/25/2005
*
* H) Convierte de números Arábicos a Romanos
*/
package estructura.examen;

/**
* @author vns
*
*/
public class Romanos {

/**
*
*/
public Romanos() {
super();
}

/**
* @param num
* @return
*/
public static String convertirR2A(int num) {
int[] arabicos = {1,4,5,9,10,40,50,90,100,400,500,900,1000};
String[] romanos = {"I","IV","V","IX","X","XL",
"L","XC","C","CD","D","CM","M"};
StringBuffer sb = new StringBuffer();

for(int i = 12; i > = 0 ;i--)
while( num > = arabicos[i] ){
num -= arabicos[i];
sb.append(romanos[i]);
}
return sb.toString();
}

}