如何为使用 java 创建的时钟获得正确的输出?

How do I get the correct output for my clock created with java?

老师给我布置了这个作业。她给了我们一个 ClockTester.java 主程序,我们必须创建一个 public class 来声明要在时钟测试器中使用的对象和方法。下面是测试器,下面是我的 clock.java 代码。我无法按照她的意愿将其格式化为 00:00:00。我也把她的说明指南。

如果有人可以,请帮帮我。 当我尝试 运行 程序时,我也收到堆栈溢出错误。

Instructions from my teacher

//-------------------------------------------------------------------
//Program:  ClockTester
//Author: D. Spence
//Date:  April 4, 2014
//Purpose: Tests the features of the Clock class
//-------------------------------------------------------------------

import java.util.Scanner;

public class ClockTester
{
 public static void main (String[] args)
 {
  //Declare five objects in the Clock class
  Clock c1, c2, c3, c4, c5;

  //Initialize Clock objects using constructors
  c1 = new Clock();
  c2 = new Clock(15);
  c3 = new Clock(8,30);
  c4 = new Clock(12,5,45);

  //Show all four objects
  System.out.println("c1 is " + c1);
  System.out.println("c2 is " + c2);
  System.out.println("c3 is " + c3);
  System.out.println("c4 is " + c4);
  System.out.println();

  //Determine if clock times are past noon
  System.out.println(c1 + " is " + (c1.isPM() ? "after noon" : "at or before noon"));
  System.out.println(c2 + " is " + (c2.isPM() ? "after noon" : "at or before noon"));
  System.out.println(c3 + " is " + (c3.isPM() ? "after noon" : "at or before noon"));
  System.out.println(c4 + " is " + (c4.isPM() ? "after noon" : "at or before noon"));
  System.out.println();

  //Manually set a value
  c1.setTime(14,15,30);
  System.out.println("Changed c1 to " + c3);

  //Test for equality
  System.out.print (c1 + " is ");
  System.out.print ( (c1.equals(c3)) ? "equal" : "NOT equal");
  System.out.println (" to " + c3);
  System.out.println();

  c5 = new Clock(15,0,0);
  System.out.print (c5 + " is ");
  System.out.print ( (c5.equals(c2)) ? "equal" : "NOT equal");
  System.out.print (" to " + c2);
  System.out.println();


  //Demonstrate math operations
  System.out.println ("\n Some Clock Operations:");
  System.out.print (c1 + " + 10 hours is ");
  c1.addHours(10);
  System.out.println (c1);

  System.out.print (c3 + " + 45 minutes is ");
  c3.addMinutes(45);
  System.out.println (c3);

  System.out.print (c4 + " + 90 seconds is ");
  c4.addSeconds(90);
  System.out.println (c4);

  System.out.print (c4 + " + 12:59:55 is ");
  c4.addTime(12,59,55);
  System.out.println (c4);

  //TestBonus();

  System.out.println();
  System.out.println("This concludes the test of the Clock class.");
  System.out.println();

 }

/*
 public static void TestBonus()
 {
  System.out.println("\n ****TESTING BONUS FEATURE****\n");

  Clock c6 = new Clock(2,30,'p');
  System.out.println ("Non-military clock time: " + c6);
  c6.setMilitary (true);
  System.out.println ("Same clock in military: " + c6);
 }
*/

}










//-------------------------------------------------------------------
//Program:  Clock
//Author: Taylor P.
//Date:  November 30, 2016
//Purpose: Creates a clock class
//-------------------------------------------------------------------

