Attachment '20121219-Classi-Time.txt'
Download 1 Esercizio 1: La classe Time e la classe TestTime con esempi di uso.
2 -----------
3
4
5 // inizio file Time.java /////////////////////
6 //////////////////////////////////////////////
7 package miopack;
8 import java.text.*;
9 public class Time {
10
11 private int ore, minuti, secondi;
12
13 public Time() {
14 reset();
15 }
16
17 public Time(int ore, int minuti, int secondi) {
18 setTime(ore, minuti, secondi);
19 }
20
21 public Time(Time t) {
22 setTime(t.ore, t.minuti, t.secondi);
23 }
24
25 public void reset() {
26 ore = 0;
27 minuti = 0;
28 secondi = 0;
29 }
30
31 public void setTime(int ore, int minuti, int secondi) {
32 // con valori fuori range azzera
33 this.ore = ( ore >= 24 || ore < 0) ? 0 : ore;
34 this.minuti = ( minuti >= 60 || minuti < 0) ? 0 : minuti;
35 this.secondi = ( secondi >= 60 || secondi < 0) ? 0 : secondi;
36 }
37
38 public void setTime (Time t) {
39 this.setTime(t.ore, t.minuti, t.secondi);
40 }
41
42 public void setTime (int secondi) {
43 int ore = secondi/3600;
44 secondi-=(ore*3600);
45 int minuti = secondi/60;
46 secondi-=(minuti*60);
47 this.setTime(ore, minuti, secondi);
48 }
49
50 public int getOre() {
51 return ore;
52 }
53
54 public int getMinuti() {
55 return minuti;
56 }
57
58 public int getSecondi() {
59 return secondi;
60 }
61
62 public String toString() {
63 DecimalFormat dueCifre = new DecimalFormat("00");
64 return dueCifre.format(ore) + ":" + dueCifre.format(minuti) + ":" + dueCifre.format(secondi);
65 }
66
67 public boolean piuGrandeDi(Time t) {
68 if (ore > t.ore)
69 return true;
70 if (ore == t.ore && minuti > t.minuti)
71 return true;
72 if (ore == t.ore && minuti == t.minuti && secondi > t.secondi)
73 return true;
74 return false;
75 }
76
77 public double timeInOre () {
78 return (ore + (double)minuti/60 + (double)secondi/3600);
79 }
80
81 public double timeInMinuti () {
82 return (ore*60 + minuti + (double)secondi/60);
83 }
84
85 public int timeInSecondi () {
86 return (ore*3600 + minuti*60 + secondi);
87 }
88
89 public void aggiungi(int ore, int minuti, int secondi) {
90 this.aggiungi(new Time(ore, minuti, secondi));
91 }
92
93 public void aggiungi(Time t) {
94 this.setTime(this.timeInSecondi()+t.timeInSecondi());
95 }
96 }
97
98 // fine file Time.java /////////////////////
99 //////////////////////////////////////////////
100
101
102 // inizio file TestTime.java /////////////////
103 //////////////////////////////////////////////
104 package miopack;
105 public class TestTime {
106
107 public static void main(String[] args) {
108
109 Time t1 = new Time();
110 System.out.println(t1);
111 t1.setTime(12,12,43);
112 Time t2 = new Time(t1);
113 System.out.println(t1);
114 t1.setTime(13,12,43);
115 t2.setTime(13,11,11);
116 System.out.println("t1: " + t1);
117 System.out.println("t2: " + t2);
118 System.out.println(t1.piuGrandeDi(t2));
119 t1.setTime(t2);
120 System.out.println("t1: " + t1);
121 System.out.println();
122 t1.setTime(3601);
123 System.out.println("t1: " + t1);
124 System.out.println(t1.timeInSecondi());
125
126 t1.setTime(86401);
127 System.out.println("t1: " + t1);
128 System.out.println(t1.timeInSecondi());
129 }
130 }
131 // fine file TestTime.java /////////////////
132 //////////////////////////////////////////////
Attached Files
You are not allowed to attach a file to this page.