Question

In: Computer Science

This task is about classes and objects, and is solved in Python 3. We will look...

This task is about classes and objects, and is solved in Python 3.

We will look at two different ways to represent a succession of monarchs, e.g. the series of Norwegian kings from Haakon VII to Harald V, and print it out like this:

King Haakon VII of Norway, accession: 1905

King Olav V of Norway, accession: 1957

King Harald V of Norway, accession: 1991

>>>

Make a class Monarch with three attributes: the name of the monarch, the nation and year of accession. The class should have a method write () which prints out the information on a line like in the example. It should also have the method _init_ (…), so that you can create a Monarch instance by name, nation and year of accession as arguments. Example:

haakon = Monarch("Norway","King Haakon VII",1905)

Here, the variable haakon will be assigned to the Monarch object for King Haakon VII. Now the series of kings can be represented in a variable series of kings, that contains a list of the three monarchs, in the right order. Finally, the program will print the series of kings as shown above.

Solutions

Expert Solution

class Monarch():
def __init__(self,name,nation,year):
self.name=name
self.nation=nation
self.year=year
def write(self):
print('King {} of {}, accession: {}'.format(self.name,self.nation,self.year))
  
  
haakon = Monarch("Norway","King Haakon VII",1905)
olav = Monarch("Norway","King Olav V",1957)
harald = Monarch("Norway","King Harald V",1991)

haakon.write()
olav.write()
harald.write()


Related Solutions

This is Python programming Focus 1. Classes and Objects 2. Creating objects 3. Manipulating objects This...
This is Python programming Focus 1. Classes and Objects 2. Creating objects 3. Manipulating objects This lab maps to learning the following objectives: Write a working program that creates a Class, Instances of Objects from that Class, and Functions that use Objects as parameters. For this portion of the lab, you will create a new program for your Professor. Create a class named Student that holds the following data about a student: 1. Name 2. Student ID number 3. GPA...
Homework 3 – Programming with C++ What This Assignment Is About: • Classes and Objects •...
Homework 3 – Programming with C++ What This Assignment Is About: • Classes and Objects • Methods • Arrays of Primitive Values • Arrays of Objects • Recursion • for and if Statements • Insertion Sort 2. Use the following Guidelines • Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc). • User upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for...
11) When we solved problems dealing with objects in free-fall, we often exploited a symmetry that...
11) When we solved problems dealing with objects in free-fall, we often exploited a symmetry that led to the conclusion that the time it takes an object to go from a starting height to a maximum height (where the vertical (y-direction) velocity is zero) is the same as the time that it takes for the object to fall from that maximum height back to the starting height. If we introduce air resistance as a force similar to a friction force,...
Explain speed of light, and why, when we look at celestial objects, we are not seeing...
Explain speed of light, and why, when we look at celestial objects, we are not seeing them the way they are right now but the way they were in the past.
LOOK over code Python The task aims to develop a Kalman filter that is able to...
LOOK over code Python The task aims to develop a Kalman filter that is able to hit the moving target (the pink box) in as many situations as possible. However, there are some limitations: IT IS NOT PERMITTED TO CHANGE THE PRODEIL AND TARGET CODE IT IS ALSO NOT ALLOWED TO CHANGE THE GAME LOOP, OTHER THAN WHAT HAS BEEN COMMENTS SHALL BE CHANGED WHEN THE Kalman CLASS IS IMPLEMENTED I have made the callman class, but my question is;...
Using Python to play Checkers: 1) Create classes to represent the necessary objects for game play....
Using Python to play Checkers: 1) Create classes to represent the necessary objects for game play. This will include, at least, the game board, the two types of pieces and the two sides. You will need to determine the best way to represent the relationship between them. 2) Set up one side of the board. Print the status of the board.
Need this program Using Classes , Objects, Save to file, and Printbill Simple python assignment Write...
Need this program Using Classes , Objects, Save to file, and Printbill Simple python assignment Write a menu-driven program for Food Court. (You need to use functions!) Display the food menu to a user (Just show the 5 options' names and prices - No need to show the Combos or the details!) Ask the user what he/she wants and how many of it. (Check the user inputs) AND Use strip() function to strip your inputs. Keep asking the user until...
Approaching the task as a "judgement to be made" instead of a "problem to be solved"...
Approaching the task as a "judgement to be made" instead of a "problem to be solved" can reduce the Common Information Effect. True False Conflict is harmful to a team. True False Highly feasible ideas that are high value result in: breakthrough disruption foolishness radical Teams that engage in after-action review are more likely to have the following, EXCEPT Better team performance. Stronger processes to begin with. Enhanced communication. Higher team cohesion. Which of the following is effective in reducing...
A. What are Objects? B. How do Objects differ from Classes? C. Where are Objects stored...
A. What are Objects? B. How do Objects differ from Classes? C. Where are Objects stored in Memory? D. Why do you not need to declare when you are finished using an Object in Java? E. Can you edits the contents of a String? View keyboard shortcuts EditViewInsertFormatToolsTable 12pt Paragraph
How do objects enhance Java? How do objects relate to classes and methods?
How do objects enhance Java? How do objects relate to classes and methods?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT