19.15. Mixed-Up Code Exercises

Construct a block of code that creates a class called ‘Person’and defines the __init__ method with one parameter, “name”. Then define the personName method, which returns the name when the method is called. Watch out for indentation and extra pieces of code.

Construct a class named Person. The __init__ method has parameters of “name”, “age” and “gender”. Also create the method updateAge, that increases the age by 1. Then update the age of James. Watch out for indentation and extra pieces of code.

Construct a block of code to create the class Cat. The __init__ method has “name” and “age” as parameters. Then define the “make_sound” method, returning the cat’s meow and the __str__ method returns a string of the cat’s introduction. Watch out for indentation and extra pieces of code.

Construct a class named ‘Book’ that has an __init__ method with the parameters “title” and “author”. Then create an instance of the ‘Book’ class named newbook.

Construct a class named ‘Dog’ with the __init__ method taking name as its parameter. Then create a method, updateTricks, that adds a new trick to the list and a final string method that returns all information for the dog. Print both the tricks and the string for “sparky” at the end.

Construct a class named “Book” that assigns values to the “title”, “author” and “info” object properties. Then, add in the method “__str__” that returns the string representation of the class. Create an object “Iliad” for that book and print the string associated with it.

Construct a class named “Dog” assigns the “name”, “age” and “breed” values. Then, create a the method “__str__” to return the string representation of the class. Finally add a method “isGoldenRetriever” to check whether the dog is a Golden Retriever.

Construct a class named “Book” that assigns “title”, “author” and “availability” format as initial values. Add a method named updateAvailability that updates availability of the book, and a method named “__str__” that returns the string representation of the class.

Construct a class named “Apple” that assigns kind, color and total available as initial values and a method named “__str__” that returns the string representation of the “Apple” class. Write a method named “updateTotal” that updates the total available apples.

Construct a class named “Vehicle” that assigns “type”, “color”, “price” and “sold” as initial values and a method named “__str__” that returns the string representation of the class. Construct a method updatePrice that changes the price of the vehicle. Add another method isSold if the vehicle is sold to a person.

You have attempted of activities on this page