Question

Write a class called Time whose only field is a time in seconds. It should have...

Write a class called Time whose only field is a time in seconds. It should have a method called

convert_to_minutes that returns a string of minutes and seconds formatted as in the following

example: if seconds is 230, the method should return '5:50'. It should also have

a method called convert_to_hours that returns a string of hours, minutes, and seconds

formatted analogously to the previous method.

(python)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

'''
Python version : 3.6
Python program to create and test class Time
'''

class Time:
  
   # constructor
   def __init__(self, seconds):
       self.seconds = seconds
  
   # function to convert and return a string of minutes and seconds
   def convert_to_minutes(self):
       mins = self.seconds//60 # get the total minutes in the given seconds
       secs = self.seconds - (mins*60) # get the remaining seconds
      
       # return the string of minutes:seconds
       return "%d:%d" %(mins,secs)
  
   # function to convert and return string of hours, minutes, and seconds
   def convert_to_hours(self):
       secs = self.seconds
       hours = secs//3600 # get the total hours in the given seconds
       secs = secs - (hours*3600) # get the remaining seconds
       mins = secs//60 # get the total minutes in the remaining seconds
       secs = secs - (mins*60) # get the remaining seconds
      
       # return the string of hours:minutes:seconds
       return "%d:%d:%d" %(hours,mins,secs)
      
def main():
  
   # test the Time class
   time = Time(230)
   print(time.convert_to_minutes())
   time = Time(4520)
   print(time.convert_to_hours())

#call the main function      
main()      
#end of program

Code Screenshot:

Python version : 3.6 Python program to create and test class Time LI Eclass Time: # constructor def init (self, seconds): sel

Output:

3:50 1:15:20

Add a comment
Know the answer?
Add Answer to:
Write a class called Time whose only field is a time in seconds. It should have...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time a...

    JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time at your location. Also include a getTime method that returns a string with the hours and minutes by calling the getHours and getMinutes methods. Provide a subclass WorldClock whose constructor accepts a time offset. For example, if you livein California, a new WorldCLock(3) should show the time in NewYork, three time zones ahead. Include an Alarm feature in the Clock class....

  • Q1. Write a C++ class called Time24h that describes a time of the day in hours,...

    Q1. Write a C++ class called Time24h that describes a time of the day in hours, minutes, and seconds. It should contain the following methods: • Three overloaded constructors (one of which must include a seconds value). Each constructor should test that the values passed to the constructor are valid; otherwise, it should set the time to be midnight. A display() method to display the time in the format hh:mm:ss. (Note: the time can be displayed as 17:4:34, i.e. leading...

  • Problem 1.(1) Implement a class Clock whose getHours and getMinutes methods return the current time at...

    Problem 1.(1) Implement a class Clock whose getHours and getMinutes methods return the current time at your location. (Call java.time.LocalTime.now().toString() or, if you are not using Java 8, new java.util.Date().toString() and extract the time from that string.) Also provide a getTime method that returns a string with the hours and minutes by calling the getHours and getMinutesmethods. (2) Provide a subclass WorldClock whose constructor accepts a time offset. For example, if you live in California, a new WorldClock(3) should show...

  • Write a class called Primes. This class should contain a constructor and a method called next_prime....

    Write a class called Primes. This class should contain a constructor and a method called next_prime. The next_prime method returns the next prime number. For example: >>> n = int(input('How many prime numbers?\n')) >>> p = primes() >>> prime_numbers = [ ] >>> for i in range(n): >>> prime = p.next_prime() >>> primes_numbers.append(prime) >>> print(prime_numbers) If the user types 5 in response to the prompt, then the output should be [2, 3, 5, 7, 11]. python 2.7.13

  • Programming Assignment 1 Write a class called Clock. Your class should have 3 instance variables, one...

    Programming Assignment 1 Write a class called Clock. Your class should have 3 instance variables, one for the hour, one for the minute and one for the second. Your class should have the following methods: A default constructor that takes no parameters (make sure this constructor assigns values to the instance variables) A constructor that takes 3 parameters, one for each instance variable A mutator method called setHour which takes a single integer parameter. This method sets the value of...

  • This is a Java programming assignment and I want help with the Time class. See below...

    This is a Java programming assignment and I want help with the Time class. See below for assignment details: For this question you must write a java class called Time and a client class called TimeClient. The partial Time class is given below. (For this assignment, you will have to submit 2 .java files: one for the Time class and the other one for the TimeClient class and 2 .class files associated with these .java files. So in total you...

  • Java Time Class Class declarations are one of the ways of defining new types in Java....

    Java Time Class Class declarations are one of the ways of defining new types in Java. we will create two classes that both represent time values for a 24 hours period. The valid operations are as follows. int getHours(): returns the number of hours int getMinutes(): returns the number of minutes int getSeconds(): returns the number of seconds String toString(): returns a String representation boolean equals(Time other): returns true if and only if other designates an object that has the...

  • The Tokenizer.java file should contain: A class called: Tokenizer Tokenizer should have a private variable that...

    The Tokenizer.java file should contain: A class called: Tokenizer Tokenizer should have a private variable that is an ArrayList of Token objects. Tokenizer should have a private variable that is an int, and keeps track of the number of keywords encountered when parsing through a files content. In this case there will only be one keyword: public. Tokenizer should have a default constructor that initializes the ArrayList of Token objects Tokenizer should have a public method called: tokenizeFile tokenizeFile should...

  • Write a class named Month. The class should have an int field named monthNumber that holds...

    Write a class named Month. The class should have an int field named monthNumber that holds the number of the month. For example. January would be 1. February would be 2. and so forth. In addition, provide the following methods A no-arg constructor that sets the monthNumber field to 1 A constructor that accepts the number of the month as an argument. It should set the monthNumber field to the value passed as the argument. If a value less than...

  • Part 3 (Lab2a) In this exercise you will: a. Write a method called secondTime that takes...

    Part 3 (Lab2a) In this exercise you will: a. Write a method called secondTime that takes as argument an integer corresponding to a number of seconds, computes the exact time in hours, minutes and seconds, then prints the following message to the screen: <inputseconds> seconds corresponds to: <hour> hours, <minute> minutes and <second> seconds Write another method called in Seconds that takes as arguments three integers: hours, minutes and seconds, computes the exact time in seconds, then returns the total...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT