Knowee
Questions
Features
Study Tools

Create a Time class and initialize it with hours and minutes.1. Make a method addTime which should take two time object and add them. E.g.- (2 hour and 50 min)+(1 hr and 20 min) is (4 hr and 10 min)2. Make a method displayTime which should print the time.3. Make a method DisplayMinute which should display the total minutes in the Time. E.g.- (1 hr 2 min) should display 62 minute.

Question

Create a Time class and initialize it with hours and minutes.1. Make a method addTime which should take two time object and add them. E.g.- (2 hour and 50 min)+(1 hr and 20 min) is (4 hr and 10 min)2. Make a method displayTime which should print the time.3. Make a method DisplayMinute which should display the total minutes in the Time. E.g.- (1 hr 2 min) should display 62 minute.

🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a Python solution for your problem:

class Time:
    def __init__(self, hours, minutes):
        self.hours = hours
        self.minutes = minutes

    def addTime(self, time_obj):
        total_minutes = self.minutes + time_obj.minutes
        total_hours = self.hours + time_obj.hours

        # Convert any set of 60 minutes to an hour
        if total_minutes >= 60:
            total_hours += total_minutes // 60
            total_minutes = total_minutes % 60

        return Time(total_hours, total_minutes)

    def displayTime(self):
        print(f"Time is {self.hours} hours and {self.minutes} minutes")

    def displayMinute(self):
        print(f"Total is {(self.hours * 60) + self.minutes} minutes")


# Testing the Time class
time1 = Time(2, 50)
time2 = Time(1, 20)

added_time = time1.addTime(time2)
added_time.displayTime()  # Should print: Time is 4 hours and 10 minutes

time3 = Time(1, 2)
time3.displayMinute()  # Should print: Total is 62 minutes

This Python class Time has an __init__ method to initialize hours and minutes, an addTime method to add two Time objects, a displayTime method to print the time in hours and minutes, and a displayMinute method to print the total time in minutes.

This problem has been solved

Similar Questions

Develop a C++ program that uses a class called time A time is represented in hours, minutes and seconds. For example (3:35:50). Define three objects for time class and get the user input values for first object.  Initialize the second object using constructors. Set the third object equal to the sum of other two and display the value of the new timings using operator overloading, destructors and friend functions. Sample Input     3:35:504:30:20Sample Output: 8:06:10.

if(t2.seconds > t1.seconds) { --t1.minutes; t1.seconds += 60; } difference->seconds = t1.seconds - t2.seconds; if(t2.minutes > t1.minutes) { --t1.hours; t1.minutes += 60; } difference->minutes = t1.minutes-t2.minutes; difference->hours = t1.hours-t2.hours;}

Write a program to print 1 after 1 sec, 2 after 2 sec till 5. Complete the given code to get the desired output as shown in the expected output.

Objectivesimproving the ability to use numbers, operators, and arithmetic operations in Python;using the print() function's formatting capabilities;learning to express everyday-life phenomena in terms of programming language.ScenarioYour task is to prepare a simple code able to evaluate the end time of a period of time, given as a number of minutes (it could be arbitrarily large). The start time is given as a pair of hours (0..23) and minutes (0..59). The result has to be printed to the console.For example, if an event starts at 12:17 and lasts 59 minutes, it will end at 13:16.Don't worry about any imperfections in your code - it's okay if it accepts an invalid time - the most important thing is that the code produce valid results for valid input data.Test your code carefully. Hint: using the % operator may be the key to success.

convert minutes into a number of years and days.Write a Java program to convert minutes into a number of years and days. Constraints:N/AExample:InputThe number of minutes: 3456789Output:6years 210 days

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.