Guest User

Untitled

a guest
Apr 27th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.20 KB | None | 0 0
  1. package Nasa;
  2.  
  3. import java.io.File;
  4.  
  5. import javax.swing.JOptionPane;
  6.  
  7. public class EstadisticaApp {
  8.  
  9.  
  10.  
  11.  
  12.  
  13. public static void main(String[] args) {
  14.  
  15. File file;
  16.  
  17. while ( true ) {
  18. String fileNameString = JOptionPane.showInputDialog("Escriba el nombre del archivo con los datos",
  19. "C:\\Users\\841174936\\Desktop\\datos.txt");
  20.  
  21. if ( fileNameString == null ) {
  22. System.exit(0);
  23. }
  24.  
  25. file = new File(fileNameString);
  26.  
  27. if( file.exists() )
  28. break;
  29. } // while
  30.  
  31. Estadisticas datosEstadisticas = new Estadisticas( file );
  32.  
  33. System.out.print("0. MOSTRAR DATOS: \n");
  34. System.out.println( datosEstadisticas.getDatos() );
  35. System.out.print("1. CUANTOS DATOS HAY: ");
  36. System.out.println( datosEstadisticas.getLength() );
  37. System.out.print("2. CUANTO SUMAN LOS DATOS: ");
  38. System.out.println( datosEstadisticas.getTotal() );
  39. System.out.print("3. VALOR MAYOR: ");
  40. System.out.println( datosEstadisticas.getMaximum() );
  41. System.out.print("4. VALOR MENOR: ");
  42. System.out.println( datosEstadisticas.getMinimum() );
  43. System.out.print("5. PROMEDIO DE LOS DATOS: ");
  44. System.out.println( datosEstadisticas.getMean() );
  45. // System.out.print("6. MEDIANA DE LOS DATOS: ");
  46. // System.out.println( datosEstadisticas.getMedian() );
  47. System.out.print("7. RANGE (ALCANCE): ");
  48. System.out.println( datosEstadisticas.getRange() );
  49. System.out.print("8. MID RANGE: ");
  50. System.out.println( datosEstadisticas.getMidRange() );
  51. System.out.print("9. MEDIA GEOMETRICA: ");
  52. System.out.println( datosEstadisticas.getGeometricMean() );
  53. System.out.print("10. MEDIA ARMONICA: ");
  54. System.out.println( datosEstadisticas.getHarmonicMean() );
  55. System.out.print("11. VARIANZA: ");
  56. System.out.println( datosEstadisticas.getVariance() );
  57. // System.out.print("12. DESVIACION ESTANDAR: ");
  58. // System.out.println( datosEstadisticas.getStdDeviation() );
  59.  
  60. String buscarString = JOptionPane.showInputDialog("Que numero quiere buscar?", "0" );
  61.  
  62. if( buscarString == null )
  63. System.exit(0);
  64.  
  65. double buscar = Double.parseDouble( buscarString );
  66.  
  67. System.out.println("13. BUSCAR EL VALOR\nBUSCAR " + buscar );
  68. int indice = datosEstadisticas.buscarValor(buscar);
  69.  
  70. if( indice < 0 ){
  71. System.out.println( buscar + " no se encuentra." );
  72. System.exit(0);
  73. }// if
  74. else
  75. System.out.println( buscar + " esta en " + indice );
  76.  
  77. System.out.println( datosEstadisticas.getDatos() );
  78. } // final de main()
  79.  
  80. } // final de EstadisticasApp
  81.  
  82. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  83.  
  84. package Nasa;
  85. import javax.swing.JOptionPane;
  86. import java.io.File;
  87. import java.io.FileNotFoundException;
  88. import java.util.Scanner;
  89.  
  90. public class Estadisticas {
  91.  
  92.  
  93. private static final int Size = 100;
  94. private int cantidad;// contador
  95.  
  96.  
  97.  
  98. double[] numeros = new double[Size ];
  99.  
  100.  
  101. public Estadisticas(File file) {
  102.  
  103. try {
  104. Scanner scanner = new Scanner(file);
  105.  
  106. while(scanner.hasNext() && cantidad < Size){
  107.  
  108. double foo = scanner.nextDouble();
  109. this.numeros[cantidad] = foo;
  110. cantidad++;
  111.  
  112.  
  113. }// while
  114.  
  115. scanner.close();
  116.  
  117. } catch (FileNotFoundException e) {
  118. // TODO Auto-generated catch block
  119. //e.printStackTrace();
  120.  
  121. JOptionPane.showMessageDialog(null, "Error leyendo datos.");
  122. }
  123.  
  124.  
  125. readData(file);
  126.  
  127.  
  128.  
  129. }// overload constructor
  130.  
  131. private void readData(File file) {
  132. // TODO Auto-generated method stub
  133.  
  134. }// readData
  135.  
  136. public String getDatos() {
  137.  
  138.  
  139. String foo = "" ;
  140.  
  141. for (int i = 0; i < this.cantidad; i++) {
  142.  
  143.  
  144. foo = foo + this.numeros[i] + " \n";
  145.  
  146. }// for
  147.  
  148. return foo ;
  149. }//getDatos
  150.  
  151. public int getLength() {
  152.  
  153. return this.cantidad;
  154. }//getLength
  155.  
  156. public double getTotal() {
  157. double acumulador = 0;
  158.  
  159. for (int i = 0; i < numeros.length; i++) {
  160.  
  161. acumulador = this.numeros[i];
  162. }// for
  163.  
  164.  
  165.  
  166. return acumulador;
  167. }
  168.  
  169. public double getMaximum() {
  170. double mayor = this.numeros[0];
  171.  
  172. for (int i = 1; i < numeros.length; i++) {
  173.  
  174. if(this.numeros[i] > mayor){
  175.  
  176. mayor = this.numeros[i];
  177. }//if
  178.  
  179. }//for
  180. return mayor;
  181. }//getMaximo
  182.  
  183. public double getMinimum() {
  184. double menor = this.numeros[0];
  185.  
  186.  
  187. for (int i = 1; i < numeros.length; i++) {
  188.  
  189. if(this.numeros[i] < menor){
  190.  
  191. menor = this.numeros[i];
  192. }//if
  193.  
  194. }//for
  195.  
  196.  
  197. return menor;
  198. }//getMin
  199.  
  200. public double getMean() {
  201.  
  202.  
  203. return getTotal() / this.cantidad;
  204. }//getMean
  205.  
  206. public double getRange() {
  207.  
  208.  
  209. return getMaximum() - getMinimum();
  210. }//getMax
  211.  
  212. public double getMidRange() {
  213.  
  214. return (( getMaximum() + getMinimum() ) /2);
  215. }
  216.  
  217. public double getGeometricMean() {
  218.  
  219. double acumulador = 0;
  220.  
  221. for (int i = 0; i < cantidad; i++) {
  222. acumulador *= this.numeros[i];
  223.  
  224. }// for
  225.  
  226.  
  227. return Math.pow(acumulador, 1/this.cantidad);
  228. }//getGeoMean
  229.  
  230. public int buscarValor(double buscar) {
  231.  
  232. for(int i = 0; i < numeros.length; i++){
  233. if(numeros[i] == buscar)
  234. return i;
  235. }
  236.  
  237. return -1;
  238. }// buscarValor
  239.  
  240. public double getVariance() {
  241.  
  242.  
  243. double acum = 0;
  244. for (int i = 0; i < numeros.length; i++) {
  245. acum += ( numeros[i] - getMean() )*(numeros[i] - getMean() );
  246. }
  247.  
  248. return acum/cantidad;
  249.  
  250.  
  251. }//getVariance
  252.  
  253. public double getHarmonicMean() {
  254.  
  255. double sum = 0.0;
  256.  
  257. for (int i = 0; i < numeros.length; i++) {
  258. sum += 1.0 / numeros[i];
  259. }
  260. return numeros.length / sum;
  261. }//getHarmonicMean
Add Comment
Please, Sign In to add comment