Sunday, August 9, 2020

 Chapter-14 : Solution of Object oriented Programming Question


Question-1:- Write a class called Investment with fields called principal and interest. The constructor should set the values of those fields. There should be a method called value_after that returns the value of the investment after n years. The formula for this is p(1+i)**n, where p is the principal, and i is the interest rate. It should also use the special method __str__ so that printing the object will result in something like below:

Principal - $1000.00, Interest rate - 5.12%

Solution:-


Explanation:-  If you read Previous blog then you easily understand this. If not able to understand then  comment.


Question-2: Write a class called Product. The class should have fields called name, amount, and price, holding the product’s name, the number of items of that product in stock, and the regular price of the product. There should be a method get_price that receives the number of items to be bought and returns a the cost of buying that many items, where the regular price is charged for orders of less than 10 items, a 10% discount is applied for orders of between 10 and 99 items, and a 20% discount is applied for orders of 100 or more items. There should also be a method called make_purchase that receives the number of items to be bought and decreases amount by that much.

Solution:-



Example-3:  Write a class called Password_manager. The class should have a list called old_passwords that holds all of the user’s past passwords. The last item of the list is the user’s current password. There should be a method called get_password that returns the current password and a method called set_password that sets the user’s password. The set_password method should only change the password if the attempted password is different from all the user’s past passwords. Finally, create a method called is_correct that receives a string and returns a boolean True or False depending on whether the string is equal to the current password or not.

Solution:-


Explanation:- Here first we make a file by password name which contain random password. Then we call the file by old=open('password.txt').read() and then to convert the file into list use old.split(). Then further process easy if you read Chapter-7 List.


Example-4: Write a class called Time whose only field is a time in seconds. It should have a method called convert_to_minutes that returns a string of minutes and seconds formatted as in the following example: if seconds is 230, the method should return '5:50'. It should also have a method called convert_to_hours that returns a string of hours, minutes, and seconds formatted analogously to the previous method.

Solution:


Example-5:  Write a class called Wordplay. It should have a field that holds a list of words. The user of the class should pass the list of words they want to use to the class. There should be the following methods:

• words_with_length(length) — returns a list of all the words of length

• starts_with(s) — returns a list of all the words that start with s 

• ends_with(s) — returns a list of all the words that end with s

 • palindromes() — returns a list of all the palindromes in the list 

• only(L) — returns a list of the words that contain only those letters in L 

• avoids(L) — returns a list of the words that contain none of the letters in L


Solution:-


Explanation:- First of all we make a file with some written paragraph. Then we call this file in python and then further process easy because contain the same portion of Chapter-6 (String) and Chapter-7 (List)


Example-6:- Write a class called Converter. The user will pass a length and a unit when declaring an object from the class—for example, c = Converter(9,'inches'). The possible units are inches, feet, yards, miles, kilometers, meters, centimeters, and millimeters. For each of these units there should be a method that returns the length converted into those units. For example,using the Converter object created above,the user could call c.feet() and should get 0.75 as the result

Solution:-


Explanation:- This is not complete solution because user can choose any unit but here limited unit. So, by use if command like I use write all the left unit. In Question about 8 unit and first you fix one unit and then write all the command it about 8*8=64 command. But, here I write only 7 command lines in solution. So, write left 64-7=57 command then it will be perfect calculator.


Example-7:- Use the Standard_deck class of this section to create a simplified version of the game War. In this game, there are two players. Each starts with half of a deck. The players each deal the top card from their decks and whoever has the higher card wins the other player’s cards and adds them to the bottom of his deck. If there is a tie, the two cards are eliminated from play (this differs from the actual game, but is simpler to program). The game ends when one player runs out of cards.

Solution:-


Explanation:- This solution is also not complete because here I divide deck into two part and then take the value of card and then add all the value and find who win. But, according to question first you take first card of the both user and then compare card, those get higher number he will win and put the card to bottom then choose 2nd card like that way find the result. If the card value same then remove the card. So, It is lengthy process I give you hint by this example and try yourself to solve this question.


Example-8:- Write a class called Rock_paper_scissors that implements the logic of the game Rock paper-scissors. For this game the user plays against the computer for a certain number of rounds. Your class should have fields for the how many rounds there will be, the current round number, and the number of wins each player has.There should be methods forgetting the computer’s choice,finding the winner of around,and checking to see if someone has one the (entire) game. You may want more methods.

Solution:-


Hope you like these solution, You can use your trick to solve these question. May be you solve these question in some easy way with less number of code. Ultimate, thing is to decrease the length of code and find the better result.


If you have any query, mention in comment section.


No comments:

Post a Comment

If you have any doubt, let me know.

How and Why you should learn python? About Python Programming Language Python is the programming language. By the help of Python you can ...