icse-promo

Question

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 member/instance variable:
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.

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java. util.Scanner;
public class Adder
{
    int a[]=new int[2];
    Scanner sc=new Scanner( System.in);
    Adder()
    {
        a[0]=0;
        a[1]=0;
    }

    void readtime()
    {
        System.out.println("Enter hours and minutes");
        a[0]=sc. nextInt();
        a[1]=sc. nextInt();
    }
    
    void disptime()
    {
        System. out.println("Hours=" + a[0]);
        System.out.println("Minutes="  + a[ 1]);
    }
    
    void addtime(Adder X,Adder Y)
    {
        /* adds minutes */
        a[1]=X.a[1] + Y.a[1];
        /*if minutes is greater than 60 than add hours(a[1]/60) to a[0] position*/ 
        if(a[1]>=60)
        {
            a[0]=a[1]/60;
            a[1]=a[1]%60;
        }
        a[0] += X.a[0] + Y.a[0];
    }
    
    public static void main()
    {    
        Adder ob1=new Adder(); 
        Adder ob2=new Adder();
        Adder ob3=new Adder();
        ob1.readtime();
        ob2.readtime() ;
        ob3.addtime(ob1,ob2);
        ob3.disptime();
    }

}



				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *