CH11 – Adding Times

  1. Create a new class named AddingTimes with three integer properties: hour, minute, and second. Also add a constructor that takes three parameters, a default constructor, and a toString method.
public class AddingTimes{
  int hour;
  int minute;
  int second;

  public AddingTimes(int hour, int minute, int second){
    this.hour = hour;
    this.minute = minute;
    this.second = second;
  }
  public AddingTimes(){}

  public String toString(){
    return this.hour + ":" + this.minute + ":" + this.second;
  }

}
  1. Create a static method named add that takes two AddingTimes as parameters.
public static AddingTimes add(AddingTimes t1, AddingTimes t2){
  AddingTimes sum = new AddingTimes();
  sum.hour = t1.hour + t2.hour;
  sum.minute = t1.minute + t2.minute;
  sum.second = t1.second + t2.second;
  return sum;
}
  1. Add a main method with the following code.
public static void main(String[] args){
  AddingTimes startTime = new AddingTimes(18, 50, 0);
  AddingTimes runningTime = new AddingTimes(2,16,0);
  AddingTimes endTime = AddingTimes.add(startTime, runningTime);
  System.out.println(endTime.toString());
}
  1. Compile and run the program.
$ javac AddingTimes.java 
$ java AddingTimes
20:66:0

The complete class appears as follows.

public class AddingTimes{
  int hour;
  int minute;
  int second;

  public AddingTimes(int hour, int minute, int second){
    this.hour = hour;
    this.minute = minute;
    this.second = second;
  }
  public AddingTimes(){
  }
  public String toString(){
    return this.hour + ":" + this.minute + ":" + this.second;
  }

  public static AddingTimes add(AddingTimes t1, AddingTimes t2){
    AddingTimes sum = new AddingTimes();
    sum.hour = t1.hour + t2.hour;
    sum.minute = t1.minute + t2.minute;
    sum.second = t1.second + t2.second;
    return sum;
  }

  public static void main(String[] args){
    AddingTimes startTime = new AddingTimes(18, 50, 0);
    AddingTimes runningTime = new AddingTimes(2,16,0);
    AddingTimes endTime = AddingTimes.add(startTime, runningTime);
    System.out.println(endTime.toString());
  }
}
  1. Add another method named add
public AddingTimes add(AddingTimes t2){
 AddingTimes sum = new AddingTimes();
 sum.hour = this.hour + t2.hour;
 sum.minute = this.minute + t2.minute;
 sum.second = this.second + this.second;
 return sum;
}
  1. Add the following lines to main.
 endTime = startTime.add(runningTime);
 System.out.println(endTime.toString());
 
  1. Compile and run the program.
$ javac AddingTimes.java 
$ java AddingTimes
20:66:0
20:66:0

The completed class appears as follows.

ublic class AddingTimes{
  int hour;
  int minute;
  int second;

  public AddingTimes(int hour, int minute, int second){
    this.hour = hour;
    this.minute = minute;
    this.second = second;
  }
  public AddingTimes(){
  }

  public String toString(){
    return this.hour + ":" + this.minute + ":" + this.second;
  }

  public static AddingTimes add(AddingTimes t1, AddingTimes t2){
    AddingTimes sum = new AddingTimes();
    sum.hour = t1.hour + t2.hour;
    sum.minute = t1.minute + t2.minute;
    sum.second = t1.second + t2.second;
    return sum;
  }

  public AddingTimes add(AddingTimes t2){
    AddingTimes sum = new AddingTimes();
    sum.hour = this.hour + t2.hour;
    sum.minute = this.minute + t2.minute;
    sum.second = this.second + this.second;
    return sum;
  }

  public static void main(String[] args){
    AddingTimes startTime = new AddingTimes(18, 50, 0);
    AddingTimes runningTime = new AddingTimes(2,16,0);
    AddingTimes endTime = AddingTimes.add(startTime, runningTime);
    System.out.println(endTime.toString());
    endTime = startTime.add(runningTime);
    System.out.println(endTime.toString());
  }
}