Time2Test(通过用户输入)

问题描述 投票:0回答:1

我正在寻找一些帮助,以完成涉及用户输入的测试类的Time2分配。分配是:修改Time2类(如下),以将时间实现为自午夜以来的秒数。该类应具有一个数据字段(一个具有自午夜以来的秒数的int值),而不是三个。此更改不应影响公共方法的参数,行为或输出。使用主要方法创建一个Driver类来测试您的Time2类。该程序应要求用户输入午夜之后的小时,分​​钟和秒数,创建一个Time2对象并使用mutator方法。然后,程序应使用toString()方法打印出时间。我相信唯一需要修改的是3个整数(小时,分钟,秒),以成为一个整数(总秒数)和set / get方法。我相信我的计算是正确的,但是我的考试班是我绊脚石的地方。我想知道在toString方法和toUniversalString之间是否存在问题。我可以让测试类请求用户输入,但它会一直输出12:00:00 AM。

public class Time2 {
    private int totalseconds;

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

    public Time2(Time2 time)setTime
    {
        this(time.getHour(), time.getMinute(), time.getSecond());
    }

    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);
    }

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

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

    public void setSecond(int second)
    {
        if (second < 0 || second >= 24)
            throw new IllegalArgumentException("Second must be 0-59");
        this.totalseconds = (second);
    }

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

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

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

    public String toUniversalString()
    {
        return String.format(
        "%02d:%02d:%02d", getHour(), getMinute(), getSecond());
    }

    public String toString()
    {
        return String.format("%d:%02d:%02d %s",((getHour() == 0 || getHour() ==
        12) ? 12 : getHour() % 12), getMinute(), getSecond(), (getHour()
        < 12 ? "AM" : "PM"));
    }
}

测试类

 import java.util.Scanner;

  public class Time2Test {
     public static void main(String[] args) { // instantiate CommissionEmployee object

       Scanner input = new Scanner(System.in);
       System.out.print("Enter hour:");
       String hour = input.next();
       Integer.parseInt(hour);
       System.out.print("Enter minute:");
       String minute = input.next();
       Integer.parseInt(minute);
       System.out.print("Enter second:");
       String second = input.next();
       Integer.parseInt(second);

       Time2 clock = new Time2(0, 0, 0);
       System.out.printf(clock.toString());
     }
  }
java testing this tostring
1个回答
0
投票

您使用的是错误的构造函数。您有一系列伸缩式构造函数,它们的作用似乎比混淆自己更大。您可以像这样创建Clock的新实例:

Time2 Clock = new Time2();

您真正想要的是您的另一个构造函数:

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

但是,在调用它之前,请注意它接受int输入,因此您将不得不使用parseIntvalueOf解析来自扫描仪的String输入。无论哪种情况,您都可以获取NumberFormatException,必须将其捕获。

[之后,请注意您的构造函数缺少对setTime()方法的调用。您将获得12:00:00,因为您从未使用用户输入来设置时间。

然后,再次注意,在构造函数中,您有一系列赋值运算符(=),实际上,分和秒的运算符应为+=

这些东西都不是真正的技术性内容,但它很草率。有时,当您陷入困境时,您需要走开然后做点其他事情。然后返回并重新查看代码。坚持下去,但要休息一下。这些东西很快就会成为第二天性。

注意:请勿以大写字母开头的实例变量,例如Clock。首字母大写保留给实际的类名。保持实例名称的大小写(小写的首字母,大写字母表示第二个及后续单词的变量名,例如This)。

© www.soinside.com 2019 - 2024. All rights reserved.