Starting Out with Python (4th Edition)
4th Edition
ISBN: 9780134444321
Author: Tony Gaddis
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Textbook Question
Chapter 11.2, Problem 5CP
Look at the following class definitions:
class Vegetable:
def _ _init_ _(self, vegtype):
self._ _vegtype = vegtype
def message(self):
print("I'm a vegetable.")
class Potato(Vegetable):
def_ _init_ _(self):
Vegetable._ _init_ _(self, 'potato')
def message(self):
print("I'm a potato.")
Given these class definitions, what will the following statements display?
v = Vegetable('veggie')
p = Potato()
v.message()
p.message ()
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Look at the following class definitions:class Plant:def _ _init_ _(self, plant_type):self._ _plant_type = plant_typedef message(self):print("I'm a plant.")class Tree(Plant):def _ _init_ _(self):Plant._ _init_ _(self, 'tree')def message(self):print("I'm a tree.")Given these class definitions, what will the following statements display?p = Plant('sapling')t = Tree()p.message()t.message()
Parent Class: Food
Write a parent class called Food. A food is described by a name, the number of grams of sugar (as a whole number), and the number of grams of sodium (as a whole number).
Core Class Components
For the Food class, write:
the complete class header
the instance data variables
a constructor that sets the instance data variables based on parameters
getters and setters; use validity checking on the parameters where appropriate
a toString method that returns a text representation of a Food object that includes all three characteristics of the food
Class-Specific Method
Write a method that calculates what percent of the daily recommended amount of sugar is contained in a food. The daily recommended amount might change, so the method takes in the daily allowance and then calculates the percentage.
For example, let's say a food had 6 grams of sugar. If the daily allowance was 24 grams, the percent would be 0.25. For that same food, if the daily allowance was 36 grams, the…
class SavingsAccount(object):
"""This class represents a savings account
with the owner's name, PIN, and balance."""
RATE = 0.02
def __init__(self, name, pin, balance = 0.0):
self._name = name
self._pin = pin
self._balance = balance
def __str__(self):
result = 'Name: ' + self._name + '\n'
result += 'PIN: ' + self._pin + '\n'
result += 'Balance: ' + str(self._balance)
return result
def getBalance(self):
return self._balance
def getName(self):
return self._name
def getPin(self):
return self._pin
def deposit(self, amount):
"""Deposits the given amount."""
self._balance += amount
return self._balance
def withdraw(self, amount):
"""Withdraws the given amount.
Returns None if successful, or an
error message if unsuccessful."""
if amount < 0:
return 'Amount must be >=…
Chapter 11 Solutions
Starting Out with Python (4th Edition)
Ch. 11.1 - In this section, we discussed superclasses and...Ch. 11.1 - Prob. 2CPCh. 11.1 - What does a subclass inherit from its superclass?Ch. 11.1 - Look at the following code, which is the first...Ch. 11.2 - Look at the following class definitions: class...Ch. 11 - In an inheritance relationship, the ___________ is...Ch. 11 - In an inheritance relationship, the _________ is...Ch. 11 - Suppose a program uses two classes: Airplane and...Ch. 11 - This characteristic of object-oriented programming...Ch. 11 - Prob. 5MC
Ch. 11 - Polymorphism allows you to write methods in a...Ch. 11 - It is not possible to call a superclasss _ _init_...Ch. 11 - A subclass can have a method with the same name as...Ch. 11 - Only the _ _init_ _method can be overridden.Ch. 11 - You cannot use the isinstance function to...Ch. 11 - What does a subclass inherit from its superclass?Ch. 11 - Look at the following class definition. What is...Ch. 11 - Prob. 3SACh. 11 - Write the first line of the definition for a...Ch. 11 - Look at the following class definitions: class...Ch. 11 - Look at the following class definition: class...Ch. 11 - Employee and ProductionWorker Classes Write an...Ch. 11 - ShiftSupervisor Class In a particular factory, a...Ch. 11 - Person and Customer Classes The Person and...
Additional Engineering Textbook Solutions
Find more solutions based on key concepts
Is the following program legal (assuming appropriate #include and using directives are added)? void showEmploye...
Problem Solving with C++ (9th Edition)
Finish the program that takes a word as a command-line argument and looks up the word to see whether it is in t...
Programming in C
Consider a program that will keep track of the items in a schools library. Draw a class hierarchy, including a ...
Java: An Introduction to Problem Solving and Programming (7th Edition)
For each of the following E-R diagrams from Chapter 2 C: Transform the diagram to a relational schema that show...
Modern Database Management (12th Edition)
What is denormalization?
Database Concepts (7th Edition)
Suppose the registers 0x4 and 0x5 in the Vole contain the bit patterns 0x3A and 0xC8, respectively. What bit pa...
Computer Science: An Overview (13th Edition) (What's New in Computer Science)
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Similar questions
- A class variable does not belong to a particular object. It belongs to the entire class. Which field below is an example of a class variable? public class Pizza { private static int numPizzas; private static int numRestaurants; private static double reheatTemperature; private static string foodGroup; private string name; private string[] toppings; private double calories; private double price; } price foodGroup calories namearrow_forwardFill-in-the-Blank A class may have more than one constructor, as long as each has a different_________.arrow_forwardC # Write the code to create a class called Pet that can store the information about a pet: Example: a female dog named Molly who is 6 years old. Include at least 1 constructor that doesn't allow age to be negativearrow_forward
- We use the _____ operator to create an instance of (object in) a particular class.arrow_forwardPlease code in python Create a program with two classes – the person class and the student The person class has the following properties: first name (first_name), last name (last_name) street address (address) city (city) zip code (zip) The class has the following methods: get_full_name, which returns the full name of a person get_full_address, which return greeting, returns a greeting message. The class should provide accessor and mutator for each property The class should override the __str__ method to return the state of the object. Create a child class called student which has a property named graduation year (graduation_year) and major. Provide accessor and mutator for each property of its own It inherits all the properties and methods of the person parent class as well. Create a test program that Create an object of the person class and print the full name of a person. Create an object of the person class and print…arrow_forwardFill-in-the-Blank A constructor that takes a single parameter of a type different from the class type is a __________ constructor.arrow_forward
- A constructor that takes a single parameter of a type different from the class type is a_________ constructor.arrow_forwardT/F: Instance variables are shared by all the instances of the class. T/F: The scope of instance and static variables is the entire class. They can be declared anywhere inside a class. T/F: To declare static variables, constants, and methods, use the static modifier.arrow_forwardPython Programming A- Define a class called Vehicle as follows: The Vehicle class has two instance variables: name, max_speed Define an __init__ function for the Vehicle class that initializes the name and max_speed instance variables Define get and set methods for each of the name and max_speed variables. You may call them getName, getMax_speed, setName and setMax_speed Define a __str__ function that prints the name and max_speed of the Vehicle class. B- Define a class called Car as follows: The Car class is a subclass of the Vehicle class. The Car class has an instance variable called number_of_cylinders Define an __init__ function for the Car class that initializes the name and max_speed and number_of_cylinders instance variables Please note that the Car class inherits the name and max_speed variables from the Vehicle class and does not redefine them. Define get and set methods for the number_of_cylinders variable. Define a __str__ function that prints the name and…arrow_forward
- Fill in the blank: A(n)________specifies how a class member can be accessed by code outside the class.arrow_forwardIn order to extend a class, the new class should have access to all the data and inner workings of the parent class. True Falsearrow_forwardQuestion 11 View the following class definition then answer the question that follows. class Date (): def _init_ (self, day, month, year, descr): self.month = month self. day = day self.year = year self.descr = descr def _str_ (self): return str(self.month) + / + str(self.day) + / + str(self.year) +-+ self.descr d1 = Date('01', '04', '2022', "Important date') print(d1) What is the display output of the above code? O 01/04/2022 - Important date O 2022/04/01 - Important date O 01/2022/04 - Important date O 04/01/2022 - Important date O None of the listed answers is correctarrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTProgramming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:Cengage
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Introduction to Classes and Objects - Part 1 (Data Structures & Algorithms #3); Author: CS Dojo;https://www.youtube.com/watch?v=8yjkWGRlUmY;License: Standard YouTube License, CC-BY