如何将实现从 3 个时间变量(小时、分钟、秒)更改为仅由秒组成的单个变量



现在我有 3 个变量存储时间(小时、分钟、秒(,我有与这些变量对应的构造函数和 getter。我对如何使用单个 int 变量来存储总秒数来代替小时、分钟和秒变量感到非常困惑。谁能帮我开始?我主要对重新排列构造函数和 getter 以便使用单个变量(秒(正确执行感到困惑。我希望我的要求是明确的,我只需要朝着正确的方向推动,非常感谢。

public class Relay 
{
    public static void main(String[] args) 
    {
       TimeB[] raceLegs = new TimeB[3];
       raceLegs[0] = new TimeB(903);
       raceLegs[1] = new TimeB(0,1,43);
       raceLegs[2] = new TimeB(0,45,17);
       System.out.println("First runner:  " + raceLegs[0].toString());
       System.out.println("Second runner: " + raceLegs[1].toString());
       System.out.println("Third runner:  " + raceLegs[2].toString());
       raceLegs[0].add( raceLegs[1] );
       raceLegs[0].add( raceLegs[2] );
       System.out.println("The Sum of all runners is:  " + raceLegs[0].toString());
    }

}       

public class TimeB implements Time {
    private int hours;
    private int minutes;
    private int seconds;
    private int totalSeconds;
    /**
     * Simple constructor assumes data is in proper format
     * 
     * @param h - number of hours
     * @param m - number of minutes
     * @param s - number of seconds
     */
    public TimeB(int h, int m, int s) {
        hours = h;
        minutes = m;
        seconds = s;

    }
//formula to calculate total sum of runners
    public void addTime(TimeB other) {
        this.hours += other.hours;
        this.minutes += other.minutes;
        this.seconds += other.seconds;
    }
    /**
     * Return the number of leftover seconds (those not part of a full minute)
     * in this object
     * 
     * @return the number of seconds
     */
    public int getSeconds() {
        return seconds;
    }
    /**
     * Return the number of leftover minutes (those not part of a full hour) in
     * this object
     * 
     * @return the number of minutes
     */
    public int getMinutes() {
        return minutes;
    }
    /**
     * Return the number of full hours in this object
     * 
     * @return the number of hours
     */
    public int getHours() {
        return hours;
    }
    /**
     * Constructor that assumes a total number of seconds
     * 
     * @param total - the total number of seconds taken
     */
//  public TimeB(int total) {
//      hours = total / 3600;
//      minutes = (total / 60) % 60;
//      seconds = total % 60;
//  }
    public TimeB(int total) {
    hours = totalSeconds / 3600;
    minutes = (totalSeconds % 3600) / 60;
    seconds = totalSeconds % 60;
    }
    /**
     * Adds the given time to the current time, producing the sum
     * 
     * @param other - the given time to add
     * @return the sum of this time and the other time
     */
    public Time add(Time other) {
        int remainder = 0;
        int newSec = this.seconds + other.getSeconds();
        // if over a minute, carry.
        if (newSec >= 60) {
            remainder = 1;
            newSec -= 60;
        }
        this.seconds = newSec;
        int newMin = this.minutes + remainder + other.getMinutes();
        remainder = 0;
        // carry if over an hour
        if (newMin >= 60) {
            remainder = 1;
            newMin -= 60;
        }
        this.minutes = newMin;
        this.hours += other.getHours() + remainder;
        return this;
    }
    /**
     * Return a String representation of this time
     * 
     * @return this time represented as a String in hh:mm:ss format
     */
//  public String toString() {
//      return String.format("%d:%02d:%02d", hours, minutes, seconds);
//
//       return hours + ":" + minutes + ":" + seconds;
//  }
    public String toString() {
    return String.format("%02d",seconds);
    }
    public int compareTo(Time other) {
        return 17;
    }
    /**
     * Constructor that initializes TimeA object in string format
     * @param t
     */
    public TimeB(String t) {
        hours = Integer.parseInt(t.substring(0,t.indexOf(':')));
        minutes = Integer.parseInt(t.substring(t.indexOf(':') + 1,t.lastIndexOf(':')));
        seconds = Integer.parseInt(t.substring(t.lastIndexOf(':') + 1,t.length()));
    }
}

public interface Time extends Comparable<Time> {
    /**
     * Return the number of leftover seconds (those not part of a full minute) 
     *      in this object
     * @return the number of seconds
     */
    public int getSeconds();
    /**
     * Return the number of leftover minutes (those not part of a full hour) 
     *      in this object
     * @return the number of minutes
     */
    public int getMinutes();
    /**
     * Return the number of full hours in this object
     * @return the number of hours
     */
    public int getHours();
    /**
     * Return a String representation of this object in hh:mm:ss format
     * @return this object as a String 
     */
    public String toString();
    /**
     * Add the given time record to this one
     * @param other the time record to be added to this one
     * @return the sum of this time record and the other
     */
    public Time add(Time other);
}
//my attempt
public class TimeB implements Time {
//  private int hours;
//  private int minutes;
//  private int seconds;
    private int totalSeconds;
    /**
     * Simple constructor assumes data is in proper format
     * 
     * @param h - number of hours
     * @param m - number of minutes
     * @param s - number of seconds
     */
    public TimeB(int h, int m, int s) {
        totalSeconds = h;
        totalSeconds = m;
        totalSeconds = s;

    }
//formula to calculate total sum of runners
    public void addTime(TimeB other) {
        this.totalSeconds += other.totalSeconds;
        this.totalSeconds += other.totalSeconds;
        this.totalSeconds += other.totalSeconds;
    }
    /**
     * Return the number of leftover seconds (those not part of a full minute)
     * in this object
     * 
     * @return the number of seconds
     */
    public int getSeconds() {
        return totalSeconds % 60;
    }
    /**
     * Return the number of leftover minutes (those not part of a full hour) in
     * this object
     * 
     * @return the number of minutes
     */
    public int getMinutes() {
        return (totalSeconds / 60) % 60;
    }
    /**
     * Return the number of full hours in this object
     * 
     * @return the number of hours
     */
        public int getHours() {
          return totalSeconds / 3600;
        }
    /**
     * Constructor that assumes a total number of seconds
     * 
     * @param total - the total number of seconds taken
     */
//  public TimeB(int total) {
//      hours = total / 3600;
//      minutes = (total / 60) % 60;
//      seconds = total % 60;
//  }
    public TimeB(int total) {
    totalSeconds = totalSeconds / 3600;
    totalSeconds = (totalSeconds % 3600) / 60;
    totalSeconds = totalSeconds % 60;
    }
    /**
     * Adds the given time to the current time, producing the sum
     * 
     * @param other - the given time to add
     * @return the sum of this time and the other time
     */
    public Time add(Time other) {
        int remainder = 0;
        int newSec = this.totalSeconds + other.getSeconds();
        // if over a minute, carry.
        if (newSec >= 60) {
            remainder = 1;
            newSec -= 60;
        }
        this.totalSeconds = newSec;
        int newMin = this.totalSeconds + remainder + other.getMinutes();
        remainder = 0;
        // carry if over an hour
        if (newMin >= 60) {
            remainder = 1;
            newMin -= 60;
        }
        this.totalSeconds = newMin;
        this.totalSeconds += other.getHours() + remainder;
        return this;
    }
    /**
     * Return a String representation of this time
     * 
     * @return this time represented as a String in hh:mm:ss format
     */
    public String toString() {
//      return String.format("%d:%02d:%02d", hours, minutes, seconds);
        return String.format("%d:%02d:%02d", getHours(), getMinutes(), getSeconds());
         return hours + ":" + minutes + ":" + seconds;
    }

    public int compareTo(Time other) {
        return 17;
    }
    /**
     * Constructor that initializes TimeA object in string format
     * @param t
     */
    public TimeB(String t) {
        hours = Integer.parseInt(t.substring(0,t.indexOf(':')));
        minutes = Integer.parseInt(t.substring(t.indexOf(':') + 1,t.lastIndexOf(':')));
        seconds = Integer.parseInt(t.substring(t.lastIndexOf(':') + 1,t.length()));
    }
}

涉及到重构时,过程通常是这样的:

  1. 旧变量交换为新变量。
  2. 看看有什么坏处。
  3. 更新损坏的代码以使用新变量。
  4. 重复 2 和 3,直到您的代码运行,并执行您希望它执行的操作!

当你第一次开始重构时,它可能有点令人生畏,但一旦你开始,它通常会自行解决。最终,您必须决定希望新代码的外观以及希望它如何执行,因为重构通常包括对代码工作方式的一些更改。


关于您问题中的情况,这里有一些提示:

构造 函数
你可以在这里只得到一个,它只seconds作为参数。您还可以保留小时/分钟/秒构造函数并从中计算出秒数。示例如下:

private int totalSeconds;
public TimeB(int totalSeconds) {
    this.totalSeconds = totalSeconds;
}
public TimeB(int hours, int minutes, int seconds){
    this.totalSeconds = (hours * 3600) + (minutes * 60) + seconds;
}

吉特斯(和二传手(
你可以保留你当前的getSeconds()getMinutes()getHours()方法,只需一点点数学。试试这些:

public int getSeconds() {
    // Modulus to return remainder after "/ 60" (factoring out hours and mins)
    return totalSeconds % 60;
}
public int getMinutes() {
    // Divide by 60 to get to minutes (factoring out seconds), then modulus to return remainder after "/ 60" (factoring out hours)
    return (totalSeconds / 60) % 60;
}
public int getHours() {
    // Divide by 60 to get to minutes (factoring out seconds), then by 60 again convert to hours
    return (totalSeconds / 60) / 60;
}

添加两个时间 B
现在这很容易!您只需要获得总秒数,而不必担心携带任何东西。假设您有一个返回总秒数的 getTotalSeconds() 方法,则 add 方法将如下所示:

public Time add(Time other) {
    totalSeconds = totalSeconds + other.getTotalSeconds();
    return this;
}

最新更新