Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Nasa;
- import java.io.File;
- import javax.swing.JOptionPane;
- public class EstadisticaApp {
- public static void main(String[] args) {
- File file;
- while ( true ) {
- String fileNameString = JOptionPane.showInputDialog("Escriba el nombre del archivo con los datos",
- "C:\\Users\\841174936\\Desktop\\datos.txt");
- if ( fileNameString == null ) {
- System.exit(0);
- }
- file = new File(fileNameString);
- if( file.exists() )
- break;
- } // while
- Estadisticas datosEstadisticas = new Estadisticas( file );
- System.out.print("0. MOSTRAR DATOS: \n");
- System.out.println( datosEstadisticas.getDatos() );
- System.out.print("1. CUANTOS DATOS HAY: ");
- System.out.println( datosEstadisticas.getLength() );
- System.out.print("2. CUANTO SUMAN LOS DATOS: ");
- System.out.println( datosEstadisticas.getTotal() );
- System.out.print("3. VALOR MAYOR: ");
- System.out.println( datosEstadisticas.getMaximum() );
- System.out.print("4. VALOR MENOR: ");
- System.out.println( datosEstadisticas.getMinimum() );
- System.out.print("5. PROMEDIO DE LOS DATOS: ");
- System.out.println( datosEstadisticas.getMean() );
- // System.out.print("6. MEDIANA DE LOS DATOS: ");
- // System.out.println( datosEstadisticas.getMedian() );
- System.out.print("7. RANGE (ALCANCE): ");
- System.out.println( datosEstadisticas.getRange() );
- System.out.print("8. MID RANGE: ");
- System.out.println( datosEstadisticas.getMidRange() );
- System.out.print("9. MEDIA GEOMETRICA: ");
- System.out.println( datosEstadisticas.getGeometricMean() );
- System.out.print("10. MEDIA ARMONICA: ");
- System.out.println( datosEstadisticas.getHarmonicMean() );
- System.out.print("11. VARIANZA: ");
- System.out.println( datosEstadisticas.getVariance() );
- // System.out.print("12. DESVIACION ESTANDAR: ");
- // System.out.println( datosEstadisticas.getStdDeviation() );
- String buscarString = JOptionPane.showInputDialog("Que numero quiere buscar?", "0" );
- if( buscarString == null )
- System.exit(0);
- double buscar = Double.parseDouble( buscarString );
- System.out.println("13. BUSCAR EL VALOR\nBUSCAR " + buscar );
- int indice = datosEstadisticas.buscarValor(buscar);
- if( indice < 0 ){
- System.out.println( buscar + " no se encuentra." );
- System.exit(0);
- }// if
- else
- System.out.println( buscar + " esta en " + indice );
- System.out.println( datosEstadisticas.getDatos() );
- } // final de main()
- } // final de EstadisticasApp
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- package Nasa;
- import javax.swing.JOptionPane;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.util.Scanner;
- public class Estadisticas {
- private static final int Size = 100;
- private int cantidad;// contador
- double[] numeros = new double[Size ];
- public Estadisticas(File file) {
- try {
- Scanner scanner = new Scanner(file);
- while(scanner.hasNext() && cantidad < Size){
- double foo = scanner.nextDouble();
- this.numeros[cantidad] = foo;
- cantidad++;
- }// while
- scanner.close();
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- //e.printStackTrace();
- JOptionPane.showMessageDialog(null, "Error leyendo datos.");
- }
- readData(file);
- }// overload constructor
- private void readData(File file) {
- // TODO Auto-generated method stub
- }// readData
- public String getDatos() {
- String foo = "" ;
- for (int i = 0; i < this.cantidad; i++) {
- foo = foo + this.numeros[i] + " \n";
- }// for
- return foo ;
- }//getDatos
- public int getLength() {
- return this.cantidad;
- }//getLength
- public double getTotal() {
- double acumulador = 0;
- for (int i = 0; i < numeros.length; i++) {
- acumulador = this.numeros[i];
- }// for
- return acumulador;
- }
- public double getMaximum() {
- double mayor = this.numeros[0];
- for (int i = 1; i < numeros.length; i++) {
- if(this.numeros[i] > mayor){
- mayor = this.numeros[i];
- }//if
- }//for
- return mayor;
- }//getMaximo
- public double getMinimum() {
- double menor = this.numeros[0];
- for (int i = 1; i < numeros.length; i++) {
- if(this.numeros[i] < menor){
- menor = this.numeros[i];
- }//if
- }//for
- return menor;
- }//getMin
- public double getMean() {
- return getTotal() / this.cantidad;
- }//getMean
- public double getRange() {
- return getMaximum() - getMinimum();
- }//getMax
- public double getMidRange() {
- return (( getMaximum() + getMinimum() ) /2);
- }
- public double getGeometricMean() {
- double acumulador = 0;
- for (int i = 0; i < cantidad; i++) {
- acumulador *= this.numeros[i];
- }// for
- return Math.pow(acumulador, 1/this.cantidad);
- }//getGeoMean
- public int buscarValor(double buscar) {
- for(int i = 0; i < numeros.length; i++){
- if(numeros[i] == buscar)
- return i;
- }
- return -1;
- }// buscarValor
- public double getVariance() {
- double acum = 0;
- for (int i = 0; i < numeros.length; i++) {
- acum += ( numeros[i] - getMean() )*(numeros[i] - getMean() );
- }
- return acum/cantidad;
- }//getVariance
- public double getHarmonicMean() {
- double sum = 0.0;
- for (int i = 0; i < numeros.length; i++) {
- sum += 1.0 / numeros[i];
- }
- return numeros.length / sum;
- }//getHarmonicMean
Add Comment
Please, Sign In to add comment