public class Clock
{
 private int hours;
 private int minutes;
 private int seconds;
 private final int hours_Min = 0;
 private final int hours_Max = 23;
 private final int minutes_Min = 0;
 private final int minutes_Max = 59;
 private final int seconds_Min = 0;
 private final int seconds_Max = 59;


//Constructor sets default time to midnight (00:00:00)
 public Clock ()
 {
  this.hours = 00;
  this.minutes = 00;
  this.seconds = 00;
 }

//Constructor sets hours to input value
 public Clock (int h)
 {
  this.hours = h;
  this.minutes = 00;
  this.seconds = 00;
 }

//Constructor sets hours and minutes to input value
 public Clock (int h, int m)
 {
  this.hours = h;
  this.minutes = m;
  this.seconds = 00;
 }

//Constructor sets hours, minutes, seconds to input value
 public Clock (int h, int m, int s)
 {
  this.hours = h;
  this.minutes = m;
  this.seconds = s;
 }

//-------------------------------
//Method: getHours
//Return: int - hours
//Parameters: none
//Purpose: returns hours
//-------------------------------
 public int getHours()
 {
  return this.hours;
 }

//------------------------------
//Method: getMinutes
//Return: int
//Parameters: none
//Purpose: returns minutes
//------------------------------
 public int getMinutes()
 {
  return this.minutes;
 }

//------------------------------
//Method: getSeconds
//Return: int
//Parameters: none
//Purpose: returns seconds
//------------------------------
 public int getSeconds()
 {
  return this.seconds;
 }


//------------------------------
//Method: isPM
//Return: boolean
//Parameter: none
//Purpose: Determines if the time is past noon
//------------------------------
 public boolean isPM ()
 {
  if (this.hours < 12)
   return false;
  else
   return true;
 }

//------------------------------
//Method: setTime
//Return: void
//Parameter: 3 int types - hours, minutes, seconds
//Purpose: set time to (00:00:00) format
//-----------------------------
 public void setTime (int h, int m, int s)
 {
  this.hours = h;
  this.minutes = m;
  this.seconds = s;

 }

//-----------------------------
//Method: addHours
//Return: void
//Parameter: an int type of hours
//Purpose: to add hours together
//-----------------------------
 public void addHours (int h)
 {
  this.hours = this.hours + h;
 }

//-----------------------------
//Method: addMinutes
//Return: void
//Parameter: a int type of minutes
//Purpose: to add minutes together
//-----------------------------
 public void addMinutes (int m)
 {
  this.minutes = this.minutes + m;
 }

//----------------------------
//Method: addMinutes
//Return: void
//Parameter: an int type of seconds
//Purpose: to add seconds together
//----------------------------
 public void addSeconds (int s)
 {
  this.seconds = this.seconds + s;
 }

//-----------------------------
//Method: addTime
//Return: void
//Parameter: three int types of hours, minutes, seconds
//Purpose:
//-----------------------------
 public void addTime (int h, int m, int s)
 {
  this.hours = this.hours + h;
  this.minutes = this.minutes + m;
  this.seconds = this.seconds + s;
 }

//-----------------------------
//Method: equals
//Return:
//Parameter:
//Purpose:
//----------------------------
 public boolean equals (Clock c)
 {
  boolean equiv = c.equals(c);
  return equiv;
 }

//------------------------------
//Method: toString
//Returns:
//Parameters:
//Purpose:
//-----------------------------
 public String toString()
 {
  String string = "";
           if (this.equals(this.hours) && this.equals(this.minutes) && this.equals (this.seconds))
           {
               string = "00:00:00";
           }
           else if (this.equals(this.hours))
           {
      string = "00";
               string = string + ":" + this.minutes + ":" + this.seconds;
           }
           else if (this.equals(this.minutes))
           {
      string = "00";
      string = this.hours + ":" + string + ":" + this.seconds;
     }
     else if (this.equals(this.seconds))
     {
      string = "00";
            string = string + ":" + string + ":" + this.seconds;
     }
     else
      string = string + ":" + this.hours + ":" + this.minutes + ":" + this.seconds;
        return string;

 }

}

Clock

中的 toString 方法的代码似乎已损坏
 if (this.equals (this.hours ....

只是没有意义。

其次,一个值为 00 的整数与 0

相同

所以你的代码所做的是 return

String.format ("%02d:%02d:%02d", hours, minutes, seconds);

Clockequals方法中有一个递归调用。这就是您收到堆栈溢出错误的原因。

public boolean equals (Clock c)
{
    boolean equiv = c.equals(c); // this keeps calling itself
    return equiv;
}

将其更改为更合适的逻辑以检查 Clock 对象的相等性。

例如,

public boolean equals (Clock c)
{
    boolean equiv = c!=null && this.hours==c.hours && this.minutes==c.minutes && this.seconds==c.seconds;
    return equiv;
}

还有,toString()方法很乱。那也需要重写。像这样:

public String toString()
{
    String string = String.format("%02d:%02d:%02d", this.hours, this.minutes, this.seconds);
    return string;
}

您的 equals 方法是递归的,并将 c 与自身进行比较;相反,比较 cthis 的字段。此外,您应该使用 @Override 注释。它会告诉您 Object.equals 需要一个 Object。像

@Override
public boolean equals(Object o) {
    if (o instanceof Clock) { // <-- Is it a Clock?
        Clock c = Clock.class.cast(o); // <-- Use o as a Clock
        return c.isPM() == this.isPM() //
                && c.getHours() == this.getHours() //
                && c.getMinutes() == this.getMinutes() //
                && c.getSeconds() == this.getSeconds();
    }
    return false;
}

然后,您的 toString 方法可以通过创建一个简单的辅助方法来为两位数填充 int(s) 来大大简化。喜欢,

private static String get2Digits(int i) {
    if (i < 10) {
        return "0" + i;
    }
    return String.valueOf(i);
}

然后使用它来更正 toString(并使用 @Override,即使您 知道 toString 不接受任何参数)。您可以使用 StringBuilder 来执行串联。

@Override
public String toString() {
    return new StringBuilder(8) //
            .append(get2Digits(hours)) //
            .append(':').append(get2Digits(minutes)) //
            .append(':').append(get2Digits(seconds)) //
            .toString();
}