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...
This task is solved in Python 3. Develop a function which counts the number of vowels...
This task is solved in Python 3. Develop a function which counts the number of vowels in a text. >>> numberofVowels ("There is a cat outside the house") 13
This task is solved in Python 3. Develop a program that can change the phone number...
This task is solved in Python 3. Develop a program that can change the phone number of a person in phone.txt (hint: the easiest thing is probably to make a new version of the file, and then delete the old one before the new is renamed to phone.txt) Name: John Old phone number: 99776612 New number: 99889999 >>> phone.txt Replaced by ------> phone.txt Mary 98654321 June 99776655 Chris 99112233 Viv 98554455 John 99776612 Joe 97888776 Rick 99455443 Susan 98122134 Jill...
This task is solved in Python 3. Develop a program that lets the user add new...
This task is solved in Python 3. Develop a program that lets the user add new people to the file phone.txt Add name and number, end with <enter> Name and number: Robin 94567402 Name and number: Jessica 99468283 Name and number: >>> Phone.txt Expanded to ------> Phone.txt Mary 98654321 June 99776655 Chris 99112233 Viv 98554455 John 99776612 Joe 97888776 Rick 99455443 Susan 98122134 Jill 99655732 Bob 98787896 Mary 98654321 June 99776655 Chris 99112233 Viv 98554455 John 99776612 Joe 97888776 Rick...
This task is solved in Python 3. Develop a program that can, among other things, manage...
This task is solved in Python 3. Develop a program that can, among other things, manage your courses and course results. It must also be able to calculate the average grade, for all grades or for a selection of grades limited to a certain subject area and / or course level (100, 200 or 300 level). NB! All courses are worth the same amount of credits. The program should have two global collections Courses is a list that contains the...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT