Question

In: Computer Science

Background For this exercise, you will write an Appointment class that tracks information about an appointment...

Background For this exercise, you will write an Appointment class that tracks information about an appointment such as the start and end times and a name for the appointment. Your Appointment class will also have an overlaps() method that takes another Appointment object as an argument and determines whether the two appointments overlap in python.

Instructions

Write a class called Appointment with methods as described below:

__init__() method Define an __init__() method with four parameters: self name, a string indicating the name of the appointment (for example, "Sales brunch"). start, a tuple consisting of two integers representing the start time of the appointment. The first integer represents an hour in 24-hour time; the second integer represents a number of minutes. For example, the tuple (10, 30) would represent 10:30 am. end, a tuple similar to start representing the end time of the appointment. The __init__() method should create attributes name, start, and end, and use them to store the information from the parameters.

overlaps() method Define an overlaps() method with two parameters, self and other, where other is another Appointment object. This method should return True if self and other overlap and False if they do not. Be sure to return a boolean value rather than a string. Note that two appointments overlap if the start time of one appointment occurs between the start and end times of the other appointment (including if the start times are equal), OR the end time of one appointment occurs between the start and end times of the other appointment (including if the end times are equal) For this assignment, if the start time of one appointment is equal to the end time of the other appointment, the two appointments are not considered to overlap. Hints Assuming you have two values a and c such that a is less than c, you can determine if a third value b is between a (inclusive) and c (exclusive) with an expression like this: a <= b < c When Python compares two sequences (such as tuples), it compares each item in the first sequence to the corresponding item in the second sequence until it finds a difference. For example, given the expression (10, 4, 12) < (10, 5, 1), Python first compares 10 to 10. Because they are the same, Python then compares 4 to 5. Because 4 is smaller than 5, Python stops comparing and the expression evaluates to True: (10, 4, 12) is considered less than (10, 5, 1) in Python. A boolean expression such as a <= b < c evaluates to True or False. You don’t need to put it inside a conditional statement to return a boolean value. In other words, instead of this: if a <= b < c: return True else: return False you can do this: return a <= b < c

Docstrings Write a docstring for your class that documents the purpose of the class as well as the purpose and expected data type of each attribute. For each of the methods you wrote, write a docstring that documents the purpose of the method as well as the purpose and expected data type of each parameter other than self (you never need to document self in a docstring). If your document returns a value, document that. If your document has side effects, such as modifying attributes or writing to standard output, document those as well. Be sure your docstrings are the first statement in the class or method they document. Improperly positioned docstrings are not recognized as docstrings by Python.

Using your class You can use your class by importing your module into another script. Below is an example of such a script; it assumes the Appointment class is defined in a script called appointment.py which is located in the same directory as this script. use_appointment.py from appointment import Appointment def main(): """ Demonstrate use of the Appointment class. """ appt1 = Appointment("Physics meeting", (9, 30), (10, 45)) appt2 = Appointment("Brunch", (10, 30), (11, 00)) appt3 = Appointment("English study session", (13, 00), (14, 00)) if appt1.overlaps(appt2): print(f"{appt1.name} overlaps with {appt2.name}") else: print(f"{appt1.name} does not overlap with {appt2.name}") if appt1.overlaps(appt3): print(f"{appt1.name} overlaps with {appt3.name}") else: print(f"{appt1.name} does not overlap with {appt3.name}") assert appt1.overlaps(appt2) assert appt2.overlaps(appt1) assert not appt1.overlaps(appt3) assert not appt3.overlaps(appt1) assert not appt2.overlaps(appt3) assert not appt3.overlaps(appt2) if __name__ == "__main__": main()

Solutions

Expert Solution

appointment.py

class Appointment:
        
        '''
        Tracks the information about an appointment from provided variables.
        And also checks if two appointment overlaps or not.

        Class methods:
        |__  __init__ --> Method to initialize class variables
        |__  overlaps --> Checks if two appointments overlaps or not.
        '''

        def __init__(self, name, start, end):
                '''
                Initializes the class variables to store information with the provided variable

                Input: self, name, start, end
                |__  self: Appointment class instance    | datatype: Appointment class 
                |__  name: name of the appointment       | datatype: String
                |__ start: start time of the appointment | datatype: Tuple
                |__   end: end time of the  appointment  | datatype: Tuple
                
                Output:
                Stores the input values as class variables
                class variables: name, start, end
                (string) name  --> from parameter: name
                (tuple)  start --> from parameter: start
                (tuple)  end   --> from parameter: end
                '''

                self.name = name
                self.start = start
                self.end = end

        def overlaps(self, appointmentTwo):
                '''
                Checks if two appointment overlaps or not.

                Input: self, appointmentTwo
                |__           self: Appointment class instance        | datatype: Appointment class
                |__ appointmentTwo: Second Appointment class instance | datatype: Appointment class


                Output:
                Returns True if both the appointments overlaps else returns False

                True, if -> start1 <= start2 < end1  OR  start1 < end2 <= end1
                False, else

                '''
                return (self.start <= appointmentTwo.start < self.end) or (self.start < appointmentTwo.end <= self.end)

