2016-12-01 74 views
1

我的老師給了我這個任務。她給了我們一個ClockTester.java主程序,我們必須創建一個公共類來聲明在Clock Tester中使用的對象和方法。下面是測試器,下面是我的clock.java代碼。我無法像想要的那樣格式化爲00:00:00。我也把她的指示指導。如何獲得用java創建的時鐘的正確輸出?

如果有人可以請,請幫助我在這裏。 另外,當我嘗試運行程序時,出現堆棧溢出錯誤。

Instructions from my teacher

//------------------------------------------------------------------- 
 
//Program: \t ClockTester 
 
//Author: \t D. Spence 
 
//Date: \t \t April 4, 2014 
 
//Purpose: \t Tests the features of the Clock class 
 
//------------------------------------------------------------------- 
 

 
import java.util.Scanner; 
 

 
public class ClockTester 
 
{ 
 
\t public static void main (String[] args) 
 
\t { 
 
\t \t //Declare five objects in the Clock class 
 
\t \t Clock c1, c2, c3, c4, c5; 
 

 
\t \t //Initialize Clock objects using constructors 
 
\t \t c1 = new Clock(); 
 
\t \t c2 = new Clock(15); 
 
\t \t c3 = new Clock(8,30); 
 
\t \t c4 = new Clock(12,5,45); 
 

 
\t \t //Show all four objects 
 
\t \t System.out.println("c1 is " + c1); 
 
\t \t System.out.println("c2 is " + c2); 
 
\t \t System.out.println("c3 is " + c3); 
 
\t \t System.out.println("c4 is " + c4); 
 
\t \t System.out.println(); 
 

 
\t \t //Determine if clock times are past noon 
 
\t \t System.out.println(c1 + " is " + (c1.isPM() ? "after noon" : "at or before noon")); 
 
\t \t System.out.println(c2 + " is " + (c2.isPM() ? "after noon" : "at or before noon")); 
 
\t \t System.out.println(c3 + " is " + (c3.isPM() ? "after noon" : "at or before noon")); 
 
\t \t System.out.println(c4 + " is " + (c4.isPM() ? "after noon" : "at or before noon")); 
 
\t \t System.out.println(); 
 

 
\t \t //Manually set a value 
 
\t \t c1.setTime(14,15,30); 
 
\t \t System.out.println("Changed c1 to " + c3); 
 

 
\t \t //Test for equality 
 
\t \t System.out.print (c1 + " is "); 
 
\t \t System.out.print ((c1.equals(c3)) ? "equal" : "NOT equal"); 
 
\t \t System.out.println (" to " + c3); 
 
\t \t System.out.println(); 
 

 
\t \t c5 = new Clock(15,0,0); 
 
\t \t System.out.print (c5 + " is "); 
 
\t \t System.out.print ((c5.equals(c2)) ? "equal" : "NOT equal"); 
 
\t \t System.out.print (" to " + c2); 
 
\t \t System.out.println(); 
 

 

 
\t \t //Demonstrate math operations 
 
\t \t System.out.println ("\n Some Clock Operations:"); 
 
\t \t System.out.print (c1 + " + 10 hours is "); 
 
\t \t c1.addHours(10); 
 
\t \t System.out.println (c1); 
 

 
\t \t System.out.print (c3 + " + 45 minutes is "); 
 
\t \t c3.addMinutes(45); 
 
\t \t System.out.println (c3); 
 

 
\t \t System.out.print (c4 + " + 90 seconds is "); 
 
\t \t c4.addSeconds(90); 
 
\t \t System.out.println (c4); 
 

 
\t \t System.out.print (c4 + " + 12:59:55 is "); 
 
\t \t c4.addTime(12,59,55); 
 
\t \t System.out.println (c4); 
 

 
\t \t //TestBonus(); 
 

 
\t \t System.out.println(); 
 
\t \t System.out.println("This concludes the test of the Clock class."); 
 
\t \t System.out.println(); 
 

 
\t } 
 

 
/* 
 
\t public static void TestBonus() 
 
\t { 
 
\t \t System.out.println("\n ****TESTING BONUS FEATURE****\n"); 
 

 
\t \t Clock c6 = new Clock(2,30,'p'); 
 
\t \t System.out.println ("Non-military clock time: " + c6); 
 
\t \t c6.setMilitary (true); 
 
\t \t System.out.println ("Same clock in military: " + c6); 
 
\t } 
 
*/ 
 

 
} 
 

 

 

 

 

 

 

 

 

 

 
//------------------------------------------------------------------- 
 
//Program: \t Clock 
 
//Author: \t Taylor P. 
 
//Date: \t \t November 30, 2016 
 
//Purpose: \t Creates a clock class 
 
//------------------------------------------------------------------- 
 

 
public class Clock 
 
{ 
 
\t private int hours; 
 
\t private int minutes; 
 
\t private int seconds; 
 
\t private final int hours_Min = 0; 
 
\t private final int hours_Max = 23; 
 
\t private final int minutes_Min = 0; 
 
\t private final int minutes_Max = 59; 
 
\t private final int seconds_Min = 0; 
 
\t private final int seconds_Max = 59; 
 

 

 
//Constructor sets default time to midnight (00:00:00) 
 
\t public Clock() 
 
\t { 
 
\t \t this.hours = 00; 
 
\t \t this.minutes = 00; 
 
\t \t this.seconds = 00; 
 
\t } 
 

 
//Constructor sets hours to input value 
 
\t public Clock (int h) 
 
\t { 
 
\t \t this.hours = h; 
 
\t \t this.minutes = 00; 
 
\t \t this.seconds = 00; 
 
\t } 
 

 
//Constructor sets hours and minutes to input value 
 
\t public Clock (int h, int m) 
 
\t { 
 
\t \t this.hours = h; 
 
\t \t this.minutes = m; 
 
\t \t this.seconds = 00; 
 
\t } 
 

 
//Constructor sets hours, minutes, seconds to input value 
 
\t public Clock (int h, int m, int s) 
 
\t { 
 
\t \t this.hours = h; 
 
\t \t this.minutes = m; 
 
\t \t this.seconds = s; 
 
\t } 
 

 
//------------------------------- 
 
//Method: getHours 
 
//Return: int - hours 
 
//Parameters: none 
 
//Purpose: returns hours 
 
//------------------------------- 
 
\t public int getHours() 
 
\t { 
 
\t \t return this.hours; 
 
\t } 
 

 
//------------------------------ 
 
//Method: getMinutes 
 
//Return: int 
 
//Parameters: none 
 
//Purpose: returns minutes 
 
//------------------------------ 
 
\t public int getMinutes() 
 
\t { 
 
\t \t return this.minutes; 
 
\t } 
 

 
//------------------------------ 
 
//Method: getSeconds 
 
//Return: int 
 
//Parameters: none 
 
//Purpose: returns seconds 
 
//------------------------------ 
 
\t public int getSeconds() 
 
\t { 
 
\t \t return this.seconds; 
 
\t } 
 

 

 
//------------------------------ 
 
//Method: isPM 
 
//Return: boolean 
 
//Parameter: none 
 
//Purpose: Determines if the time is past noon 
 
//------------------------------ 
 
\t public boolean isPM() 
 
\t { 
 
\t \t if (this.hours < 12) 
 
\t \t \t return false; 
 
\t \t else 
 
\t \t \t return true; 
 
\t } 
 

 
//------------------------------ 
 
//Method: setTime 
 
//Return: void 
 
//Parameter: 3 int types - hours, minutes, seconds 
 
//Purpose: set time to (00:00:00) format 
 
//----------------------------- 
 
\t public void setTime (int h, int m, int s) 
 
\t { 
 
\t \t this.hours = h; 
 
\t \t this.minutes = m; 
 
\t \t this.seconds = s; 
 

 
\t } 
 

 
//----------------------------- 
 
//Method: addHours 
 
//Return: void 
 
//Parameter: an int type of hours 
 
//Purpose: to add hours together 
 
//----------------------------- 
 
\t public void addHours (int h) 
 
\t { 
 
\t \t this.hours = this.hours + h; 
 
\t } 
 

 
//----------------------------- 
 
//Method: addMinutes 
 
//Return: void 
 
//Parameter: a int type of minutes 
 
//Purpose: to add minutes together 
 
//----------------------------- 
 
\t public void addMinutes (int m) 
 
\t { 
 
\t \t this.minutes = this.minutes + m; 
 
\t } 
 

 
//---------------------------- 
 
//Method: addMinutes 
 
//Return: void 
 
//Parameter: an int type of seconds 
 
//Purpose: to add seconds together 
 
//---------------------------- 
 
\t public void addSeconds (int s) 
 
\t { 
 
\t \t this.seconds = this.seconds + s; 
 
\t } 
 

 
//----------------------------- 
 
//Method: addTime 
 
//Return: void 
 
//Parameter: three int types of hours, minutes, seconds 
 
//Purpose: 
 
//----------------------------- 
 
\t public void addTime (int h, int m, int s) 
 
\t { 
 
\t \t this.hours = this.hours + h; 
 
\t \t this.minutes = this.minutes + m; 
 
\t \t this.seconds = this.seconds + s; 
 
\t } 
 

 
//----------------------------- 
 
//Method: equals 
 
//Return: 
 
//Parameter: 
 
//Purpose: 
 
//---------------------------- 
 
\t public boolean equals (Clock c) 
 
\t { 
 
\t \t boolean equiv = c.equals(c); 
 
\t \t return equiv; 
 
\t } 
 

 
//------------------------------ 
 
//Method: toString 
 
//Returns: 
 
//Parameters: 
 
//Purpose: 
 
//----------------------------- 
 
\t public String toString() 
 
\t { 
 
\t \t String string = ""; 
 
\t \t   \t if (this.equals(this.hours) && this.equals(this.minutes) && this.equals (this.seconds)) 
 
\t \t   \t { 
 
\t \t    \t string = "00:00:00"; 
 
\t \t   \t } 
 
\t \t   \t else if (this.equals(this.hours)) 
 
\t \t   \t { 
 
\t \t \t \t \t \t string = "00"; 
 
\t \t    \t string = string + ":" + this.minutes + ":" + this.seconds; 
 
\t \t   \t } 
 
\t \t   \t else if (this.equals(this.minutes)) 
 
\t \t   \t { 
 
\t \t \t \t \t \t string = "00"; 
 
\t \t \t \t \t \t string = this.hours + ":" + string + ":" + this.seconds; 
 
\t \t \t \t \t } 
 
\t \t \t \t \t else if (this.equals(this.seconds)) 
 
\t \t \t \t \t { 
 
\t \t \t \t \t \t string = "00"; 
 
\t \t   \t \t string = string + ":" + string + ":" + this.seconds; 
 
\t \t \t \t \t } 
 
\t \t \t \t \t else 
 
\t \t \t \t \t \t string = string + ":" + this.hours + ":" + this.minutes + ":" + this.seconds; 
 
     return string; 
 

 
\t } 
 

 
}

+0

您已將'老師的指令「鏈接到您計算機上的本地文件。 *:D * .. file /// Z:/Desktop/CSCI%201301%20Fall%2016/CSCI1301Project7.pdf – anacron

+0

您的'toString'方法沒有意義。 'this'永遠不會等於'this.seconds'(或任何其他實例字段)。 –

+0

我會嘗試這些答案,並讓你知道它是否解決了問題!非常感謝! –

回答

1

有一個在Clockequals方法的遞歸調用。這是你得到堆棧溢出錯誤的原因。

public boolean equals (Clock c) 
{ 
    boolean equiv = c.equals(c); // this keeps calling itself 
    return equiv; 
} 

它更改爲一個更合適的邏輯來檢查Clock對象相等性。

例如,

public boolean equals (Clock c) 
{ 
    boolean equiv = c!=null && this.hours==c.hours && this.minutes==c.minutes && this.seconds==c.seconds; 
    return equiv; 
} 

而且還可以,toString()方法是非常雜亂。這也需要重寫。事情是這樣的:

public String toString() 
{ 
    String string = String.format("%02d:%02d:%02d", this.hours, this.minutes, this.seconds); 
    return string; 
} 
+0

非常感謝您的幫助!我解決了這兩種方法,並且工作。 :) –

+0

現在我必須確保添加的時間是正確的小時,​​分​​鍾和秒。當我編寫和它增加時間我得到像08:45:135的答案。我如何讓時間有限制? (小時只有0-23,分鐘和秒只有0-59)。 –

+0

您需要將多餘的秒數和分鐘轉移到下一個單位。例如,如果您剩下135秒,則需要將其轉換爲分鐘數,並將分鐘數添加到您的時間。所以135變成2分15秒。這可以通過分割和找到模量來獲得。 135/60 = 2和135%60 = 15.希望你明白。 – anacron

0

它看起來像你的代碼被打破了toString方法Clock

if (this.equals (this.hours .... 

只是沒有任何意義。

其次與價值00一個int是一樣的,因爲它是0

所以你的代碼的作用是返回

String.format ("%02d:%02d:%02d", hours, minutes, seconds); 
0

equals方法是遞歸的,並比較c本身;相反,將cthis的字段進行比較。另外,您應該使用@Override註釋。它會告訴你Object.equals需要一個Object。像

@Override 
public boolean equals(Object o) { 
    if (o instanceof Clock) { // <-- Is it a Clock? 
     Clock c = Clock.class.cast(o); // <-- Use o as a Clock 
     return c.isPM() == this.isPM() // 
       && c.getHours() == this.getHours() // 
       && c.getMinutes() == this.getMinutes() // 
       && c.getSeconds() == this.getSeconds(); 
    } 
    return false; 
} 

東西然後你toString方法可以極大地爲兩位數創建一個簡單的輔助方法來墊int(S)簡化。像,

private static String get2Digits(int i) { 
    if (i < 10) { 
     return "0" + i; 
    } 
    return String.valueOf(i); 
} 

,然後用它來糾正toString(和使用@Override即使你知道toString不帶任何參數)。您可以使用StringBuilder來執行拼接。

@Override 
public String toString() { 
    return new StringBuilder(8) // 
      .append(get2Digits(hours)) // 
      .append(':').append(get2Digits(minutes)) // 
      .append(':').append(get2Digits(seconds)) // 
      .toString(); 
}