public class TestClock {
public static void main(String[] args){
/*(1)declare int variables hA, mA, hB, mB, hC, mC
*/
int hA;
int mA;
int hB;
int mB;
int hC;
int mC;
/*(2)declare String variables milA, milB, milC, civA, civB, civC
*/
String milA;
String milB;
String milC;
String civA;
String civB;
String civC;
/*(3)declare Clock variables clockA, clockB, clockC */
Clock clockA;
Clock clockB;
Clock clockC;
/*(4)construct clockA using default constructor */
clockA = new Clock();
/*(5)construct clockB using alternate constructor
* for military time 2400
*/
clockB = new Clock();
/*(6)construct clockC using alternate constructor
* with hours=11 and minutes=45
*/
clockC = new Clock();
/*(7)print clockA, clockB, clockC on separate lines
*/
System.out.println(clockA());
System.out.println(clockB());
System.out.println(clockC());
编辑:进一步说明......
/*(8)assign hours and minutes to hA,mA, hB,mB, hC,mC
* using the hours() and minutes() methods
* for clockA, clockB, and clockC respectively
*/
hA = clockA(hours);
mA = clockA(minutes);
hB = clockB(hours);
mB = clockB(minutes);
hC = clockC(hours);
mC = clockC(minutes);
/*(9)assign militaryTime and civilianTime to milA, civA,
* milB, civB, milC, civC for clockA,clockB,clockC resp
* using the appropriate methods
*/
该程序(TestClock)用于测试程序Clock中的所有方法(单独制作,参见下面的代码)。评论是我们必须做的指示。那么,为什么,对于打印语句,当我清楚地将它们定义为上面的新时钟时,它是否说找不到clockA,clockB和clockC?
这是单独的时钟程序:
public class Clock {
/* instance field:
* totalMinutes is always between 1 and 24*60
*/
private int totalMinutes;
/** default constructor sets the clock to represent 12:01 a.m.
*/
public Clock(){
totalMinutes = 1;
} //end default constructor
/** alternate contructor sets clock to represent time in military
* parameter hours - number of hours since midnight previous day
* parameter minutes - number of minutes since last hour changed
* e.g. 14:03 military is equivalent to 2:03 p.m. civilian
* preconditions: 0<=hours<=24, 0<=minutes<=59,
* 0<hours*60 + minutes<=24*60
*/
public Clock(int hours, int minutes){
totalMinutes = hours*60 + minutes;
String errMsg = null;
if ( minutes < 0 || minutes > 59){
errMsg = "\nminutes="+minutes+" not between 0 and 59";
} else if ( hours < 0 || hours > 24){
errMsg = "\nhours="+hours + " not between 0 and 24";
} else if (totalMinutes < 1 || totalMinutes>60*24 ) {
errMsg = "\ntotalMinutes not between 1 and 24*60";
}
if (errMsg != null){
throw new IllegalArgumentException(errMsg);
}
} //end alternate constructor
/** returns the number of hours in this Clock's time
*/
public int hours(){
int hrs = totalMinutes / 60;
return hrs;
}
/** returns the number of minutes since the last hour change
*/
public int minutes(){
int min = totalMinutes % 60;
return min;
}
/** returns a printable version of the time in Military context
*/
public String militaryTime(){
String mTime = "",hStr="",mStr="";
int hours = totalMinutes / 60;
int minutes = totalMinutes % 60;
if (hours<10){
hStr = "0"+hours;
} else {
hStr = "" + hours;
}
if (minutes<10){
mStr = "0"+minutes;
} else {
mStr = "" + minutes;
}
mTime = hStr + "" + mStr;
return mTime;
}
/** returns a printable version of the time in Civilian context
*/
public String civilianTime(){
String cTime = "", mStr="", suffix="";
int hours = totalMinutes / 60;
int minutes = totalMinutes % 60;
if ( totalMinutes == 12*60 ){
cTime = "12:00 noon";
}else if ( totalMinutes == 24*60)
cTime = "12:00 midnight";
else { //neither noon nor midnight
if (minutes < 10) {
mStr = "0" + minutes;
} else {
mStr = "" + minutes;
}
if (totalMinutes > 12*60){
hours = hours - 12;
suffix = " p.m.";
} else {
suffix = " a.m.";
}
cTime = hours + ":" + mStr + suffix;
} //end neithernoon nor midnight
return cTime;
}
}
答案 0 :(得分:1)
没有像System.out.println(clockA());
那样的clockA()
。
将其更改为System.out.println(clockA);
。覆盖Clock类中的toString()
。
当我清楚地定义上面的Clock实例时,为什么会出现“找不到符号”的错误?
如果无法识别Class类型,通常会出现上述错误。所以import your class Clock
。
答案 1 :(得分:0)
/*(7)print clockA, clockB, clockC on separate lines
*/
System.out.println(clockA());
System.out.println(clockB());
System.out.println(clockC());
您使用的是clockC()
,因为您添加了()
它引用了一种方法。
如果要打印变量,请使用:
/*(7)print clockA, clockB, clockC on separate lines
*/
System.out.println(clockA);
System.out.println(clockB);
System.out.println(clockC);
答案 2 :(得分:0)
如下的行:
的System.out.println(clockA());
hA = clockA(小时);
错了。 clockA是一个对象,它不是一个被调用的函数,所以不要使用clockA()。类似地,clockA(小时)是错误的.clockA不是一个通过参数传递调用的方法,即“小时”。而是通过类的对象正确调用方法: