java - 如何在我的代码行中正确设置totalSeconds()(小时分钟秒)并返回它

标签 java methods constructor getter-setter

嘿,我的老师给了我一个时钟程序来制作,它必须返回信息等,到目前为止我对总秒数应该如何工作有点困惑,我将在下面展示我的驱动程序和正常文件,请帮助。我必须确保小时、分钟和秒以总秒数返回。我将发布对代码所做的实时编辑

我尝试通过添加变量并将其添加到驱动程序中来返回它,但没有成功。

{

  //Instance Variables
  private int Hours;
  private int Minutes;
  private int Seconds;
  private double Cost;
  private boolean IsOn;
  private int DLS;



  //Zero arg constructors
  public Clock()
  { // Start
    Hours = 10;
    Minutes = 52;
    Seconds = 23;   
    Cost = 20.00;
    IsOn = true;
    DLS = 11;
  } // End

  //Multi-Argument Constructors

  public Clock( int Hours, int Minutes, int Seconds, double Cost , boolean IsOn, int DSL)
  { // Start
    this.Hours = Hours;
    this.Minutes = Minutes;
    this.Seconds = Seconds;
    this.Cost = Cost;
    this.IsOn = IsOn;
    this.DLS = DLS;
  } // End

  public void setTime(int Hours)
  {
    System.out.println(Hours + 1);
  }

  public int convertDaylightSaving(int Hours)
  {
    return Hours;
  }



  //ToString Method

  public String toString()
  { //Start
    String output;
    output = "When I checked my watch the hour was: " + Hours + "\n" + 
      "When I checked my watch the minute was: " + Minutes + "\n" +
      "When I checked my watch the seconds were: " + Seconds + "\n" +
      "The Cost is: " + Cost + "\n" +
      "Is this the time right now?: " + IsOn;

    return output;

  }
} // End

public class ClockDriver
{ 
 public class ClockDriver
{ // Start

  public static void main(String[] args)
  { // Officially Start

    Clock Watch = new Clock( 11, 04, 16, 35.99, false, 11);
    System.out.println(Watch);
  } // End
} // Officially End

预期的结果是它打印出我所做的所有内容,结果是它出错了。

最佳答案

好吧,我实际上通过执行以下操作回答了我自己的问题。



public class Clock
{

  //Instance Variables
  private int Hours;
  private int Minutes;
  private int Seconds;
  private double Cost;
  private boolean IsOn;
  private int DLS;
  int totalSeconds; 



  //Zero arg constructors
  public Clock()
  { // Start
    Hours = 10;
    Minutes = 52;
    Seconds = 23;   
    Cost = 20.00;
    IsOn = true;
    DLS = 11;
    totalSeconds = 9945;
  } // End

  //Multi-Argument Constructors

  public Clock( int Hours, int Minutes, int Seconds, double Cost , boolean IsOn, int DSL, int totalSeconds)
  { // Start
    this.Hours = Hours;
    this.Minutes = Minutes;
    this.Seconds = Seconds;
    this.Cost = Cost;
    this.IsOn = IsOn;
    this.DLS = DLS;
    this.totalSeconds = totalSeconds;
  } // End

  public void setTime(int Hours)
  {
    System.out.println(Hours + 1);
  }

  public int convertDaylightSaving(int Hours)
  {
    return Hours;
  }
    public int totalSeconds()
    {
      return totalSeconds + 5000;
    }


  //ToString Method

  public String toString()
  { //Start
    String output;
    output = "When I checked my watch the hour was: " + Hours + "\n" + 
      "When I checked my watch the minute was: " + Minutes + "\n" +
      "When I checked my watch the seconds were: " + Seconds + "\n" +
      "The Cost is: " + Cost + "\n" +
      "Is this the time right now?: " + IsOn + "\n" + 
      "The total seconds right now are: " + totalSeconds;


    return output;

  }
} // End

之后我制作了驱动程序并将其返回

public class ClockDriver
{ // Start

  public static void main(String[] args)
  { // Officially Start

    Clock Watch = new Clock( 11, 04, 16, 35.99, false, 11, 9945);
    System.out.println(Watch);
}
  } // End

关于java - 如何在我的代码行中正确设置totalSeconds()(小时分钟秒)并返回它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58560761/

相关文章:

class - 在示例中阅读 Scala,试图理解示例背后的哲学

java - Android中如何避免跨类重复代码?

Python - 返回构造函数参数名称而不实例化

c++ - 从同一文件构造基类和继承类

java 下载的 zip 文件已损坏

java - php shell_exec ("java -version")返回 null

node.js - Strongloop(环回)类方法

从子类调用方法的 JUnit 案例中的 Java 失败

java - Switch 语句和不兼容的类型?

java - 在 Java 中解析字符串以用特定数据替换标记