尝试将增量方法添加到时间类

时间:2017-08-17 18:30:50

标签: java

我正在尝试为我拥有的时间类添加增量方法。我修改了原始代码只有一个私有整数" totalseconds"它读取自午夜以来的秒数。代码的那部分工作正常,但现在我正在尝试创建一个增加setSecond,setMinute和setHour的方法。问题(我相信)我遇到的是这些set方法都是从totalseconds接收它们的值,而不是像以前那样从int小时,int分钟,int秒。当我按照现在的方式运行测试时,我会在第36,75行(当试图通过Tick方法增加秒数时)和101(再次仅通过Tick方法增加秒数时)出错。我已经附加了Time2类和Time2Test应用程序以供参考。

    public class Time2 {
    private int totalseconds;
    //no argument constructor
    public Time2()
    {
        this(0,0,0); //invoke constructor with three arguments default to 0
    }

    //constructor with hour supplied minute and second default to 0
    public Time2(int hour)
    {
        this(hour, 0, 0); //invoke constructor with 3 args
    }

    //constructor with hour and minute supplied seconds default to 0
    public Time2(int hour, int minute)
    {
        this(hour, minute, 0); //invoke constructor with 3 args
    }

    //Time2 constructor with hour minute and second supplied also tests

    public Time2(int hour, int minute, int second)
    {       
        this.totalseconds = (hour * 3600);
        this.totalseconds += (minute * 60);
        this.totalseconds += (second);
    }

    public Time2(Time2 time)
    {
        //invoke constructor with 2 args
        this(time.getHour(), time.getMinute(), time.getSecond());
    }

    // SET and GET methods start here, also Universal time conversion and check
    public void setTime(int hour, int minute, int second) 
    {
        if (hour < 0 || hour >= 24)
            throw new IllegalArgumentException("Hour must be 0-23");
        if (minute < 0 || minute >= 59)
            throw new IllegalArgumentException("Minute must be 0-59");
        if (second < 0 || second >= 59)
            throw new IllegalArgumentException("Hour must be 0-59");

         this.totalseconds = (hour * 3600);
         this.totalseconds += (minute * 60);
         this.totalseconds += second;
    }

    //validate and set hour
    public void setHour(int hour)
    {
        if (hour < 0 || hour >= 23)
            throw new IllegalArgumentException("Hour must be 0-23");
        this.totalseconds = (hour * 3600);
    }

    //validate and set minute
    public void setMinute(int minute)
    {
        if (minute < 0 || minute >= 59)
            throw new IllegalArgumentException("Minute must be 0-59");
        this.totalseconds += (minute * 60);
    }

    //validate and set second
    public void setSecond(int second)
    {
        if (second < 0 || second >= 59)
            throw new IllegalArgumentException("Second must be 0-59");
        this.totalseconds += second;
    }
    //Get Methods start here

    //Get hour
    public int getHour()
    {
        return totalseconds / 3600;
    }

    //get minute
    public int getMinute()
    {
        return (totalseconds - (3600 * getHour())) / 60;
    }

    //get second
    public int getSecond()
    {
        return totalseconds - (3600 * getHour())- (60 * getMinute());
    }

    //Assignment 1-2 tick methods start here.
    public void Tick()
    {
        setSecond(totalseconds ++);
        if (totalseconds >= 59) incrementMinute();
    }
    public void incrementMinute()
    {
        setMinute( totalseconds ++);
        if ( totalseconds >= 59) incrementHour();
    }
    public void incrementHour()
    {
        setHour ( this.totalseconds ++);
    }
    //convert our string to universal format (HH:MM:SS)
    public String ToUniversalString()
    {
        return String.format(
        "%02d:%02d:%02d", getHour(), getMinute(), getSecond());
    }

    //conver to standard format (H:MM:SS AM or PM)
    public String toString()
    {
        return String.format("%d:%02d:%02d %s",((getHour() == 0 || getHour() ==
        12) ? 12 : getHour() % 12), getMinute(), getSecond(), (getHour()
        < 12 ? "AM" : "PM"));
    }

}//end class Time2



    public class Time2Test 
{
    public static void main(String[] args)
    {
        Time2 t1 = new Time2(); //00:00:00
        Time2 t2 = new Time2(2); //02:00:00 
        Time2 t3 = new Time2(21, 34); //21:34:00
        Time2 t4 = new Time2(12, 25, 42); //12:25:42
        Time2 t5 = new Time2(t4); //12:25:42

        System.out.println("Constructed with:");
        displayTime("t1: all default arguments", t1);
        displayTime("t2: hour specified; defaults for minute and second", t2);
        displayTime("t3: hour and minute supplied second defaulted", t3);
        displayTime("t4: hour minute and second supplied", t4);
        displayTime("t5: Time2 object t4 specified", t5);
        //attempt to initialize t6 with invalid args
        try
        {
            Time2 t6 = new Time2(27,74,99); //all invalid values
        }
        catch (IllegalArgumentException e)
        {
            System.out.printf("%nException while initializing t6: %s%n",
                    e.getMessage());
        }


        System.out.println("Time before increment minute method");
        System.out.printf("%s\n", t4.toString());

        t4.Tick();
        t4.incrementMinute();
        t4.incrementHour();

        System.out.println("Time after increment minute method");
        System.out.printf("%s\n", t4.toString());
    }
    //display Time2 object in 24 hour and 12 hour formats
    private static void displayTime(String header, Time2 t)
    {
        System.out.printf("%s%n   %s%n   %s%n", header, t.ToUniversalString(),
                t.toString());
    }
}

0 个答案:

没有答案