我的老師給了我這個任務。她給了我們一個ClockTester.java主程序,我們必須創建一個公共類來聲明在Clock Tester中使用的對象和方法。下面是測試器,下面是我的clock.java代碼。我無法像想要的那樣格式化爲00:00:00。我也把她的指示指導。如何獲得用java創建的時鐘的正確輸出?
如果有人可以請,請幫助我在這裏。 另外,當我嘗試運行程序時,出現堆棧溢出錯誤。
//-------------------------------------------------------------------
//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 }
}
您已將'老師的指令「鏈接到您計算機上的本地文件。 *:D * .. file /// Z:/Desktop/CSCI%201301%20Fall%2016/CSCI1301Project7.pdf – anacron
您的'toString'方法沒有意義。 'this'永遠不會等於'this.seconds'(或任何其他實例字段)。 –
我會嘗試這些答案,並讓你知道它是否解決了問題!非常感謝! –