Programming Homework Help

Attributes & Properties in a Car Class Python Program

 

Part A

1. Write a Car class. This car should have the following attributes and properties:

A class variable called wheels with value 4 (since all cars have four wheels).
An instance variable make (string), e.g. “Toyota” , “Subaru” , “Chevy” ).
An instance variable model (string), e.g. “Corolla” , “Outback” , “Tahoe “).
An instance variable year (integer) the year the car was manufactured e.g. 2010).
An instance variable miles_per_gallon (float) the number of miles the car can drive per gallon of gas: e.g. 18.0)
An instance variable current_gas_level (float), the number of gallons currently in the tank of the car.

Prompt

  1. Your function should have an __init__ method which checks that make and model are strings, that year is an integer which is greater than 1900, that current_gas_level and miles_per_gallon are floats or ints, and that current_gas_level ! 0. You should also check that miles_per_gallon >0.Make sure to raise informative errors if any of these checks fail. You shouldappropriately use both type errors and value errors in your solution.
  2. Write a drive() method. This method should accept an argumentnumber_of_miles giving the desired number of miles to drive. The current_gas_level should be reduced by an appropriate amount. If the current_gas_level is not sufficient to drive the specified number of miles, thenraise an informative error of an appropriate type, and do not change current_gas_level .
  3. Demonstrate one example of successfully constructing an object of class Car and three different examples of raising an error from checks in the __init__ method.
  4. Demonstrate one example of sucessfully calling the drive method, printing the current_gas_level before and after. Demonstrate one example in which there isnot enough gas and the corresponding error is raised.

Specs
Comments and docstrings, please!

  1. Remember that the class itself should have a docstring describing its overall purpose, instance variables, and methods.
  2. Each method (with the possible exception of __init__ should have a docstring describing its purpose, inputs, and outputs/other effects

# define your class here : Show the required examples in the code blocks below. Feel free to make new code blocks as needed.

#demo 1 :

#demo 2 :

#demo3 :

#demo4:

# demostrate successful driving here:

# demonstrate unsuccessful driving here:

# (not enough gas)

Part B

Write a Motorcycle class that possesses the same class variables, instance variables, and drive() method as the Car class.

Demonstrate a working example of an object of class Motorcycle . Then, print the wheels instance variable of the object to verify that the object has the correct number of

wheels (which should be different from a Car ). Note: motorcycles have two wheels.

Specs

Your solution should be under five lines long.

Comments and docstrings are not required for this part

# class definition here

# contruct object of class

#show number of wheels

Part C

Write a function called age whose input is an object of class Car and whose output is the age of that object, computed according to the formula

age = 2021 – year manufactured

Your function should check that the input is an instance of class Car . This implies that it should also work for objects of class Motorcycle , and other classes that inherit from Car .

Demonstrate that your function works and raises an appropriate error when the input is a

Car or Motorcycle , but not when the input is an int or a string .

Specs

Comments and docstrings are not required in this part.

# define function here

# show that it works on Car

# show that it works on Motorcycles

# show an error on other inputs

Another part of HW

Here is a list of all of the presidents of the United States as tuples in the format (first_name,last_name) . (Middle initials are added to last names occasionally if needed to distinguish between father/son pairs.) For example, the tuple (“Thomas”, “Jefferson”) corresponds to president Thomas Jefferson.

presidents = [(“George”,”Washington”), (“John”,”Adams”),

presidents = [("George","Washington"), 
                  ("John","Adams"),                                                                                  ("Thomas","Jefferson"),
                      ("James", "Madison"),
                      ("James", "Monroe"),
                      ("John", "Q. Adams"),
                      ("Andrew","Jackson"),
                      ("Martin","Van Buren"),
                      ("William","Harrison"),
                      ("John","Tyler"),
                      ("James","Polk"),
                      ("Zachary","Taylor"),
                      ("Milliard","Filmore"),
                      ("Franklin","Pierce"),
                      ("James","Buchanan"),
                      ("Abraham","Lincoln"),
                      ("Andrew","Johnson"),
                      ("Ulysses","Grant"),
                      ("Rutherford","Hayes"),
                      ("James","Garfield"),
                      ("Chester","Arthur"),
                      ("Grover","Cleveland"),
                      ("Benjamin","Harrison"),
                      ("Grover","Cleveland"),
                      ("William","McKinley"),
                      ("Theodore","Roosevelt"),
                      ("William","Taft"),
                      ("Woodrow","Wilson"),
                      ("Warren","Harding"),
                      ("Calvin","Coolidge"),
                      ("Herbert","Hoover"),
                      ("Franklin","Roosevelt"),
                      ("Harry","Truman"),
                      ("Dwight","Eisenhower"),
                      ("John","Kennedy"),
                      ("Lyndon","Johnson"),
                      ("Richard","Nixon"),
                      ("Gerald","Ford"),
                      ("Jimmy","Carter"),
                      ("Ronald","Reagan"),
                      ("George", "H. W. Bush"),
                      ("William", "Clinton"),
                      ("George","W. Bush"),
                      ("Barack","Obama"),
                      ("Donald","Trump"),
                      ("Joseph","Biden") 
                       ]                                      

Part A

Write a function called count_distinct() . This function should take a single argument, which you may assume to be a list. The return value of count_distinct(L) should be the number of distinct items in L . For example, count_distinct([1,1,”b”]) should return 2 , since 1 and “b” are the 2 distinct values in this list.

Specs

Comments and docstrings are not required for this problem, although they may help us give you partial credit.
You are not required to perform any type-checking for this problem.
this needs to be three lines or less.

# your function definition here

Next, use count_distinct() to determine the number of different people who have been president of the United States? (Note, at least one person is on the list twice)

#check count_distinct() here

Part B

Write a function called first_names_dict which turns inputs a list of names (stored as tuples similar to the presidents list above) and returns a dictionary where the keys are first names and values are the number of times that someone with that first name appears on the list.

Demonstrate your function on the list presidents and print the resulting dictionary.

Specs

Comments and docstrings are not required for this problem, although they may help us give you partial credit.
You are not required to perform any type-checking for this problem: it’s ok to assume that the input is a correctly-constructed list whose elements are 2-tuples of strings.

My solution has 8 lines but it is okay if yours is longer or shorter.

# define first_names_dict() here

# demonstrate first_names_dict() here

Part C

Write a function called most_common_first_name() which determines the most common first name in a list formatted in the same way as presidents. Your function should return both the most common first name and the number of times it appears.

In the U.S. presidents list, the most common first name is “James”, which appears 5 times. So, your function should be able to do this:

first_name, num_occurrences = most_common_first_name(president s)
first_name, num_occurrences

(“James”, 5)

Specs

Comments and docstrings are not required for this problem, although they may help us give you partial credit.
You are not required to perform any type-checking for this problem: it’s ok to assume that the input is a correctly-constructed list whose elements are 2-tuples of strings.

You may assume that there is a unique most common first name. In a hypothetical list in which two names were equally common, it would be sufficient for your function to return one of them (with the correct count).

Write python code which determines what the most common first name amongst U.S. presidents is. Print the most common name and how many times it’s been used when you are done. (You don’t have to worry about ties. No comments are necessary for this part, but adding them could help me give you partial credit. My solution was 6 lines.)

#write your function here

#demonstrate ( using the testing code from the problem prompt ) here