如何通过Java

时间:2016-10-23 16:16:03

标签: java constructor enumeration

我正试图让这个飞机项目工作,我不能为我的生活弄清楚如何在“public void addHours()”中添加时间,或者如果我甚至做了前面的正确!任何帮助都会很棒!

要回答的问题:

  

创建一个Time类。这个类将代表一个时间点,例如   作为出发时间。它应该包含2个构造函数,2个实例   变量(小时和分钟)和10种方法(见下文)。所有方法   但toString应该是24小时格式。默认   构造函数:为12:00 AM创建Time对象。超载   构造函数:在特定的小时和分钟创建Time对象。   getHour():返回表示时间小时的整数   宾语。 getMinute():返回表示分钟的整数   时间对象。 addHours(...):通过向前移动对象来更新对象   小时数。 addMinute(...):通过移动来更新对象   转发几分钟。 (提示:小心你不允许   分钟超过59.)addTime(...):更新对象   从另一个Time对象向前移动小时和分钟。   getCopy(...):返回一个具有相同小时和的新Time对象   现有Time对象的分钟。 isEarlierThan(...):返回true   如果此Time对象早于另一个Time对象。   isSameTime(...):如果此Time对象与之相同,则返回true   另一个Time对象。 isLaterThan(...):如果此时间返回true   对象晚于另一个Time对象。 toString():返回一个字符串   表示Time对象。使用12小时AM / PM格式和打击垫   分钟是两位数。有关示例,请参阅示例输出。

到目前为止,这是我的时间课程:

public class Time {
    public static int hour;
    public static int minute;

    public Time() {
    }
    public Time(int h, int m) {
        h = hour;
        m = minute;
    }
    public int getHour() {
        return hour;
    }
    public int getMinute() {
        return minute;
    }
    public void addHours(int h) {
        h = h + hour;
    }
    public void addMinutes(int m) {
        //Minute should not allow past 59 minutes.
        if (m <= 59)
        m = m + minute;
    }
    public void addTime(Time other) { 
    }
    public Time getCopy() {
        //need to fix return statement
        return null;
    }
    public boolean isEarlierThan(Time other) {
        return (Boolean) null;
        //need to fix return statement.
    }
    public boolean isSameTime(Time other) {
        return (Boolean) null;
        //need to fix return statement.
    }
    public boolean isLaterTHan(Time other) {
        return (Boolean) null;
        //need to fix return statement.
    }
    public String formatDigits(int i) {
        return null;
        //need to fix return statement.
    }
    public String toString() {
        return null;
    }

}

以下是驱动程序:

public class Driver {
    public static void main(String[] args) {

        //Please note that these tests are not comprehensive.
        //testAirline();
        //testAirport();
        //testPlane();
        //testTime();
        //testFlight();
    }

    public static void testAirline() {
        System.out.println("==testAirline()==");
        /*
        //Test 1: create enum variables
        Airline a1 = Airline.American;
        Airline a2 = Airline.United;
        Airline a3 = Airline.Delta;
        Airline a4 = Airline.United;

        //Test 2: display and compare variables
        System.out.println("a1: " + a1);
        System.out.println("a2 == a3: " + (a1 == a2));
        System.out.println("a2 == a4: " + (a2 == a4));
        */
    }

    public static void testAirport() {
        System.out.println("==testAirport()==");

        /*
        //Test 1: create enum variables
        Airport a1 = Airport.PHX;
        Airport a2 = Airport.LAX;
        Airport a3 = Airport.SFO;
        Airport a4 = Airport.NRT;
        Airport a5 = Airport.SIN;

        //Test 2: display and compare variables
        System.out.println("a1: " + a1);
        System.out.println("a2 == a3: " + (a1 == a2));
        System.out.println("a2 == a4: " + (a2 == a4));
        System.out.println("a1: " + Airport.getAirportCity(a1));
        System.out.println("a3: " + Airport.getAirportCity(a3));
        System.out.println("a5: " + Airport.getAirportCity(a5));
        */
    }

    public static void testPlane() {
        System.out.println("==testPlane()==");
        /*

        //Test 1: create plane objects
        Plane p1 = new Plane(Airline.Delta, "Boeing 717");
        Plane p2 = new Plane(Airline.United, "Airbus A321");

        //Test 2: display plane information.
        System.out.println(p1.getAirline());
        System.out.println(p1.getModel());
        System.out.println(p1);
        System.out.println(p2);
        */
    }

