Exercise clock in Java

13

I'm a newbie in this java and I just started classes, they sent us this exercise:

Exercise 3

  • Design a class called Reloj with the integer attributes: hora (24 hours), minuto and segundo , and the following methods:

  • A constructor that initializes the attributes to values passed as parameters.

  • A method called Increase that increases the time in a second.

  • For example:

    17:53:59 will be 17:54:00

    17:59:59 will be 18:00:00

    4.A method called Decrement that decreases the time in a second.

    For example:

    17:53:00 will be 17:52:59

    17:00:00 will be 16:59:59

    5.A method called Hora12 that converts to String the time by concatenating its attributes and returns the string with the format of 12 hours ( hh:mm:ss AM/PM )

    AM: (until 12 noon), PM (from 12 to 24 hours)

    This is my code, Class Reloj :

    public class Reloj {
        int modo,hora, minutos,segundos;
    
        int getmodo(){
    
        return modo;
    
        }
    
    
        public void setmodo(int modo){
    
        this.modo=modo;
        }
    
        int gethora(){
    
        return hora;
    
        }
    
    
        public void sethora(int hora){
    
        this.hora=hora;
        }
    
        public int getminutos(){
    
        return minutos;
    
        }
    
    
        public void setminutos(int minutos){
    
        this.minutos=minutos;
        }
    
        public  int getsegundos(){
    
        return segundos;
    
        }
    
    
       public  void setsegundos(int segundos){
    
        this.segundos=segundos;
        }
    
       public  Reloj(){
        modo=24; /*por defecto ponemos 24horas*/
        hora=0;
        minutos=0;
        segundos=0;
    
    
        }
    
        public Reloj( int h, int m, int s){
            this.modo=24;
            this.hora=h %24;
            this.minutos=m % 60;
            this.segundos=s % 60;
    
        }
    
    
       public void ponerEnHora(int md, int hh, int mm, int ss){
          modo=md;
          hora=hh % 24;
          minutos=mm % 60;
          segundos=ss % 60;
       }
    
       public void incrementar(){
        segundos++;
        if (segundos==60){
        segundos=0;
        minutos++;
        if(minutos==60){
        minutos=0;
        hora=(hora+1)%24;
        }
    
        }
    
        }
    
       public void decrementa(){
        segundos--;
        if(segundos<00){
            segundos=59;
            minutos--;
            if(minutos<00){
            minutos=59;
            hora=(hora-1) %24;
    
            }
        }
       }
       public String verHora(){
    
           String hms="Son las ";
    
           if (modo==12){//modo 12 horas    
               hms+=(hora>12)?hora-12:hora;
               hms+= ":"+minutos+":"+segundos;
               hms+=(hora>=12)?"pm":"am";
    
           }else{//modo 24h
           hms+=hora+":"+minutos+":"+segundos;
           }
       return (hms);
       }
    }
    

    MAIN watches class:

    public class Relojes {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            Reloj reloj1 = new Reloj();
            reloj1.ponerEnHora(24,17,10,10);
            System.out.println(reloj1.verHora());
            reloj1.incrementar();
            System.out.println(reloj1.verHora());
            reloj1.decrementa();
            System.out.println(reloj1.verHora());
    
        }
    
    }
    

    I do not get the hours in 12H mode, everything else works, but that I print the 12h mode with AM or PM does not come out, to see if you can help me out.

        
    asked by Flowcou 27.11.2018 в 12:32
    source

    1 answer

    3

    just needed to call the setMode method of the Clock class and give it the 12

    public static void main(String[] args) {
        Reloj reloj1 = new Reloj();        
        reloj1.ponerEnHora(24,17,10,10);
    
        reloj1.setmodo(12);//cambiamos el modo por defecto de 24 a 12
    
        System.out.println(reloj1.verHora());
        reloj1.incrementar();
        System.out.println(reloj1.verHora());
        reloj1.decrementa();
        System.out.println(reloj1.verHora());
    
    }
    
        
    answered by 27.11.2018 / 17:00
    source