Answered step by step
Verified Expert Solution
Question
1 Approved Answer
#Please Answer in Python Codeblock Create a class called Nation to represent a single nation. Your class should have the following attributes: Four instance variables:
#Please Answer in Python Codeblock
Create a class called Nation to represent a single nation. Your class should have the following attributes:
- Four instance variables: country, continent, population and land_area, all stored as strings
- A class variable _num_instances to keep track of how many Nation object instances have been created
- A constructor (__init__) that sets all four instance variable using to the given parameters and increments _num_instances by one.
- Remember class variables are accessed in a non-class method by prepending self.__class__. before the name of the class variable.
- A class method num_instances to return the value of _num_instances.
- __str__ defined to return a string representation of a Nation comprised of the values of the instance variables.
- A method population_density that computes the population density of the nation (population / land area).
- Note that the instance variables are strings and must be converted to floats to perform this calculation.
Test your Nation class by manually creating an instance called usa with country "United States", continent "North America", population "318.9", and land area "3794066". Print the variable usa to observe the string representation given by __str__. Print the population density and number of instances as well.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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