Advertisement
veto14

teste.java

Aug 28th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. package construtor;
  2.  
  3. class Horario{
  4.  
  5. int hora,minuto,segundo;
  6.  
  7. Horario(int h, int m, int s){
  8. hora = h;
  9. minuto = m;
  10. segundo = s;
  11. }
  12.  
  13. Horario(int h, int m){
  14. hora=h;
  15. minuto=m;
  16. segundo=0;
  17. }
  18.  
  19. Horario(int h){
  20. hora=h;
  21. minuto=0;
  22. segundo=0;
  23. }
  24.  
  25. //letra a termina aqui
  26.  
  27. Horario(Horario h){
  28. hora=h.hora;
  29. minuto=h.minuto;
  30. segundo=h.segundo;
  31. }
  32.  
  33. String formato24h() {
  34. return hora + "HH" + minuto + "MM" + segundo + "SS";
  35. }
  36.  
  37. String formato12h() {
  38. String fim = "AM";
  39. if (hora > 12) {
  40. hora = hora - 12;
  41. fim = "PM";
  42. }
  43. return hora + "HH" + minuto + "MM" + segundo + "SS" + fim;
  44. }
  45.  
  46. int diferenca(Horario h) {
  47. int h1 = hora * 3600 + minuto * 60 + segundo;
  48. int h2 = h.hora*3600 + h.minuto*60 + h.segundo;
  49. if(h1 > h2) return h1-h2;
  50. else return h2-h1;
  51. }
  52. }
  53.  
  54. public class Teste {
  55. public static void main (String[] args) {
  56. Horario h1 = new Horario (23,10,3);
  57. Horario h2 = new Horario (23,10);
  58. Horario h3 = new Horario (23);
  59. Horario h4 = new Horario (h1);
  60.  
  61. System.out.println("formato 24h " + h1.formato24h());
  62. System.out.println("formato 12h " + h1.formato12h());
  63. System.out.println("formato 24h da copia de h1" + h4.formato24h());
  64. System.out.println("formato 12h da copia de h1" + h4.formato12h());
  65. System.out.println("formato 24h " + h2.formato24h());
  66. System.out.println("formato 12h " + h2.formato12h());
  67. System.out.println("formato 24h " + h3.formato24h());
  68. System.out.println("formato 12h " + h3.formato12h());
  69.  
  70. System.out.println("diferença entre h3 e h1 = " + h3.diferenca(h1) + " Segundos");
  71. }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement