为什么我不通过测试仪?

时间:2014-03-31 15:59:53

标签: java

它编写了两个类--Time1和BusArrival(BusArrival使用类Time1)。但是BusArrival类的测试人员在“public boolean before”和“public int elapsedTime”方法中给出了错误的答案。我真的想弄清楚但失败了。我假设问题出在方法“public boolean before”。

类Time1:

public class Time1 {
final int SECONDS_PER_MINUTE = 60,     // the num of seconds per minute
          MINUTES_PER_HOUR = 60,        // the num of minutes per hour
          DEFAULT_SECOND = 0,
          DEFAULT_MINUTE = 0,
          DEFAULT_HOUR = 0;

private int _hour,                  // the hour
            _minute,                // the minutes
            _second;                // the seconds




//constructors:

    /**  Constructs a Time1 object. 
     *   Construct a new time instance with the specified hour, minute and second.
     *   hour should be between 0-23, otherwise it should be set to 0. 
     *   minute and second should be between 0-59, otherwise they should be set to 0. 
     *   Creates a new Time1 object.
     *   @param h The hour.
     *   @param m The minutes.
     *   @param s The seconds.
     */

    public Time1 (int h, int m, int s) {
        if (hourIsValid (h))
            _hour = h;
        else _hour = DEFAULT_HOUR;

        if (minuteIsValid (m))
            _minute = m;
        else _minute = DEFAULT_MINUTE;

        if (secondIsValid (s))
            _second = s;
        else _second = DEFAULT_SECOND;
    }


    /**  Copy constructor for Time1. Construct a time with the same variables as another time. 
     *   Copy constructor for Time1.
     *   @param other Time to be copied.
     */   

    public Time1 (Time1 other) {
        if (other != null) {
          this._hour = other._hour;
          this._minute = other._minute;
          this._second = other._second;
        }
        else {
            this._hour = DEFAULT_HOUR;
            this._minute = DEFAULT_MINUTE;
            this._second = DEFAULT_SECOND;  
        }
        }

    //get methods    
    /** 
     *   Gets the hour of the time
     *   @return The hour of the time
     */

    public int getHour() {
        return _hour;
    }

    /** 
     *   Gets the minutes of the time. 
     *   @return The minutes of the time.
     */

    public int getMinute() {
        return _minute;
    }

    /** 
     *   Gets the seconds of the time.
     *   @return The seconds of the time.
     */ 

    public int getSecond() {
        return _second;
    }

    //set methods

    /** Changes the hour of the time. If illegal number is received hour will remain unchanged. 
     *  Sets the hour of the time.
     *  @param num The value to be set.
     */ 

    public void setHour (int num) {
        if (hourIsValid (num))
            _hour = num;
    }

    /** Changes the minute of the time. If illegal number is received minute will remain unchanged. 
     *  Sets the minutes of the time.
     *  @param num The value to be set.
     */   

    public void setMinute (int num) {
        if (minuteIsValid (num))
            _minute = num;
    }

    /** Changes the second of the time. If illegal number is received second will remain unchanged. 
     *  Sets the seconds of the time.
     *  @param num The value to be set.
     */   

    public void setSecond (int num) {
        if (secondIsValid (num))
            _second = num;
    }

    /**
     * Return a string representation of this time (hh:mm:ss).
     * @return String representation of time (hh:mm:ss).
     */

    public String toString () {
        String strH,
               strM,
               strS;

        if (_hour < 10)
            strH = "0" + _hour + ":";
        else strH = _hour + ":";

        if (_minute < 10)
            strM = "0" + _minute + ":";
        else strM = _minute + ":";

        if (_second < 10)
            strS = "0" + _second;
        else strS = ""+_second;

        return (strH + strM + strS);
    }

    /** 
     *  Checks if this time is equal to the received time.
     *  @param other The time to compare this time to.
     *  @return True if the received time is equal to this time.
     */ 

    public boolean equals (Time1 other) {
        if (other == null)
             return false;
         return ((_hour == other._hour) && (_minute == other._minute) && (_second == other._second));
     }

    /** 
     *  Checks if this time is before the received time.
     *  @param other The time to check if this time is before.
     *  @return True if this time is before the other.
     */ 

    public boolean before (Time1 other) {
         return ((_hour < other._hour) || 
                 ((_hour == other._hour) && (_minute < other._minute)) || 
                 ((_hour == other._hour) && (_minute == other._minute) && (_second < other._second)));
     }

    /** 
     *  Checks if this time is after the received time.
     *  @param other The time to check if this time is after.
     *  @return True if this time is after the other.
     */ 

    public boolean after (Time1 other) {
         return other.before(this);
     }

    /** 
     *  Calculates the difference (in seconds) between 2 times.
     *  @param The time to check the difference with. Assumption: this time is after other time.
     *  @return The different between 2 times (in seconds). 
     */ 

    public int difference (Time1 other) {

         int obSec = 0, otherSec = 0;

         obSec = ((_hour * MINUTES_PER_HOUR * SECONDS_PER_MINUTE) + (_minute * SECONDS_PER_MINUTE) + _second);
         otherSec = ( (other._hour * MINUTES_PER_HOUR * SECONDS_PER_MINUTE) + (other._minute * SECONDS_PER_MINUTE) + other._second);

         return (obSec - otherSec);

     }

//===================================================================================================================

    /** 
     *  Checks if the hour is valid.
     *  @param h The hour. 
     *  @return True if the hour is valid.
     */ 

    private boolean hourIsValid (int h) {
        return (h <= 23 && h >= 0);
    }

    /** 
     *  Checks if the minutes are valid.
     *  @param m The minutes. 
     *  @return True if the minutes are valid.
     */ 

    private boolean minuteIsValid (int m) {
        return (m <= 59 && m >= 0);
    }

    /** 
     *  Checks if the seconds are valid.
     *  @param m The seconds. 
     *  @return True if the seconds are valid.
     */ 

    private boolean secondIsValid (int s) {
        return (s <= 59 && s >= 0);
    }

    }  // end of class Time1 

班级BusArrival

    class BusArrival {

    final int MAX_PAS = 70,                 // the max num of passengers
              MIN_PAS = 0,                  // the min num of passengers
              MAX_LINE_NUM = 99,            // the max num of the line
              MIN_LINE_NUM = 1,             // the min num of the line
              SECONDS_PER_MINUTE = 60,      // the num of seconds per minute
              DEFAULT_SECOND = 0,
              DEFAULT_MINUTE = 0,
              DEFAULT_HOUR = 0;

    private int _lineNumber,                // the line num
                _noOfPassengers;            // the num of passengers
    private Time1 _arrivalTime;             // the arrival time

    //constructors:

    /**  Constructor a BusArrival object.
     *   with line number, number of passengers, and time (hour, minute and second) of arrival.
     *   if the parameters are illegal they will be set to 0. 
     *   Creates a new BusArrival object.
     *   @param lineNum The number of the line (should be between 1-99).
     *   @param pass The number of passengers (should be between 0-70).
     *   @param h The hour (should be between 0-23).
     *   @param m The minutes (should be between 0-59).
     *   @param s The seconds (should be between 0-59).
     */

    public BusArrival (int lineNum, int pass, int h, int m, int s) {

         _arrivalTime = new Time1 (h, m, s);

        if (passIsValid (pass))
            _noOfPassengers = pass;
        else _noOfPassengers = MIN_PAS;

        if (lineIsValid (lineNum))
            _lineNumber = lineNum;
        else lineNum = MIN_LINE_NUM;

    }

    /**  Constructor a BusArrival object.
     *   with line number, number of passengers, and time of arrival. 
     *   if the parameters are illegal they will be set to 0. 
     *   Creates a new BusArrival object.
     *   @param lineNum The number of the line (should be between 1-99).
     *   @param pass The number of passengers (should be between 0-70).
     *   @param t The time of bus arrival.
     */

     public BusArrival (int lineNum, int pass, Time1 t) {

         _arrivalTime = new Time1 (DEFAULT_HOUR ,DEFAULT_MINUTE ,DEFAULT_SECOND);

            if (passIsValid (pass))
                _noOfPassengers = pass;
            else _noOfPassengers = MIN_PAS;

            if (lineIsValid (lineNum))
                _lineNumber = lineNum;
            else lineNum = MIN_LINE_NUM;
     }

    /**  Copy constructor for a BusArrival. Construct a BusArrival with the same attributes as another BusArrival. 
     *   Copy constructor for a BusArrival.
     *   @param other Bus arrival to be copied.
     */   

     public BusArrival (BusArrival other) {
         if (other != null) {
             this._arrivalTime = other._arrivalTime;
             this._lineNumber = other._lineNumber;
             this._noOfPassengers = other._noOfPassengers;
         }
         else {
             this._arrivalTime = new Time1 (DEFAULT_HOUR ,DEFAULT_MINUTE ,DEFAULT_SECOND);
             this._lineNumber = MIN_LINE_NUM;
             this._noOfPassengers = MIN_PAS;
         }
     }

     //get methods

    /** 
     *   Gets the bus arrival time. 
     *   @return The bus arrival time.
     */

     public Time1 getArrivalTime() {
         return _arrivalTime;
     }

    /**
     *   Gets the bus line number. 
     *   @return The bus line number.
     */

     public int getLineNum() {
         return _lineNumber;
     }

    /**
     *   Gets the number of passengers. 
     *   @return The number of passengers.
     */

     public int getNoOfPass() {
         return _noOfPassengers;
     }

    //set methods

    /** 
     *  Sets the BusArrival's time.
     *  @param t The value to be set.
     */   


     public void setArrivalTime (Time1 t) {
         _arrivalTime = t;
     }

    /** 
     *  Sets the BusArrival's line number.
     *  @param num The value to be set.
     */   

     public void setLineNum (int num) {
         if (lineIsValid (num))
         _lineNumber = num;
     }

    /** 
     *  Sets  the BusArrival's number of passengers.
     *  @param num The value to be set.
     */   

     public void setNoOfPass (int num) {
         if (passIsValid (num))
             _noOfPassengers = num;
     }

    /** 
     *  Checks if the received BusArrival is equal to this BusArrival.
     *  @param other The BusArrival to be compared with this BusArrival. 
     *  @return True if the received BusArrival is equal to this BusArrival.
     */

     public boolean equals (BusArrival other) {
         if (other == null)
             return false;
         return ((_arrivalTime == other._arrivalTime) && (_lineNumber == other._lineNumber) 
                 && (_noOfPassengers == other._noOfPassengers));
     }

    /**
     * Returns a string representation of this BusArrival (for example: "Bus no. 27 arrived at 09:24:10 with 13 passengers").
     * @return String representation of this BusArrival (for example: "Bus no. 27 arrived at 09:24:10 with 13 passengers").
     */

     public String toString() {
         String strLine,
                strArrival,
                strPass;
         strLine = "Bus no. " + _lineNumber + " arrived at ";
         strArrival = _arrivalTime.toString ();
         strPass = " with " + _noOfPassengers + " passengers";

         return (strLine + strArrival + strPass);
     }

    /** 
     *  Checks if this bus's number of passengers is bigger than other bus's number of passengers.
     *  @param other  The BusArrival to be compared with this BusArrival.     
     *  @return True if this bus's number of passengers is larger than other bus's number of passengers. false otherwise.
     */ 

     public boolean fuller (BusArrival other) {
         return (_noOfPassengers > other._noOfPassengers);
     }

    /** 
     *  Checks if this bus's arrival time is before other bus's arrival time.
     *  @param other The bus arrival to check if this bus arrival is before   
     *  @return True if this bus's arrival time is before other bus's arrival time. false otherwise.
     */ 

     public boolean before (BusArrival other) {
         return (_arrivalTime.before (other._arrivalTime));
     }

    /**
     *  Checks if this bus's number of passengers is equal to the maximum number of passengers allowed.
     *  @return True if this bus's number of passengers is equal to the maximum number of passengers allowed. false otherwise.
     */ 

     public boolean isFull() {
         return (_noOfPassengers == MAX_PAS);
     }

    /**
     *   Calculates the difference (in minutes) between this bus arrival time and other.
     *   @param other The time to check the difference with.    
     *   @return The difference in minutes.
     */

    public int elapsedTime (BusArrival other) {
         int secDif,
             minDif;

         if (this.before(other)) 
             secDif = (other._arrivalTime).difference (this._arrivalTime);

         else  secDif = (this._arrivalTime).difference (other._arrivalTime);

         minDif = (secDif / SECONDS_PER_MINUTE);
         return minDif;
     }

    //===========================================================================================================

    /** 
     *  Checks if the passengers number is valid.
     *  @param pass The passengers. 
     *  @return True if the passengers number is valid.
     */

    private boolean passIsValid (int pass) {
        return (pass >= MIN_PAS && pass <= MAX_PAS);

    }

    /** 
     *  Checks if the line number is valid.
     *  @param line The line number. 
     *  @return True if the line number is valid.
     */

    private boolean lineIsValid (int line) {
        return (line >= MIN_LINE_NUM && line <= MAX_LINE_NUM);
    }
}   // end of class BussArrival

测试人员:

 public class BusArrivalDriver {
        public static void main(String[] args) {
            BusArrival firstBusArrival = new BusArrival(20, 55, 10, 0, 0);

            Time1 time1 = new Time1(16, 15, 0);
            BusArrival secondBusArrival = new BusArrival(18, 30, time1);

            BusArrival thirdBusArrival = new BusArrival(secondBusArrival);

            System.out.println("The line number of the first BusArrival object is: " + firstBusArrival.getLineNum());
            System.out.println("The number of passengers of the first BusArrival object is: " + firstBusArrival.getNoOfPass());
            System.out.println("The arrival time of the first BusArrival object is: " + firstBusArrival.getArrivalTime());

            firstBusArrival.setLineNum(35);
            firstBusArrival.setNoOfPass(24);
            firstBusArrival.setArrivalTime(new Time1(12, 20, 15));

            System.out.println("The line number of the first BusArrival object after re-setting it is: " + firstBusArrival.getLineNum());
            System.out.println("The number of passengers of first BusArrival object after re-setting it is: " + firstBusArrival.getNoOfPass());
            System.out.println("The arrival time of the first BusArrival object after re-setting it is: " + firstBusArrival.getArrivalTime());

            System.out.println("Is the first bus full? " + firstBusArrival.isFull());

            System.out.println("Is the second and third BusArrival objects equal? " + secondBusArrival.equals(thirdBusArrival));

            System.out.println("Is the second BusArrival before the first BusArrival? " + secondBusArrival.before(firstBusArrival));

            System.out.println("Is the third BusArrival fuller than the first BusArrival? " + thirdBusArrival.fuller(firstBusArrival));

            System.out.println("The elapsed time between the second and the first BusArrival objects is: " + secondBusArrival.elapsedTime(firstBusArrival));

            System.out.println("The string representation of the first BusArrival object is: " + firstBusArrival.toString());
        }
    }

测试人员应该输出的内容:

The line number of the first BusArrival object is: 20
The number of passengers of the first BusArrival object is: 55
The arrival time of the first BusArrival object is: 10:00:00
The line number of the first BusArrival object after re-setting it is: 35
The number of passengers of first BusArrival object after re-setting it is: 24
The arrival time of the first BusArrival object after re-setting it is: 12:20:15
Is the first bus full? false
Is the second and third BusArrival objects equal? true
Is the second BusArrival before the first BusArrival? false
Is the third BusArrival fuller than the first BusArrival? true
The elapsed time between the second and the first BusArrival objects is: 234
The string representation of the first BusArrival object is: Bus no. 35 arrived at 12:20:15 with 24 passengers

我的输出:

The line number of the first BusArrival object is: 20
The number of passengers of the first BusArrival object is: 55
The arrival time of the first BusArrival object is: 10:00:00
The line number of the first BusArrival object after re-setting it is: 35
The number of passengers of first BusArrival object after re-setting it is: 24
The arrival time of the first BusArrival object after re-setting it is: 12:20:15
Is the first bus full? false
Is the second and third BusArrival objects equal? true
Is the second BusArrival before the first BusArrival? true
Is the third BusArrival fuller than the first BusArrival? true
The elapsed time between the second and the first BusArrival objects is: 740
The string representation of the first BusArrival object is: Bus no. 35 arrived at 12:20:15 with 24 passengers

1 个答案:

答案 0 :(得分:0)

问题在于你的第二个构造函数:

public BusArrival (int lineNum, int pass, Time1 t)

您没有使用输入参数t来设置到达时间。 你将不得不做一些事情:

_arrivalTime = new Time1 (t.HOUR , t.MINUTE , t.SECOND);`