Question

Need help with python assignment

Create a class called Book that stores a book's ti

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

1.

Program (Code):

class Book:
   title="" # to store the title
   author="" # to store the author name
   price=0.0 # to store the price
   def __init__(self,title,author,price): # constructor to initialise the above variables
       self.title=title
       self.author=author
       self.price=price
def main(): # main method
   obj=Book("Java","Sangam",5.0) # object creation
   print obj.title
   print obj.author
   print obj.price

if __name__ == "__main__":
   main()

Program (Screenshot):

1 class Book: title- # to store the title author. # to store the author name price-0.0 # to store the price def-init-(se

Output:

C:WINDOWS system321cmd.exe C:NUser Sunny Desktop>python Book.py Java Sangam Users Sunny Desktop>

2.

Program (Code):

def main(): # main function
   while 1: # infinte while loop
       num=raw_input("enter a number to get its square:") # scanning input in string format
       if num.isdigit(): # isdigit() returns true if string contains only digits
           num=int(num) # converting input in string formta to integer
           print num*num # computing square of the number
           break
if __name__ == "__main__":
   main()

Program (Screenshot)

def main(): # main function while 1: # infinte while loop num-raw-input(enter a number to get its square :) # scanning inpu

Output:

C:WINDOWS system321cmd.exe C:NUser Sunny Desktop>python test.py enter a number to get its square mc enter a number to get its

I hope you understood the program.Feel free to contact for any further doubts.Thanks.

Add a comment
Know the answer?
Add Answer to:
Need help with python assignment Create a class called Book that stores a book's title, author,...
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
  • Create a Python file (book.py) that has a class named Book. Book class has four attributes, title...

    Create a Python file (book.py) that has a class named Book. Book class has four attributes, title (string), author(string), isbn(string), and year(int). Title - the name of the book. Author - the name of the persons who wrote the book. ISBN - is a unique 13 digit commercial book identifier. Year - the year the title was printed. Your class should have the following methods: (1) __init__(self, title, author, isbn, year): This method called when an object (book) is created...

  • Create a Python file (book.py) that has a class named Book. Book class has four attributes,...

    Create a Python file (book.py) that has a class named Book. Book class has four attributes, title (string), author(string), isbn(string), and year(int). Title - the name of the book. Author - the name of the persons who wrote the book. ISBN - is a unique 13 digit commercial book identifier. Year - the year the title was printed. Your class should have the following methods: (1) __init__(self, title, author, isbn, year): This method called when an object (book) is created...

  • Write a definition for a class named Book with attributes title, price and author, where author...

    Write a definition for a class named Book with attributes title, price and author, where author is a Contact object and title is a String, and price is a float number. You also need to define the Contact class, which has attributes name and email, where name and email are both String. Instantiate a couple of Book objects that represents books with title, price and author (You can come up with the attributes values for each object) Write a function...

  • cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher,...

    cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher, price, and copyright date. Define the Book constructor to accept and initialize this data. Include setter and getter methods for all instance data. Include a toString method that returns a nicely formatted, multi-line description of the book. Write another class called Bookshelf, which has name and array of Book objects. Bookself capacity is maximum of five books. Includes method for Bookself that adds, removes,...

  • A java class named Book contains: instance data for the title, author, publisher and copyright year...

    A java class named Book contains: instance data for the title, author, publisher and copyright year a constructor to accept and initialize the instance data variables set and get methods a toString() method to return a formatted, multi-line description of the book Produce a child class that inherits from Book. The class is called Dictionary. Dictonary must include: instance data that describes the number of words in the dictionary a constructor that takes in all information needed to describe a...

  • Create a class called AddressBook. It should be able to store a maximum of 50 names...

    Create a class called AddressBook. It should be able to store a maximum of 50 names and 50 phone numbers (make an array of structures in the object or two arrays). It should contain a method called addName that will take a name and phone number and add it to the address book. There should be a method called findName that will take a string for the name and return the phone number if found in the list otherwise return...

  • (JAVA NetBeans) Write programs in java Example 9.13~9.15 //Book.Java public class Book { private String title; private...

    (JAVA NetBeans) Write programs in java Example 9.13~9.15 //Book.Java public class Book { private String title; private String author; private double price; /** * default constructor */ public Book() { title = ""; author = ""; price = 0.0; } /** * overloaded constructor * * @param newTitle the value to assign to title * @param newAuthor the value to assign to author * @param newPrice the value to assign to price */ public Book(String newTitle, String newAuthor, double newPrice)...

  • Now create the Book class that inherits from Product. Don't forget that each class must go...

    Now create the Book class that inherits from Product. Don't forget that each class must go in a file with a matching name, so put the Book class in Book.java. In the main method create a new Book object. Use setters to set the author, title and number Pages. Use the getters to print information about the book. Product Felda stock Number price Tethods getto accettera.. Book Food Fields title Pages Hethods etters and setters Fields expiration Year expiration Day...

  • Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use impl...

    Implement the classes in the following class diagram. The Book class implements the Comparable interface. Use implements Comparable<Book> in the class definition. Now, all book objects are instances of the java.lang.Comparable interface. Write a test program that creates an array of ten books. 1. Use Arrays.sort( Book[]l books) from the java.util package to sort the array. The order of objects in the array is determined using compareTo...) method. 2. Write a method that returns the most expensive book in the...

  • Create a Python class named Phonebook with a single attribute called entries. Begin by including a...

    Create a Python class named Phonebook with a single attribute called entries. Begin by including a constructor that initializes entries to be an empty dictionary. Next add a method called add_entry that takes a string representing a person’s name and an integer representing the person’s phone number and that adds an entry to the Phonebook object’s dictionary in which the key is the name and the value is the number. For example: >>> book = Phonebook() >>> book.add_entry('Turing', 6173538919) Add...

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