该程序的目的是提示用户输入活动时间,该活动时间将打印运行总计,并在用户决定结束应用程序后最终以分钟和秒为单位返回总时间。我遇到的问题是了解如何通过在类型Itime对象上调用addTime方法来添加带有activityTime的totalTime。
驱动程序类
public class ActivityManager
{
public static void main(String[] args)
{
Itime totalTime, activityTime; // declare totalTime and activityTime of type Itime
int minutes; double seconds; // user input values
Scanner input = new Scanner (System.in); // to read user input
// display purpose and author
System.out.println ("This program tracks total time in minutes");
System.out.println ("and seconds for a series of activities.");
System.out.println (); // print blank line
// specify format for input
System.out.println ("Enter activity time in minutes and"
+ " seconds, all in a");
System.out.println ("single line with spaces in between. Entering"
+ " values" );
System.out.println ("outside appropriate ranges will terminate"
+ " the program.");
System.out.println (); // print blank line
// create the totalTime object of type Itime with 0 minutes and 0.0 seconds
totalTime = new Itime (0,0.0);
System.out.println ("Total time so far is: "
+ totalTime.toString());
System.out.println (); // print blank line
// prompt and read time for an activity
System.out.print ("Enter time for an activity: ");
minutes = input.nextInt();
seconds = input.nextDouble();
// Accumulate if appropriate
while (minutes >= 0 && seconds >= 0 && seconds < 60) {
// create the activityTime object of type Itime with given minutes and seconds
activityTime = new Itime (minutes, seconds);
// add totalTime and activityTime and put the result in totalTime
totalTime = totalTime.addTime(activityTime);
System.out.println ("Total time so far is: " + totalTime.toString());
System.out.println (); // print blank line
// prompt and read time for another activity
System.out.print ("Enter time for an activity: ");
minutes = input.nextInt();
seconds = input.nextDouble();
}
// wrap up and print final total
System.out.println ("Sentinel received");
System.out.println (); // print blank line
System.out.println ("Total time so far is: "
+ totalTime.toString());
System.out.println (); // print blank line
// print closing remarks
System.out.println ("Program has terminated.");
System.out.println (); // print blank line
}
}
主类
public class Itime
{
private int minutes;
private double seconds;
/**
* Constructer objects of class Itime
*/
public Itime (int minutes, double seconds)
{
assert minutes >=0;
assert seconds >=0 && seconds <60;
}
/**
* Getter methods
*/
public int getMinutes() { return this.minutes; }
public double getSeconds() { return this.seconds; }
/**
* Method to return time in String format
*/
public String toString ()
{
String toString = minutes + " minutes and " + seconds + " seconds";
return toString;
}
**public addTime (pass Itime objects as params here)**
{
}
}
答案 0 :(得分:0)
尝试一下
public Itime addTime(Itime itime) {
this.minutes = this.minutes + itime.getMinutes();
this.seconds = this.seconds + itime.getSeconds();
if (this.seconds > 60) {
this.seconds = this.seconds % 60;
this.minutes++;
}
return this;
}
答案 1 :(得分:0)
Java有一段时间的内置类:Duration
。因此,您实际上是在尝试重新发明轮子。我建议您不要这样做。使用Itime
进行实施时,您的Duration
类的外观如下:
public class Itime {
private static final double NANOS_PER_SECOND = TimeUnit.SECONDS.toNanos(1);
private final Duration dur;
/**
* Constructer objects of class Itime
*/
public Itime(int minutes, double seconds) {
if (minutes < 0) {
throw new IllegalArgumentException("minutes must be >= 0");
}
if (seconds < 0 || seconds >= 60) {
throw new IllegalArgumentException("seconds must be >= 0 and < 60");
}
dur = Duration.ofMinutes(minutes).plus(Duration.ofNanos(Math.round(seconds * NANOS_PER_SECOND)));
}
private Itime(Duration dur) {
this.dur = dur;
}
// Getter methods
public int getMinutes() {
return Math.toIntExact(dur.toMinutes());
}
public double getSeconds() {
return dur.toSecondsPart() + dur.toNanosPart() / NANOS_PER_SECOND;
}
/**
* @return time in String format
*/
public String toString() {
String toString = getMinutes() + " minutes and " + getSeconds() + " seconds";
return toString;
}
public Itime addTime(Itime other) {
return new Itime(dur.plus(other.dur));
}
}
您使用的第二个构造方法使您的addTime
方法成为了一种方法。 Duration
类负责所有时间的数学运算。这是一件好事,因为它写起来有点繁琐且容易出错。
使用此实现,以下是您的驱动程序类的摘录:
Enter time for an activity: 91 43.21
Total time so far is: 91 minutes and 43.21 seconds
Enter time for an activity: 12 20
Total time so far is: 104 minutes and 3.21 seconds
Enter time for an activity: -1 -1
Sentinel received
注意事项:
Duration
不直接接受或作为double
给我们几秒钟,因此很遗憾,我不得不在构造函数和getSeconds
方法中加入一些数学运算。dur
字段final
,因为我发现不需要对其进行修改(您可以类似地在实现final
中声明这两个字段)。assert
构造函数中使用public
来验证参数被认为是较差的样式。相反,我使用if
并抛出适当的IllegalArgumentException
。 (但是参数验证很好; assert
总比没有好。)Itime
传递给addTime
方法,并从其中获取一个Itime
。该方法必须使用类型为Itime
的一个参数和返回类型为Itime
的{{1}}来声明。在上述设计中,public Itime addTime(Itime other)
实际上已成为Itime
的包装。这种设计有其优点和缺点。相反,您可以完全放弃Duration
类并直接使用Itime
。优点:
Duration
为您解析的格式。然后,我们将不再需要任何数学运算即可将Duration.parse
转换为double
可以理解的内容。骗局
Duration
类设计的,在某些方面Itime
不太适合。具体来说,您可能想编写一种辅助方法,用于将Duration
转换为类似Duraiton
的字符串。