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 )";
		}
	}
	}
}

实验输出成果显示:
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值