Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Create a new class called Author. Create a regular class, not a dataclass. An Author has a name, and a list of books published.
Create a new class called Author. Create a regular class, not a dataclass. An Author has a name, and a list of books published. When you create a new Author, they don't have any books. So create an empty books list attribute in the _init_method. Your Author class should have a publish method, which takes the title of a book as an argument. Add the title of this book to this object's books list. Add astr method that returns a String with the author's name, and the names of all of their book's titles. Write a main function to test your class, create some example authors, and publish some example books. Part 2: Author class - no duplicate books Start with the program from part 1. In this version, an author can't publish two books with the same name. When the publish method is called, print an error message if the book given has the same name as a book currently in the books list. (In other words, make sure the Author object's book list doesn't already contain that name). In your breakout rooms: there's more than one way to solve this - can you come up with two different solutions? Part 3: Student dataclass. Type in the dataclass code from the slides/video. You would have done this before class. Add one more field: gpa, a float. Write a main function to create some example Student objects with some example names, college_id and GPA values. Verify you can read the name, college ID and GPA for an example student. Verify when you print an example student, the GPA is included. Add some comments in your code to compare the dataclass version to the "traditional" version.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Part 1 Author class class Author def initself name selfname name selfbooks create an empty list attribute to store the books def publishself title if ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started