Skip to content
Advertisement

How would you add a time offset using the same name as the class name for the parameter

I need to fill out the method for the time offset but using the class name as a parameter name and I don’t know how to do it I have tried adding the variables together but I get errors saying “You can’t convert Time to an int”

So I have no clue on what to do, please respond soon thank you!

Error type:

Time.java:48: error: bad operand types for binary operator '+'
       this.minutes += offset;
                    ^
  first type:  int
  second type: Time
1 error
public class Time
{
   // The values of the three parts of the time
   private int hours;
   private int minutes;
   private int seconds;

   public Time()
   {
      this.hours = 0;
      this.minutes = 0;
      this.seconds = 0;
   }

   public Time(int h, int m, int s)
   {
      this.hours = h;
      this.minutes = m;
      this.seconds = s;
   }
   
   public void add(Time offset)
   {
        // Part b: complete the add method
   }

   public String toString()
   {
      return pad(hours) + ":" + pad(minutes) + ":" + pad(seconds);
   }

   private String pad(int value)
   {
      String sign = "";
      if (value < 0)
      {
         sign = "-";
         value = -1 * value;
       }
       if (value < 10) {
          return sign + "0" + value;
       } else {
          return sign + value;
       }
    }

    public static void main(String[] args)
    {
       Time time1 = new Time(1,1,1);
       Time time2 = new Time(2,2,2);
       time1.add(time2);
       System.out.println("The result of (1,1,1).add(2,2,2) is " +
                           time1 + " and should be (03:03:03)");

       time1 = new Time(0,0,59);
       time2 = new Time(0,0,1);
       time1.add(time2);
       System.out.println("The result of (0,0,59).add(0,0,1) is " +
                           time1 + " and should be (00:01:00)");

       time1 = new Time(0,59,0);
       time2 = new Time(0,0,1);
       time1.add(time2);
       System.out.println("The result of (0,59,0).add(0,0,1) is " +
                           time1 + " and should be (00:59:01)");

       time1 = new Time(0,59,59);
       time2 = new Time(0,0,1);
       time1.add(time2);
       System.out.println("The result of (0,59,59).add(0,0,1) is " +
                           time1 + " and should be (01:00:00)");

       time1 = new Time(23,0,0);
       time2 = new Time(1,0,0);
       time1.add(time2);
       System.out.println("The result of (23,0,0).add(1,0,0) is " +
                           time1 + " and should be (00:00:00)");

       time1 = new Time(23,59,59);
       time2 = new Time(23,59,59);
       time1.add(time2);
       System.out.println("The result of (23,59,59).add(23,59,59) is " +
                           time1 + " and should be (23:59:58)");
   }
}

Advertisement

Answer

You have to add up the corresponding parts of time:

public void add(Time offset)
{
    this.seconds = this.seconds + offset.seconds;
    if(this.seconds >= 60) {
        this.seconds -= 60;
        this.minutes += 1;
    }
    this.minutes += offset.minutes;
    if(this.minutes >= 60) {
        this.minutes -= 60;
        this.hours += 1;
    }
    this.hours += offset.hours;
    if(this.hours >= 24)
        this.hours -= 24;
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement