Computer Science

Design a class 'Time' with the following specifications:

class Time

Data members/Instance variables
int hour, min, sec

Member Methods:
void gettime( ) : to accept a time in hour, minute and second. void showtime( ) : to display the time in terms of hour, minute and second.

Write a main method to create an object of class 'Time' and call the member methods.

Objects & Classes

37 Likes

Answer

import java.util.Scanner;

class Time {
    int hour; 
    int min; 
    int sec;

    void get_time() {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter hours: ");
        hour = in.nextInt();
        System.out.print("Enter minutes: ");
        min = in.nextInt();
        System.out.print("Enter seconds: ");
        sec = in.nextInt();
    }

    void show_time() {
        System.out.println(hour + ":" + min + ":" + sec);
    }

    public static void main(String args[]) {
        Time obj = new Time();
        obj.get_time();
        obj.show_time();
    }
}

Answered By

20 Likes


Related Questions