Adder Java Program ISC 2017

A class Adder has been defined to add any two accepted time.
Example:
Time A – 6 hours 35 minutes
Time B – 7 hours 45 minutes
Their sum is – 14 hours 20 minutes (where 60 minutes = 1 hour)

The details of the members of the class are given below:

Class name: Adder
Data members/instance variables:
a[]: integer array to hold two elements (hours and minutes).
Member functions/methods:
Adder(): constructor to assign 0 to the array elements.
void readTime(): to enter the elements of the array.
void addTime(Adder X, Adder Y): adds the time of the two parameterized objects X and Y and stores the sum in the current calling object.
void dispTime(): displays the array elements with an appropriate message (i.e. hours = ? and minutes = ?)

Specify the class Adder giving details of the constructor, void readTime(), void addTime(Adder, Adder) and void dispTime(). Define the main() function to create objects and call the functions accordingly to enable the task.

import java.io.*;
class Adder{
    private int a[];
    public Adder(){
        a = new int[2];
    }
    public void readTime()throws IOException{
        InputStreamReader in = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(in);
        System.out.print("hours = ");
        a[0] = Integer.parseInt(br.readLine());
        System.out.print("minutes = ");
        a[1] = Integer.parseInt(br.readLine());
    }
    public void addTime(Adder X, Adder Y){
        this.a[1] = X.a[1] + Y.a[1];
        this.a[0] = X.a[0] + Y.a[0] + this.a[1] / 60;
        this.a[1] %= 60;
    }
    public void dispTime(){
        System.out.println("hours = " + this.a[0]);
        System.out.println("minutes = " + this.a[1]);
    }
    public static void main(String args[])
    throws IOException{
        Adder x = new Adder();
        x.readTime();
        Adder y = new Adder();
        y.readTime();
        Adder z = new Adder();
        z.addTime(x, y);
        z.dispTime();
    }
}

Leave a Reply

%d bloggers like this: