1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Gekata [30.6K]
3 years ago
4

Write a class named Book containing:

Computers and Technology
1 answer:
Verizon [17]3 years ago
5 0

Answer:

class Book:

       def __init__(self, title, author):

               self.title = title

               self.author = author

               self.tableOfContents = ''

               self.nextPage = 1

       def addChapter(self, title, numberOfPages):

               self.tableOfContents += '\n{}...{}'.format(title, self.nextPage)

               self.nextPage += numberOfPages

       def getPages(self):

               return self.nextPage

       def getTableOfContents(self):

              return self.tableOfContents

       def toString(self):

              return '{}\n{}'.format(self.title, self.author)

book1 = Book('Learning Programming with Python', 'Andrew')

Explanation:

NB: Please do not ignore the way the code snippet text was indented. This is intentional and the Python interpreter uses spaces/indentation to mark the start and end of blocks of code

Python is a language that supports Object Oriented Programming(OOP). To define a constructor in a Python class, we make use of the syntax:

def __init__(self)

When an object is instantiated from the class, the __init__ method which acts as the constructor is the first method that is called. This method basically is where initialisation occurs. Consider this line of code in the snippet above:

book1 = Book('Learning Programming with Python', 'Andrew'). Here, book1 is an object of the class Book and during the creation of this instance, the title attribute was <em>Learning Programming with Python</em> and the author attribute was <em>Andrew. </em>

Now with the book1 object or instance created, we can now call different methods of the Book class on the instance like so:

book1.getPages()

The above runs the getPages function in the Book class. Notice that this method although has a self attribute in the function, this was not called: book1.getPages() evaluation. The idea behind that is the instance of the class or the object of the class is represented by the self attribute. The concepts of OOP can be overwhelming at first but it is really interesting. You can reach out to me for more explanation on this subject and I will be honoured to help out.

You might be interested in
What is a subjective point of view
anastassius [24]

Answer:

one based on opinion rather than fact, upon which reasonable people could disagree.

Explanation:

An example of a subjective point of view is a position taken on whether a movie or book is good or bad

6 0
3 years ago
Read 2 more answers
Please help it's my last question
Yuki888 [10]

Explanation:

here is your answer.. of. different between client / server architecture and peer to peer architecture of the network.

6 0
3 years ago
Read 2 more answers
Sa se sorteze liniile unui fisier in ordine alfabetica (c sau c++)
nydimaria [60]

...................................................................................

8 0
3 years ago
Computer science allows people to:
Sloan [31]

Your answer will be All of the above!!

5 0
4 years ago
Read 2 more answers
Which statement opens a text file so that you can retrieve the information it contains?
vesna_86 [32]

Answer:

Answered below

Explanation:

aFile = open("books.txt", "r")

This code uses the function open() which takes two parameters. The first parameter is the file name while the second parameter is the mode in which you are accessing the file.

The "r" mode opens the file in a reading state. That is, you can only read from the file. This code completes the reading process

aFile.read( )

The "w" mode opens the file so you can write to it and make changes.

The "a" mode opens the file so you can add contents to it.

3 0
3 years ago
Other questions:
  • While designing a website, a(n) ____ allows you to identify the specific amount of space used on a particular page before it con
    15·2 answers
  • Should i buy the Samsung s10+ or wait for s11 or any other phones you guys suggest?
    6·2 answers
  • Se citește un număr natural nenul N. Se umple, pe linii, partea de sub diagonală, inclusiv aceasta, a unui tabel pătratic de dim
    9·1 answer
  • When you add text to a slide using presentation software, which of the following is important?
    12·2 answers
  • Before entering a road from an alley or driveway, you must:Flash your high beam headlights 
    13·1 answer
  • What piece of equipment is NOT a tool that would be considered appropriate equipment for on-site visits? a. multimeter b. flashl
    14·1 answer
  • A (n) _____________ chart is represented by a circle divided into portions.
    8·1 answer
  • Information has just been sent from a Web form on a client's system to a Web server. Which of the following is required to proce
    14·1 answer
  • Mary is working on joining of a new domain tree to an existing forest, but before starting the process, she should have at least
    7·1 answer
  • Both Wireshark and NetWitness Investigator can be used for packet captures and analysis. Which tool is preferred for each task,
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!