例子问题
例子问题1:信息隐藏
考虑以下代码:
公共静态类时钟{
私有int秒;
公共时钟(int s) {
秒= s;
}
公共无效setTime(int s) {
秒= s;
}
公共无效setSeconds(int s) {
int hoursMinutes = seconds - seconds % 60;
seconds = hoursMinutes + s;
}
公共无效setMinutes(int min) {
Int hours = seconds / 3600;
int currentSeconds = seconds % 60;
seconds = hours + min * 60 + currentSeconds;
}
}
下面哪个选项表示返回时钟的分钟值的方法?
公共int getMinutes() {
返回秒* 60;
}
公共int getMinutes() {
Int hours = seconds / 3600;
返回(秒-小时* 3600);
}
公共int getMinutes() {
Int hours = seconds / 3600;
返回(秒-小时* 3600)/ 60;
}
公共int getMinutes() {
返回秒数/ 60;
}
公共int getMinutes() {
返回分钟;
}
公共int getMinutes() {
Int hours = seconds / 3600;
返回(秒-小时* 3600)/ 60;
}
这个时钟类被定义为其字段中只有秒,因此必须为任何访问器和突变器转换此值。因此,您需要为这个转换执行仔细的(虽然简单)计算。要计算给定秒数中的分钟数,首先需要从总计数中删除小时数。首先,使用整数除法(这将去掉小数部分)计算小时数。这是:
Int hours = seconds / 3600;
接下来,您需要计算出小时的值中有多少秒。这很有帮助,因为整数除法去掉了小数点部分。因此,你将减去:
时数* 3600
从总秒数来看。
这将给出应用于时间的分和秒的秒。因此,您最终需要除以60来分离分钟。
例子问题1:实现技术
考虑以下代码:
公共类时钟{
私有int秒;
公共时钟(int s) {
秒= s;
}
公共无效setTime(int s) {
秒= s;
}
公共无效setSeconds(int s) {
int hoursMinutes = seconds - seconds % 60;
seconds = hoursMinutes + s;
}
公共无效setMinutes(int min) {
Int hours = seconds / 3600;
int currentSeconds = seconds % 60;
seconds = hours + min * 60 + currentSeconds;
}
}
下面哪个选项定义了atoString方法,该方法将以以下形式以24小时格式输出时间:
1:51:03
(注意你需要填充分钟和秒。你可以称12午夜为“0”。)
公共字符串toString() {
Int hours = seconds / 3600;
Int min =(秒-小时* 3600);
字符串minString = ""+分钟;
If (min < 10) {
minString = "0" + minString;
}
Int secs = seconds % 60;
String secString = "" + secs;
If (secs < 10) {
secString = "0" + secString;
}
return hours +":"+ minString+":"+secString;
}
公共字符串toString() {
Int hours = seconds / 3600;
Int min =秒-(小时* 3600)/ 60;
字符串minString = ""+分钟;
If (min < 10) {
minString = "0" + minString;
}
Int secs = seconds * 60;
String secString = "" + secs;
If (secs < 10) {
secString = "0" + secString;
}
return hours +":"+ minString+":"+secString;
}
公共字符串toString() {
Int hours =秒/ 60;
Int min =秒-(小时* 3600)/ 60;
字符串minString = ""+分钟;
If (min < 10) {
minString = "0" + minString;
}
Int secs = seconds * 60;
String secString = "" + secs;
If (secs < 10) {
secString = "0" + secString;
}
return hours +":"+ minString+":"+secString;
}
公共字符串toString() {
Int hours = seconds / 3600;
Int min =(秒-小时* 3600)/ 60;
字符串minString = ""+分钟;
If (min < 10) {
minString += "0";
}
Int secs = seconds % 60;
String secString = "" + secs;
If (secs < 10) {
secString += "0";
}
return hours +":"+ minString+":"+secString;
}
公共字符串toString() {
Int hours = seconds / 3600;
Int min =(秒-小时* 3600)/ 60;
字符串minString = ""+分钟;
If (min < 10) {
minString = "0" + minString;
}
Int secs = seconds % 60;
String secString = "" + secs;
If (secs < 10) {
secString = "0" + secString;
}
return hours +":"+ minString+":"+secString;
}
公共字符串toString() {
Int hours = seconds / 3600;
Int min =(秒-小时* 3600)/ 60;
字符串minString = ""+分钟;
If (min < 10) {
minString = "0" + minString;
}
Int secs = seconds % 60;
String secString = "" + secs;
If (secs < 10) {
secString = "0" + secString;
}
return hours +":"+ minString+":"+secString;
}
在这段代码中有几点需要注意。首先,您需要根据存储在类中的时间计算每个组成部分。秒的“显示值”相对容易。这只是除以60的余数。考虑:
这实际上是1分零1秒。
这实际上是2分35秒。
所以,你知道显示秒是:
秒% 60
您必须使用整数除法(这将去掉小数部分)来计算小时数。这是:
Int hours = seconds / 3600;
接下来,您需要计算出小时的值中有多少秒。这很有帮助,因为整数除法去掉了小数点部分。因此,你将减去:
时数* 3600
从总秒数来看。
这将给出应用于时间的分和秒的秒。因此,您最终需要除以60来分离分钟。
然后,您必须填充值。这是不通过使用+ =
,它将在字符串的末尾添加“0”字符。相反,它是通过表单完成的:
secString = "0" + secString;