SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
Classes And Object Oriented Programming
Exercise:
Algorithm Workbench
Question:2 | ISBN:9780132576376 | Edition: 2

Question

a class definition named Book. The Book class should have data attributes for a book’s title, the author’s name, and the publisher’s name. The class should also have the following:
a. An __init__ method for the class. The method should accept an argument for each of the data attributes.
b. Accessor and mutator methods for each data attribute.
c. An __str__ method that returns a string indicating the state of the object.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

class Book:
    def __init__(self, title, author, publisher):
        """
        Initializes a Book object with the given title, author, and publisher.

        Args:
            title (str): The title of the book.
            author (str): The author's name.
            publisher (str): The publisher's name.
        """
        self.title = title
        self.author = author
        self.publisher = publisher

    def get_title(self):
        """
        Returns the title of the book.

        Returns:
            str: The title of the book.
        """
        return self.title

    def set_title(self, title):
        """
        Sets the title of the book.

        Args:
            title (str): The new title of the book.
        """
        self.title = title

    def get_author(self):
        """
        Returns the author's name.

        Returns:
            str: The author's name.
        """
        return self.author

    def set_author(self, author):
        """
        Sets the author's name.

        Args:
            author (str): The new author's name.
        """
        self.author = author

    def get_publisher(self):
        """
        Returns the publisher's name.

        Returns:
            str: The publisher's name.
        """
        return self.publisher

    def set_publisher(self, publisher):
        """
        Sets the publisher's name.

        Args:
            publisher (str): The new publisher's name.
        """
        self.publisher = publisher

    def __str__(self):
        """
        Returns a string indicating the state of the Book object.

        Returns:
            str: The string representation of the Book object.
        """
        return f"Title: {self.title}\nAuthor: {self.author}\nPublisher: {self.publisher}"


# Create a Book object and test the methods
book1 = Book("Starting Out With Python", "Tony Gaddis", "ABC Publishers")
print(book1)  # Prints the string representation of the Book object

book1.set_title("Python Learn")  # Update the title
print(book1.get_title())  # Prints the updated title

book1.set_author("Charless Doe")  # Update the author
print(book1.get_author())  # Prints the updated author

book1.set_publisher("XYZ Publications")  # Update the publisher
print(book1.get_publisher())  # Prints the updated publisher

Executed Output:

Title: Starting Out With Python
Author: Tony Gaddis
Publisher: ABC Publishers
Python Learn
Charless Doe
XYZ Publications

 

0 0

Discussions

Post the discussion to improve the above solution.