    public static void testTime() {
        System.out.println("==testTime()==");

        /*
        //Test 1: use default constructor.
        Time t1 = new Time();

        //Test 2: use overloaded constructor.
        Time t2 = new Time(9, 0);
        Time t3 = new Time(1, 15);

        //Test 3: use clone operation.
        Time t4 = t3.getCopy();

        //Test 4: run toString on AM times.
        System.out.println(new Time(0, 5));
        System.out.println(new Time(1, 15));
        System.out.println(new Time(2, 45));
        System.out.println(new Time(10, 5));
        System.out.println(new Time(11, 15));
        //Test 5: run toString on PM times.
        System.out.println(new Time(12, 45));
        System.out.println(new Time(13, 5));
        System.out.println(new Time(22, 15));
        System.out.println(new Time(23, 45));

        //Test 6: run toString on object from default constructor.
        System.out.println("t1: " + t1);

        //Test 7: testing addTime operation
        System.out.println("t2: " + t2);
        t2.addTime(t3);
        System.out.println("t2: " + t2);
        //Test 8: testing addMinutes operation
        t2.addMinutes(181);
        System.out.println("t2: " + t2);
        //Test 9: testing8 addHours operation
        t2.addHours(2);
        System.out.println("t2: " + t2);

        //Test 10: testing cloned copy.
        t4.addHours(1);
        System.out.println("t3: " + t3);//original
        System.out.println("t4: " + t4);//clone

        //Test 11: testing isEarlierThan.
        System.out.println("t3 < t4: " + t3.isEarlierThan(t4));
        System.out.println("t4 < t3: " + t4.isEarlierThan(t3));
        System.out.println("t2 < t4: " + t2.isEarlierThan(t4));
        System.out.println("t4 < t2: " + t4.isEarlierThan(t2));
        System.out.println("t2 < t2: " + t2.isEarlierThan(t2));

        //Test 12: testing isLaterThan.
        System.out.println("t2 > t4: " + t2.isLaterThan(t4));
        System.out.println("t4 > t2: " + t4.isLaterThan(t2));
        System.out.println("t4 > t4: " + t4.isLaterThan(t4));

        //Test 13: testing isSameTime.
        System.out.println("t2 = t4: " + t2.isSameTime(t4));
        System.out.println("t4 = t4: " + t4.isSameTime(t4));
        System.out.println("t4 = 2:15AM: " + t4.isSameTime(new Time(2, 15)));
        */
    }

    public static void testFlight() {
        System.out.println("==testFlight()==");
        /*
        //Test 1: create flights using different settings
        Flight f1 = new Flight(new Plane(Airline.American, "Airbus A321"), 
                               "495", 
                               79,
                               new Time(11,5), 100,
                               Airport.PHX, Airport.LAX);

        Flight f2 = new Flight(new Plane(Airline.Delta, "Boeing 717"),
                               "1063",
                               79,
                               new Time(7, 10),
                               95,
                               Airport.PHX,
                               Airport.LAX);

        Flight f3 = new Flight(new Plane(Airline.American, "Airbus A321"),
                               "400",
                               44,
                               new Time(21, 25),
                               127,
                               Airport.PHX,
                               Airport.SFO);

        Flight f4 = new Flight(new Plane(Airline.United, "Boeing 787"),
                               "400",
                               525,
                               new Time(10, 50),
                               715,
                               Airport.LAX,
                               Airport.NRT);

        Flight f5 = new Flight(new Plane(Airline.United, "Boeing 737"),
                               "414",
                               59,
                               new Time(6, 50),
                               85,
                               Airport.LAX,
                               Airport.SFO);


        System.out.println(f1.toDetailedString()); 
        System.out.println();
        System.out.println(f1.toOverviewString());

        System.out.println();
        System.out.println();

        System.out.println(f5.toDetailedString()); 
        System.out.println();
        System.out.println(f5.toOverviewString());
        */
    }
}

2 个答案:

答案 0 :(得分:2)

使用

hour += h;

而不是

h = h + hour;

同样在函数addMinutes中使用以下内容:

minute += m;
if(minute > 59){
    hour += minute/60;
    minute = minute%60;
}

同时将hourminute声明为static将导致修改这些变量将在所有实例中共享相同的值,并且在一个实例中的任何修改都将导致修改所有其他实例实例

发表评论后

就当前代码而言,由于您已将hourminute标记为static,因此addTime方法只会将当前时间加倍。

由于您要将hourminute转换为实例变量,我认为addTime的下一个过程非常简单。你已经声明你已经尝试了一百万件事,但却无法发布任何一件事。所以,我认为你没有尝试过,只是要求我们给你解决方案。不过,我会为您提供解决方案:

public void addTime(Time other){
    addHours(other.getHour());
    addMinutes(other.getMinute());
}

除此之外,您的解决方案还存在AM / PM的另一个问题。如果将任何分钟数添加到晚上11:59的时间,你也没有处理这个条件。

如果您想获得其他方法的解决方案,请先尝试自己。然后,如果您遇到任何问题,请使用相关代码发布问题。

答案 1 :(得分:-1)

修改addHours和addMinutes方法

public void addHours(int h) {
    h = h + hour;
}
public void addMinutes(int m) {
    //Minute should not allow past 59 minutes.
    if (m <= 59)
    m = m + minute;
}

到这个

public void addHours(int h) {
    hour += h;
}
public void addMinutes(int m) {
    minute += m;
   if(minute > 59){
       hour += minute/60;
       minute = minute%60;
   }
}

说明:每当你添加min时,它也可能会改变你的小时。你有新的小时和分钟。

EG。 10分钟加到55分钟将增加1小时,新分钟为5