use_appointment.py: Note this is already provided in the question, but attaching this also for correct indentation.

from appointment import Appointment 

def main(): 
        """ Demonstrate use of the Appointment class. """ 
        
        appt1 = Appointment("Physics meeting", (9, 30), (10, 45)) 
        appt2 = Appointment("Brunch", (10, 30), (11, 00)) 
        appt3 = Appointment("English study session", (13, 00), (14, 00)) 
        
        if appt1.overlaps(appt2): 
                print(f"{appt1.name} overlaps with {appt2.name}") 
        else: 
                print(f"{appt1.name} does not overlap with {appt2.name}") 
        
        if appt1.overlaps(appt3): 
                print(f"{appt1.name} overlaps with {appt3.name}") 
        else: 
                print(f"{appt1.name} does not overlap with {appt3.name}") 

        assert appt1.overlaps(appt2) 
        assert appt2.overlaps(appt1) 
        assert not appt1.overlaps(appt3) 
        assert not appt3.overlaps(appt1) 
        assert not appt2.overlaps(appt3) 
        assert not appt3.overlaps(appt2) 

if __name__ == "__main__": 
        main()

Screenshots of programs for better indentation:

appointment.py --->

use_appointment.py --->

Note: To run the files both should be in same directory, and it should be run using use_appointment program

Sample run:


Related Solutions

Write a computer program that stores and tracks information about high school students. The first piece...
Write a computer program that stores and tracks information about high school students. The first piece of information is what grade the student is in. Since this is for high school, the available values are 9, 10, 11, and 12. We also want to track the student’s GPA. Examples of GPAs are 2.2, 2.6, and 4.0. Finally, we want to track the letter grade that the student got on his or her final exam. These values are A, B, C,...
Write some background information about the Economic and cost issue of the opioid crisis in the...
Write some background information about the Economic and cost issue of the opioid crisis in the US.
Using the class Date that you defined in Exercise O3, write the definition of the class...
Using the class Date that you defined in Exercise O3, write the definition of the class named Employee with the following private instance variables: first name               -           class string last name                -           class string ID number             -           integer (the default ID number is 999999) birth day                -           class Date date hired               -           class Date base pay                 -           double precision (the default base pay is $0.00) The default constructor initializes the first name to “john”, the last name to “Doe”, and...
Write background information about Oman in terms of and not limited to income, population , political environment...
Write background information about Oman in terms of and not limited to income, population , political environment , social environment, purchaing power, and GDP, and rules and regulation to attract investors from local and out side. (in 3 pages)
write an introduction about KODAK and in a separate paragraphs write the background about kodak.
write an introduction about KODAK and in a separate paragraphs write the background about kodak.
For this exercise, you are going to write your code in the FormFill class instead of...
For this exercise, you are going to write your code in the FormFill class instead of the main method. The code is the same as if you were writing in the main method, but now you will be helping to write the class. It has a few instance variables that stores personal information that you often need to fill in various forms, such as online shopping forms. Read the method comments for more information. As you implement these methods, notice...
Write a Java program that implements a song database. The SongsDatabase class keeps tracks of song...
Write a Java program that implements a song database. The SongsDatabase class keeps tracks of song titles by classifying them according to genre (e.g., Pop, Rock, etc.). The class uses a HashMap to map a genre with a set of songs that belong to such a genre. The set of songs will be represented using a HashSet. Your driver output should sufficiently prove that your code properly implements the code below. public class SongsDatabase { private Map<String, Set<String>> genreMap =...
provide background information about energy production in plants
provide background information about energy production in plants
Write about background of Tonga and its macroeconomics performance.
Write about background of Tonga and its macroeconomics performance.
how to write the background of information technology for business in the assignment.
how to write the background of information technology for business in the assignment.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT