Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class EtapaDeCompetencia implements Comparable<EtapaDeCompetencia> {
- private static final int SEG_EN_HORA = 3600;
- private static final int SEG_EN_MIN = 60;
- private static final int SEG_EN_SEG = 1;
- private final int hora,min,seg;
- public EtapaDeCompetencia(int h){
- this(h,0,0);
- }
- public EtapaDeCompetencia(int h, int m){
- this(h,m,0);
- }
- public EtapaDeCompetencia(int h, int m, int s){
- hora = h;
- min = m;
- seg = s;
- }
- private int convTiempoASegundos(int tiempo,int segundosEnTiempo){
- return tiempo * segundosEnTiempo;
- }
- public int horaEnsegundos(){
- return convTiempoASegundos(hora,SEG_EN_HORA);
- }
- public int minEnSegundos(){
- return convTiempoASegundos(min,SEG_EN_MIN);
- }
- public int segundosEnTiempo(){
- return seg;
- }
- public int compareTo(EtapaDeCompetencia e){
- return getTiempoDeEtapaEnSegundos() > e.getTiempoDeEtapaEnSegundos()? 1:
- getTiempoDeEtapaEnSegundos() < e.getTiempoDeEtapaEnSegundos()?-1:0;
- }
- public int getTiempoDeEtapaEnSegundos(){
- int suma = 0;
- for(int t = SEG_EN_HORA; t >= SEG_EN_SEG; t /= SEG_EN_MIN){
- switch (t){
- case SEG_EN_HORA: suma += horaEnsegundos();break;
- case SEG_EN_MIN : suma += minEnSegundos();break;
- case SEG_EN_SEG : suma += segundosEnTiempo();break;
- }
- }
- return suma;
- }
- public double getTiempoDeEtapaEnMinutos(){
- return (double)getTiempoDeEtapaEnSegundos() / SEG_EN_MIN;
- }
- public double getTiempoDeEtapaEnHoras(){
- return (double)getTiempoDeEtapaEnSegundos() / SEG_EN_HORA;
- }
- public String toString() {
- StringBuilder tiempo = new StringBuilder();
- for (int t = SEG_EN_HORA; t >= SEG_EN_SEG; t /= SEG_EN_MIN) {
- switch (t) {
- case SEG_EN_HORA:tiempo.append(hora);break;
- case SEG_EN_MIN: tiempo.append(min);break;
- case SEG_EN_SEG: tiempo.append(seg);break;
- }
- if (t >= SEG_EN_MIN)
- tiempo.append(":");
- }
- return tiempo.toString();
- }
- }
- import java.util.Arrays;
- public class Test {
- public static void main(String[] args){
- //crear una etapa de una hora y un minuto
- EtapaDeCompetencia e = new EtapaDeCompetencia(1,1);
- //puedes pedir tiempo en varios formatos
- System.out.println(e.getTiempoDeEtapaEnHoras());
- System.out.println(e.getTiempoDeEtapaEnMinutos());
- System.out.println(e.getTiempoDeEtapaEnSegundos());
- //muestra la etapa h:m:S
- System.out.println(e);
- //crear un etapa de solo una hora
- EtapaDeCompetencia e2 = new EtapaDeCompetencia(1);
- //comparar etapas
- EtapaDeCompetencia mayor = null,menor = null;
- if(e.compareTo(e2) > 0){
- mayor = e;
- menor = e2;
- }
- //mostrar mayor y menor entre dos etapas
- System.out.println("etapa mayor : "+ mayor +" tiempo total en segundos " +mayor.getTiempoDeEtapaEnSegundos());
- System.out.println("etapa mrnor : "+ menor +" tiempo total en segundos " +menor.getTiempoDeEtapaEnSegundos());
- //comparar n etapas(3)
- EtapaDeCompetencia[] etapas = new EtapaDeCompetencia[3];
- //2 horas 3 mins 45 s
- etapas[0] = new EtapaDeCompetencia(2,3,45);
- //1 hora 30 min 40 s
- etapas[1] = new EtapaDeCompetencia(1,30,40);
- //3 hroras
- etapas[2] = new EtapaDeCompetencia(3);
- //ordenar etapas por tiempo
- Arrays.sort(etapas);
- //mostrar etapa mayor
- System.out.println("etapa mayor " + etapas[etapas.length - 1]);
- System.out.println("con tiempo total de " + etapas[etapas.length - 1].getTiempoDeEtapaEnSegundos()+" segundos");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement