welcome: please sign in
location: attachment:Classi-Time.txt of InformaticaCDLmatematica

Attachment 'Classi-Time.txt'

Download

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

Attached Files

You are not allowed to attach a file to this page.