Java面向對象靜態方法及域值控制&日周時分進制方法"以時鐘對象實驗爲例"

Suppose you are given a class named ClockTime with the following contents:
假設你有一個包含下列內容名爲“時鐘時間”的組:
Clock Time
Write an instance method advance that will be placed inside the ClockTime class. The method accepts a number of minutes as its parameter and moves your object forward in time by that amount of minutes. The minutes passed could be any non-negative number, even a large number such as 500 or 1000000. If necessary, your object might wrap into the next hour or day, or it might wrap from the morning (“AM”) to the evening (“PM”) or vice versa. (A ClockTime object doesn’t care about what day it is; if you advance by 1 minute from 11:59 PM, it becomes 12:00 AM.)
For example, the following calls would produce the following results:
在時鐘對象中編寫一個內置的靜態方法,名爲“快進”。要求接受分鐘整數做爲參數,使得時鐘對象的時間根據參數前進。分鐘要求可以爲任意大小的非負數,根據時分制進制,從AM(早晨)進制PM(夜晚),超出一日一週的,能夠反映幾日或幾星期前進。
*進階要求,數字組整齊,PM 與 AM應對照整齊。
*再不改變對象大體面貌的情況下
在這裏插入圖片描述
博主總結此對象難點有三:
1) 前進的時間轉換爲周、日、時、分反映
// 可以通過/60 /1440 /10080 %60 %1440 %10080 結合實現
// 對於細節把控能力要求較高,利用if,else/if 基礎語句即可實現
2) 在前進時間超出後,如何反映日,月,年
// clcoktime的字符串域初始化爲"AM",“PM” 如何避免更改這一值,並避開增加字符串變量達到反映日周變更,直接更改AM與PM + “日周變更”
3) 反映AM、PM變更;定義對象參數例外參數處理
// 利用if,else/if 基礎語句即可實現
//利用 throw new IllegalArgumentException(“xxxx”);
完整實驗代碼:


public class _ClockExperiment {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		clocktime n1 = new clocktime(6,27,"PM");
        System.out.println(n1.toString());
        n1.advance(1);
        System.out.println(n1.toString());
        n1.advance(30);
        System.out.println(n1.toString());
        n1.advance(5);
        System.out.println(n1.toString());
        n1.advance(60);
        System.out.println(n1.toString());
        n1.advance(128);
        System.out.println(n1.toString());
        n1.advance(180);
        System.out.println(n1.toString());
        n1.advance(1440);
        System.out.println(n1.toString());
        n1.advance(21075);
        System.out.println(n1.toString());
	}

}

完整對象代碼:

//_ClockExperiment
public class clocktime {
	private int hour;
	private int minute;
	private String amPm;
	//域值控制 
	public clocktime (int h, int m, String ap) {
		if (h<0||h>12) {
		//小時>0 && <12 
			throw new IllegalArgumentException("TYPE HOUR BETWEEN 1 TO 12!");
	}else {
		this.hour = h;
	}
		if (m<0||m>60) {
		//分>=0 <60
			throw new IllegalArgumentException("TYPE HOUR BETWEEN 0 TO 60!");
		}else{
			this.minute = m;
		}
		if ((ap.equalsIgnoreCase("AM"))||ap.equalsIgnoreCase("PM")) {
			this.amPm = ap.toUpperCase();
	}else{
	//標籤爲 "AM"||"PM"
		throw new IllegalArgumentException("TYPE AM OR PM!!");
	}
	
	}
	public int getHour() {
		return this.hour;
	}
	public int getMinute() {
		return this.minute;
	}
	public String getamPm() {
		return this.amPm;
	}
	//使不足兩位的分值以兩位轉換,因爲必定是從字符串角度轉換,所以在toString方法中進行調整
	public String toString() {
		String result;
		if (this.minute/10 !=0) {
		result = this.hour +":"+ this.minute+" "+this.amPm;
		}else {
			result = this.hour +":0"+ this.minute+" "+this.amPm;
		}
		if (this.hour/10!=0) {
			return result;
		}else {
			return " "+result;
		}
	}
	//前進日、周、晚轉換
	public void advance(int amount) {
		int days,weeks;
		if (amount<1440) {
		if (amount/60!=0) {
			this.hour+=amount/60;
			this.minute+= amount%60;
			
		}else {
			this.minute+=amount;
		}
		if (this.minute>=60) {
			this.minute = this.minute-60;
			this.hour++;
		}
		if (this.hour>12) {
			this.hour = this.hour-12;
			if (this.amPm == "PM") {
				this.amPm = "AM";
			}else {
				this.amPm = "PM";
			}
		}
	}else if(amount>=1440&&amount<10080) {
		days = amount/1440;
		amount = amount%1440;
		if (amount/60!=0) {
			this.hour+=amount/60;
			this.minute+= amount%60;
		}else {
			this.minute+=amount;
		}
		if (this.minute>=60) {
			this.minute = this.minute-60;
			this.hour++;
		}
		if (this.hour>12) {
			this.hour = this.hour-12;
			if (this.amPm == "PM") {
				this.amPm = "AM";
			}else {
				this.amPm = "PM";
			}
		}
		this.amPm= this.amPm+" ( "+days+" days later )";
	}else if(amount>=10080) {
		weeks = amount/10080;
		amount = amount%10080;
		days = amount/1440;
		amount = amount%1440;
		if (amount/60!=0) {
			this.hour+=amount/60;
			this.minute+= amount%60;
		}else {
			this.minute+=amount;
		}
		if (this.minute>=60) {
			this.minute = this.minute-60;
			this.hour++;
		}
		if (this.hour>12) {
			this.hour = this.hour-12;
			if (this.amPm == "PM") {
				this.amPm = "AM";
			}else {
				this.amPm = "PM";
			}
		}
		//如果days爲0,跳過days前進顯示
		if (days!=0) {
		this.amPm= this.amPm+" ( "+weeks+" weeks "+ days+" days later )";
		}else {
			this.amPm= this.amPm+" ( "+weeks+" weeks later )";
		}
	}
	}
}

實驗輸出成果顯示:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章