Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. public class EtapaDeCompetencia implements Comparable<EtapaDeCompetencia> {
  2. private static final int SEG_EN_HORA = 3600;
  3. private static final int SEG_EN_MIN = 60;
  4. private static final int SEG_EN_SEG = 1;
  5. private final int hora,min,seg;
  6.  
  7. public EtapaDeCompetencia(int h){
  8. this(h,0,0);
  9. }
  10.  
  11. public EtapaDeCompetencia(int h, int m){
  12. this(h,m,0);
  13. }
  14.  
  15. public EtapaDeCompetencia(int h, int m, int s){
  16. hora = h;
  17. min = m;
  18. seg = s;
  19. }
  20.  
  21. private int convTiempoASegundos(int tiempo,int segundosEnTiempo){
  22. return tiempo * segundosEnTiempo;
  23. }
  24.  
  25. public int horaEnsegundos(){
  26. return convTiempoASegundos(hora,SEG_EN_HORA);
  27. }
  28.  
  29. public int minEnSegundos(){
  30. return convTiempoASegundos(min,SEG_EN_MIN);
  31. }
  32.  
  33. public int segundosEnTiempo(){
  34. return seg;
  35. }
  36.  
  37. public int compareTo(EtapaDeCompetencia e){
  38. return getTiempoDeEtapaEnSegundos() > e.getTiempoDeEtapaEnSegundos()? 1:
  39. getTiempoDeEtapaEnSegundos() < e.getTiempoDeEtapaEnSegundos()?-1:0;
  40. }
  41.  
  42. public int getTiempoDeEtapaEnSegundos(){
  43. int suma = 0;
  44. for(int t = SEG_EN_HORA; t >= SEG_EN_SEG; t /= SEG_EN_MIN){
  45. switch (t){
  46. case SEG_EN_HORA: suma += horaEnsegundos();break;
  47. case SEG_EN_MIN : suma += minEnSegundos();break;
  48. case SEG_EN_SEG : suma += segundosEnTiempo();break;
  49. }
  50. }
  51. return suma;
  52. }
  53.  
  54. public double getTiempoDeEtapaEnMinutos(){
  55. return (double)getTiempoDeEtapaEnSegundos() / SEG_EN_MIN;
  56. }
  57.  
  58. public double getTiempoDeEtapaEnHoras(){
  59. return (double)getTiempoDeEtapaEnSegundos() / SEG_EN_HORA;
  60. }
  61.  
  62. public String toString() {
  63. StringBuilder tiempo = new StringBuilder();
  64. for (int t = SEG_EN_HORA; t >= SEG_EN_SEG; t /= SEG_EN_MIN) {
  65. switch (t) {
  66. case SEG_EN_HORA:tiempo.append(hora);break;
  67. case SEG_EN_MIN: tiempo.append(min);break;
  68. case SEG_EN_SEG: tiempo.append(seg);break;
  69. }
  70. if (t >= SEG_EN_MIN)
  71. tiempo.append(":");
  72. }
  73. return tiempo.toString();
  74. }
  75. }
  76.  
  77.  
  78. import java.util.Arrays;
  79.  
  80. public class Test {
  81. public static void main(String[] args){
  82. //crear una etapa de una hora y un minuto
  83. EtapaDeCompetencia e = new EtapaDeCompetencia(1,1);
  84. //puedes pedir tiempo en varios formatos
  85. System.out.println(e.getTiempoDeEtapaEnHoras());
  86. System.out.println(e.getTiempoDeEtapaEnMinutos());
  87. System.out.println(e.getTiempoDeEtapaEnSegundos());
  88. //muestra la etapa h:m:S
  89. System.out.println(e);
  90. //crear un etapa de solo una hora
  91. EtapaDeCompetencia e2 = new EtapaDeCompetencia(1);
  92. //comparar etapas
  93. EtapaDeCompetencia mayor = null,menor = null;
  94. if(e.compareTo(e2) > 0){
  95. mayor = e;
  96. menor = e2;
  97. }
  98. //mostrar mayor y menor entre dos etapas
  99. System.out.println("etapa mayor : "+ mayor +" tiempo total en segundos " +mayor.getTiempoDeEtapaEnSegundos());
  100. System.out.println("etapa mrnor : "+ menor +" tiempo total en segundos " +menor.getTiempoDeEtapaEnSegundos());
  101.  
  102. //comparar n etapas(3)
  103. EtapaDeCompetencia[] etapas = new EtapaDeCompetencia[3];
  104. //2 horas 3 mins 45 s
  105. etapas[0] = new EtapaDeCompetencia(2,3,45);
  106. //1 hora 30 min 40 s
  107. etapas[1] = new EtapaDeCompetencia(1,30,40);
  108. //3 hroras
  109. etapas[2] = new EtapaDeCompetencia(3);
  110. //ordenar etapas por tiempo
  111. Arrays.sort(etapas);
  112. //mostrar etapa mayor
  113. System.out.println("etapa mayor " + etapas[etapas.length - 1]);
  114. System.out.println("con tiempo total de " + etapas[etapas.length - 1].getTiempoDeEtapaEnSegundos()+" segundos");
  115.  
  116. }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement