【问题标题】:Java Clock AssignmentJava 时钟分配
【发布时间】:2019-05-09 02:47:18
【问题描述】:

对于我的任务,我们假设要制作一个时钟。我们需要小时、分钟和秒的变量以及 setHours/getHours、setMinutes/getMinutes、setSeconds/getSeconds 等方法。我还需要一个 addClock() 方法来计算两个时钟对象的总和,以及一个递减时钟的 tickDown() 方法。我还需要一个将 Clock 对象增加一秒的 tick() 方法。

这是我目前所拥有的......

public class Clock {

   private int hr; //store hours
   private int min; //store minutes
   private int sec; //store seconds

   // Default constructor

   public Clock () {
      setClock (0, 0, 0);
   }

   public Clock (int hours, int minutes, int seconds) {
      setClock (hours, minutes, seconds);
   }

   public void setClock (int hours, int minutes, int seconds) {
      if (0 <= hours && hours < 24)
         hr = hours;
      else
         hr = 0;
      if (0 <= minutes && minutes < 60)
         min = minutes;
      else
         min = 0;
      if (0 <= seconds && seconds < 60)
         sec = seconds;
      else
         sec = 0;
   }

   public int getHours() {
      return hr;
   }

   public int getMinutes() {
      return min;
   }

   public int getSeconds() {
      return sec;
   }

   public void addClock( Clock secondClock ) {
      this.sec += secondClock.getSeconds();
      this.min += secondClock.getMinutes();
      //add overflow to minutes from seconds
      this.min +=(int)(this.sec/60);
      //update seconds 
      this.sec = this.sec % 60;
      this.hr += secondClock.getHours();
      //add overflow to minutes from seconds
      this.hr +=(int)(this.min/60);
      //update minutes
      this.min = this.min % 60;
      //adjust hours
      this.hr = this.hr % 24;
   }

   public void tick(){
      this.sec += 1;
      //add overflow to minutes from seconds
      this.min +=(int)(this.sec/60);
      //update seconds 
      this.sec = this.sec % 60;
      //add overflow to minutes from seconds
      this.hr +=(int)(this.min/60);
      //update minutes
      this.min = this.min % 60;
      //adjust hours
      this.hr = this.hr %24;
   }

   public void tickDown(){
      this.sec -= 1;
      if(this.sec <0){
         this.sec+=60;
         this.min-=1;
      }
      if(this.min<0){
         this.min+=60;
         this.hr-=1;
      }
      if(this.hr<0){
         this.hr+=24;
      }
   }
}

【问题讨论】:

标签: java clock


【解决方案1】:

你可以写addClock()如下:

    public void addClock(Clock secondClock){
       this.second += secondClock.getSecond();
       this.minute+= secondClock.getMinute();
       //add overflow to minutes from seconds
       this.minute+=(int)(this.second/60);
       //update seconds 
        this.second = this.second % 60;
       this.hour += secondClock.getHour();
       //add overflow to minutes from seconds

        this.hour+=(int)(this.minute/60);
       //update minutes
        this.minute= this.minute% 60;

        //adjust hours
        this.hour = this.hour%24;
    }

同样你可以写ticktickDown方法,唯一的区别是你在方法中不会有任何参数,你需要在秒加减1后重新调整分钟和小时

  public void tick(){
       this.second += 1;
       //add overflow to minutes from seconds
       this.minute+=(int)(this.second/60);
       //update seconds 
       this.second = this.second % 60;
       //add overflow to minutes from seconds
        this.hour+=(int)(this.minute/60);
       //update minutes
        this.minute= this.minute% 60;
        //adjust hours
        this.hour = this.hour%24;
    }

  public void tickDown(){
       this.second -= 1;
       if(this.second <0){
          this.second+=60;
          this.minute-=1;
        }
       if(this.minute<0){
          this.minute+=60;
          this.hour-=1;
        }
       if(this.hour<0){
          this.hour+=24;
       }
    }

添加main方法为:

   public static void main(String[]  args){
      Clock clock1 = new Clock(2,4,7);
      Clock clock2 = new Clock(8,26,57);
      clock1.tick();
      int newSeconds = clock1.getSeconds();
      //validate it should be 8 now

      clock2.tickDown();
      newSeconds = clock1.getSeconds();
      //validate it should be 56 now

      clock1.addClock(clock2);
      //get Hour, minute and second from clock1 and validate
      int newHour = clock1.getHours();
      int nuwMinute = clock1.getMinutes();
      newSeconds = clock1.getSeconds();
      //validate if they are with the right expected value or not
  }

【讨论】:

  • 啊!太感谢了!我在 BlueJ 中对此进行编码,并且在我的原始代码顶部的 IfElse 语句似乎有问题。有什么建议吗?
  • 你的意思是setClock方法吗?您是否尝试将值设置为0,如果它们被传递高,即如果某些通过分钟为75,您是否要将其设置为0。另外,您指的是有什么具体问题吗?
  • 我修复了它,我将 if else 大写,但它现在可以工作了。现在我正在尝试添加你之前提到的tick()、tickDown() 和main 方法。这些方法都进入同一个类程序吗?
  • 如果您在if(0 &lt;= hours &amp;&amp; hours &lt; 24);{ 中谈论;,只需从所有ifs 的末尾删除; 就像这样if(0 &lt;= hours &amp;&amp; hours &lt; 24); {-->将其更改为` if(0
  • 是的,他们都将进入同一个班级,即Classmain 方法由您决定。如果您想运行 Clock 类本身,则将其放在 clock 方法中,否则,您可以将它放在另一个类中,例如ClockTest,在上面实例化clock对象并运行它。
【解决方案2】:

您可以通过仅维护刻度来简化刻度逻辑。 从刻度值中提取小时、分钟和秒。

public Clock
{
 long ticks = 0;
 public void tick()
 {
   ticks++;
 }

 public void tickDown()
 {
  ticks--;
 }

 public int getSeconds()
 {
   return ticks % 60;
 }

 public int getMinutes()
 {
   return (ticks / 60) % 60;
 }

 public int getHours()
 {
      return (ticks / (60 * 60)) % 24;
 }
 }

【讨论】:

    【解决方案3】:

    您必须了解有关 Java 语言的更多信息。 ;-)

    if( condition ) { /* no ';' here */
       <block when true>
    }
    else {
       <block when false>
    }
    

    主线不见了,我提议

    public static void main( String args[] ) {
       Clock c1 = new Clock( 9, 59, 59 );
       c1.tickSeconds();
       System.err.println( c1 ); // You have to overload toString()
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-29
      • 1970-01-01
      相关资源
      最近更新 更多