java添加时间,如何通过Java中的addHours()方法添加时间
我試圖讓這個飛機項目工作,我不能為我的生活弄清楚如何在“public void addHours()”中添加時間,或者如果我甚至做了前面的正確!任何幫助都會很棒!
問題有待回答:
創建一個Time類 . 該課程將代表一個時間點,例如出發時間 . 它應該包含2個構造函數,2個實例變量(小時和分鐘)和10個方法(見下文) . 除了toString之外的所有方法都應該是24小時格式 . default constructor:為12:00 AM創建一個Time對象 . 重載的構造函數:在特定的小時和分鐘創建一個Time對象 . getHour():返回表示Time對象的小時的整數 . getMinute():返回表示Time對象分鐘的整數 . addHours(...):通過將對象向前移動數小時來更新對象 . addMinute(...):通過將對象向前移動幾分鐘來更新對象 . (提示:請注意,不要讓分鐘數超過59.)addTime(...):通過從另一個Time對象向前移動小時和分鐘來更新對象 . getCopy(...):返回一個新的Time對象,該對象具有與現有Time對象相同的小時和分鐘 . isEarlierThan(...):如果此Time對象早于另一個Time對象,則返回true . isSameTime(...):如果此Time對象與另一個Time對象相同,則返回true . isLaterThan(...):如果此Time對象晚于另一個Time對象,則返回true . toString():返回表示Time對象的字符串 . 使用12小時AM / PM格式并將分鐘數分為兩位數 . 有關示例,請參閱示例輸出 .
到目前為止,這是我的Time類:
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());
*/
}
}
總結
以上是生活随笔為你收集整理的java添加时间,如何通过Java中的addHours()方法添加时间的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php 处理raw数据,php以raw格
- 下一篇: fileviewfinder.php,L