Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package altre;
- public class AlessiaeMassi {
- public static int[] scanTime (String timeStr){
- char[] timeChar = timeStr.toCharArray();
- int[] time = new int[3];
- String temp = "";
- int cont = 0;
- if (timeStr == ""){
- return time;
- }
- for(int i = 0; i<timeChar.length; i++){
- if (timeChar[i] != ':'){
- temp += timeChar[i];
- } else {
- time[cont] = Integer.parseInt(temp);
- temp = "";
- cont++;
- }
- }
- time[cont] = Integer.parseInt(temp);
- return time;
- }
- public static int toSeconds(int hour, int min, int sec){
- assert min < 60;
- assert sec < 60;
- return sec + min*60 + hour*3600;
- }
- public static int[] toHMS(int secs){
- int[] ris = new int[3];
- while (secs > 3600){
- secs -= 3600;
- ris[0]++;
- }
- while (secs > 60){
- secs -= 60;
- ris[1]++;
- }
- ris[2] = secs;
- assert ris[1] < 60;
- assert ris[2] < 60;
- return ris;
- }
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- if (args.length < 2){
- int[] tempo = scanTime(args[0]);
- System.out.println("il tempo calcolato in secondi รจ : " + toSeconds(tempo[0], tempo[1], tempo[2]));
- } else {
- System.out.println("Formato degli argomenti errato.");
- }
- }
- }
- //---JUNIT---
- package altre;
- import junit.framework.TestCase;
- public class AlessiaeMassiTest extends TestCase {
- public void testscanTime(){
- String[] casiditest = {
- "",
- "00:00:00",
- "0:0:0",
- "1:2:3",
- "01:02:03",
- "11:22:33"
- };
- int[][] risAttesi = {
- {0,0,0},
- {0,0,0},
- {0,0,0},
- {1,2,3},
- {1,2,3},
- {11,22,33},
- };
- for(int i = 0; i < casiditest.length; i++){
- assertEquals("errore scanTime - " + i, AlessiaeMassi.scanTime(casiditest[i])[0], risAttesi[i][0]);
- assertEquals("errore scanTime - " + i, AlessiaeMassi.scanTime(casiditest[i])[1], risAttesi[i][1]);
- assertEquals("errore scanTime - " + i, AlessiaeMassi.scanTime(casiditest[i])[2], risAttesi[i][2]);
- }
- }
- public void testtoSeconds(){
- int[][] prove = {
- {0,0,0},
- {99,59,59},
- {1,2,3},
- {01,02,03},
- {11,22,33},
- };
- int[] attesi = {
- 0,
- 359999,
- 3723,
- 3723,
- 40953
- };
- for(int i = 0; i < prove.length; i++){
- assertTrue("errore toSeconds - " + i, AlessiaeMassi.toSeconds(prove[i][0], prove[i][1], prove[i][2]) == attesi[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement