如何打印出带有 void 的方法?

问题描述 投票:0回答:0

我在 java 中创建了一个代码,可以在时钟中打印出时间。有些方法使用 void 我很难打印出来。我打印来测试我的代码的那个是使用我重载的构造函数。我也对代码的作用添加了评论。

我只需要一些帮助我打印出 void 方法。

 public class ClockDriver
 {
 public static void main(String[] args)
 {
 //create a Clock object with default constructor


//create a Clock object with overloaded constructor
    

//print the Clock objects
    


//run the Clock class methods you wrote to ensure they work 

//please put each method on its own separate line        
    
    Clock c1 = new Clock(3, 51);
    System.out.println(c1);
    
    // System.out.println(c1.addMinute());
    
    }
    }

   class Clock
  {
  //attributes of the Clock object
  private int hours;
  private int minutes;

 //default constructor for the Clock object
 //set hours and minutes to 0, as in military time for midnight.
 public Clock()
     {
         hours = 0;
         minutes = 0;
     }

//overloaded constructor for the Clock object
//sets instance variables to values in the parameters
  public Clock(int h, int m)
  {
     hours = h;
     minutes = m;
  }

  //get hours
  public int getHours()
  {
    return hours;
   }

  //get minutes
  public int getMinutes()
  {
    return minutes;
  }

  // adds a minute to the time
  public void addMinute ()
  {
      if(this.hours== 23 &&this.minutes== 59) 
     {
        this.hours=0;
        this.minutes=0;
     }
         else if(this.minutes==59)
       {
            this.hours++;this.minutes=0;
       }

      this.minutes++;

}

  // sets the object to a specific time
  public void reset (int h, int m)
 {
     this.hours = h % 24;
     this.minutes = m % 60;
 }

 // springs the time one hour ahead
 public void springAhead (int moreHours)
 {
     if(this.hours==23) this.hours=0;
     else this.hours++;
 }

 // falls the time one hour behind
 public void fallBack (int lessHours)
{
     if(this.hours==0) this.hours=23;
     else this.hours--;
}

//toString method tells the computer how to print the object
//it should print in this format hours:minutes
//remember that if the minutes are less than 10, add a leading 0
//it should still print the way the time would
public String toString()
{
      if(this.minutes<10) return this.hours+":0"+ this.minutes;

      return this.hours+":"+ this.minutes;
}




 //normalize method 
 //you can decide if you would like to use military time or regular time
 //no need to distinguish AM or PM for regular time

 //this method should be called anytime there is a possibility
 //that the number of hours could be over 24 (or 12)
 //or minutes could be over 59.
//if that is the case, this method should fix it
public void normalize()
{
     if(this.minutes>59)
   {

     this.minutes=this.minutes%60;

   }

   if(this.hours>23)
   {

     this.hours=this.hours%24;

    }

  }
}
java class void
© www.soinside.com 2019 - 2024. All rights reserved.