实体类-时间类(零点后经过秒数换算为时、分、秒)(Modifying the Internal Data Representation of a Class)

问题描述:

给出零点以后经过的任意时间(用秒数表示),如何确定相应的时、分、秒。

本程序中, 在原来的时间类中,增加了新的构造函数:Time2 (int secondsPassed) ,测试类中使用t2,t3进行了测试,很完美!

其中,换算算法的推演及测试是在Excel中完成的。(Java +Excel,很强大!)

更新记录:

2016/07/09:增加了tick,incrementMinute, incrementHour三个方法,分别为增加1秒、1分和1小时。(已考虑时间的进位问题)

 

代码如下:

package example;
//JHTP Exercise 8.5,8.7: Modifying the Internal Data Representation of a Class,Enhancing Class Time2
//by pandenghuang@163.com
/**(Modifying the Internal Data Representation of a Class) It would be
 * perfectly reasonable for the Time2 class of Fig. 8.5 to represent 
 * the time internally as the number of seconds since midnight rather 
 * than the three integer values hour, minute and second. Clients could
 * use the same public methods and get the same results. Modify the Time2
 * class of Fig. 8.5 to implement the time as the number of seconds 
 * since midnight and show that no change is visible to the clients 
 * of the class.
 * (Enhancing Class Time2) Modify class Time2 of Fig. 8.5 to include a tick method that increments
the time stored in a Time2 object by one second. Provide method incrementMinute to increment
the minute by one and method incrementHour to increment the hour by one. Write a
program that tests the tick method, the incrementMinute method and the incrementHour method
to ensure that they work correctly. Be sure to test the following cases:
a) incrementing into the next minute,
b) incrementing into the next hour and
c) incrementing into the next day (i.e., 11:59:59 PM to 12:00:00 AM).
*/
	// Fig. 8.5: Time2.java
	// Time2 class declaration with overloaded constructors.  

	class Time2
	{
	   private int hour; // 0 - 23
	   private int minute; // 0 - 59
	   private int second; // 0 - 59

	   // Time2 no-argument constructor: 
	   // initializes each instance variable to zero
	   public Time2()
	   {
	      this(0, 0, 0); // invoke constructor with three arguments
	   } 
	   
	 //自定义时间类
	   public Time2(int secondsPassed){
		   if (secondsPassed<=0){
			   throw new IllegalArgumentException("经过秒数不能为负!");
		   }
		   
		   this.second=secondsPassed%60;
		   this.minute=secondsPassed/60%60;
		   this.hour=(secondsPassed/60/60%60>=24)?0:secondsPassed/60/60%60;
	   }
	   
	   public Time2(int hour, int minute, int second) 
	   { 
	      if (hour < 0 || hour >= 24)
	         throw new IllegalArgumentException("hour must be 0-23");

	      if (minute < 0 || minute >= 60)
	         throw new IllegalArgumentException("minute must be 0-59");

	      if (second < 0 || second >= 60)
	         throw new IllegalArgumentException("second must be 0-59");

	      this.hour = hour;
	      this.minute = minute; 
	      this.second = second; 
	   } 

	   // Time2 constructor: another Time2 object supplied
	   public Time2(Time2 time)
	   {
	      // invoke constructor with three arguments
	      this(time.getHour(), time.getMinute(), time.getSecond());
	   } 

	   // Set Methods
	   // set a new time value using universal time; 
	   // validate the data
	   public void setTime(int hour, int minute, int second)
	   {
	      if (hour < 0 || hour >= 24)
	         throw new IllegalArgumentException("hour must be 0-23");

	      if (minute < 0 || minute >= 60)
	         throw new IllegalArgumentException("minute must be 0-59");

	      if (second < 0 || second >= 60)
	         throw new IllegalArgumentException("second must be 0-59");

	      this.hour = hour;
	      this.minute = minute; 
	      this.second = second; 
	   } 

	   // validate and set hour 
	   public void setHour(int hour) 
	   { 
	      if (hour < 0 || hour >= 24)
	         throw new IllegalArgumentException("hour must be 0-23");

	      this.hour = hour;
	   } 

	   // validate and set minute 
	   public void setMinute(int minute) 
	   { 
	      if (minute < 0 || minute >= 60)
	         throw new IllegalArgumentException("minute must be 0-59");

	      this.minute = minute; 
	   } 

	   // validate and set second 
	   public void setSecond(int second) 
	   { 
	      if (second < 0 || second >= 60)
	         throw new IllegalArgumentException("second must be 0-59");

	       this.second = second; 
	   } 

	   // Get Methods
	   // get hour value
	   public int getHour() 
	   { 
	      return hour; 
	   } 

	   // get minute value
	   public int getMinute() 
	   { 
	      return minute; 
	   } 

	   // get second value
	   public int getSecond() 
	   { 
	      return second; 
	   } 

	   // convert to String in universal-time format (HH:MM:SS)
	   public String toUniversalString()
	   {
	      return String.format(
	         "%02d:%02d:%02d", getHour(), getMinute(), getSecond());
	   } 

	   // convert to String in standard-time format (H:MM:SS AM or PM)
	   public String toString()
	   {
	      return String.format("%d:%02d:%02d %s", 
	         ((getHour() == 0 || getHour() == 12) ? 12 : getHour() % 12),
	         getMinute(), getSecond(), (getHour() < 12 ? "AM" : "PM"));
	   } 
	   
	 //增加一秒
	   public void tick(){
		   if(second+1<60)		   
			   second++;
		   else if (second+1==60){
			   second=0;
			   this.incrementMinute();
		   }
	   }
	   
	 //增加一分钟
	   public void incrementMinute(){
		   if(minute+1<60)		   
			   minute++;
		   else if (minute+1==60){
			   minute=0;
			   this.incrementHour();
		   }
	   }
	   
		 //增加一小时
	   public void incrementHour(){
		   if(hour+1<24)		   
			   hour++;
		   else if (hour+1==24){
			   hour=0;
		   }
	   }
	   
	} // end class Time2


	public class TimeTest 
	{
	   public static void main(String[] args)
	   {
	      Time2 t1 = new Time2(); // 00:00:00
	      Time2 t2 = new Time2(14386); // 零点后经过秒数(小于1天-86400秒)
	      Time2 t3 = new Time2(1548980); // 零点后经过秒数(任意秒数)
	      Time2 t4 = new Time2(12, 59, 40); // 12:59:40
	      Time2 t5 = new Time2(23, 59, 59); // 23:59:59

	      System.out.println("Constructed with:");
	      displayTime("t1: all default arguments", t1);
	      displayTime("t2: 通过零点后经过秒数(小于1天-86400秒)推算时、分、秒", t2);
	      displayTime("t3: 通过零点后经过秒数(任意秒数)推算时、分、秒", t3);
	      displayTime("t4: hour, minute and second specified", t4);
	      displayTime("t5: Time2 object t4 specified", t5);

		   t4.tick();
		   t5.tick();
		   
		   displayTime("t4: 增加1秒后的t4", t4);
		   displayTime("t5: 增加1秒后的t5", t5);
	      // attempt to initialize t6 with invalid values
	      try
	      {
	         Time2 t6 = new Time2(27, 74, 99); // invalid values
	      } 
	      catch (IllegalArgumentException e)
	      {
	         System.out.printf("%nException while initializing t6: %s%n",
	            e.getMessage());
	      } 
	   } 

	   // displays a Time2 object in 24-hour and 12-hour formats
	   private static void displayTime(String header, Time2 t)
	   {
	      System.out.printf("%s%n   %s%n   %s%n",
	         header, t.toUniversalString(), t.toString());
	   } 
	   

	   
	} // end class Time2Test

 

 

 

 

运行结果:

Constructed with:
t1: all default arguments
   00:00:00
   12:00:00 AM
t2: 通过零点后经过秒数(小于1天-86400秒)推算时、分、秒
   00:00:00
   12:00:00 AM
t3: 通过零点后经过秒数(任意秒数)推算时、分、秒
   10:16:20
   10:16:20 AM
t4: hour, minute and second specified
   12:25:42
   12:25:42 PM
t5: Time2 object t4 specified
   12:25:42
   12:25:42 PM

Exception while initializing t6: hour must be 0-23

 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值