Lab 10C - Creating a new class
This assignment assumes that you have read and understood the following documents:
http://www.annedawson.net/Python3_Intro_OOP.pdf
http://www.annedawson.net/Python3_Prog_OOP.pdf
and that you're familiar with the following example
programs:
http://www.annedawson.net/python3programs.html
13-01.py, 13-02.py, 13-03.py, 13-04.py
Instructions:
Complete as much as you can in the time allowed.
Write a Python class named "Car" that has the following data attributes (please create your own variable names for these attributes using the recommended naming conventions):
- year (for the car's year of manufacture)
- make (for the car's maker)
- model (for the car's model)
- speed (for the car's speed)
The class should have an __init__ method that accepts year, make and model as arguments. These values should be assigned to the object's corresponding attributes. Speed should be set to 0 in the __init__ method.
The class should have a __str__ method that returns the state of the Car object - in other words, it returns a string composed of the values of each of the car object's attributes.
The class should have an accessor method and a mutator method for each attribute - also following the recommended naming conventions.
The class should also have the following three methods:
- a method called stop, which sets the speed of the Car object to 0 and prints the name of the model and the text "stopped" on the same line.
- a method called brake, which reduces the speed by 5 (as long as the speed is greater than 0) and prints the name of the model and the text "slowing down" on the same line.
- a method called accelerate, which increases the speed by 5 and prints the name of the model and the text "speeding up" on the same line.
Each of the methods of the class should have a docstring explaining the purpose of the method.
Write a main program to create two Car objects. The first car should be created with these values: "2014","Toyota","Tacoma", the second car should have these values: "2017","Ford","Focus".
Write one line of code to print the docstring for the class.
Write one line of code to print the state (current attribute values) of the first car object.
Write one line of code to print the state (current attribute values) of the second car object.
Write one line of code to print the docstring of any accessor method.
Write one line of code to print the docstring of any mutator method.
Write one line of code to set the speed of the first Car object to 50
Write one line of code to set the speed of the second Car object to 65
Write one line of code to print the state (current attribute values) of the first car object.
Write one line of code to print the state (current attribute values) of the second car object.
Write one line of code to stop the second Car object.
Write one line of code to print the state (current attribute values) of the first car object.
Write one line of code to print the state (current attribute values) of the second car object.
Write one line of code to accelerate the second Car object.
Write one line of code to brake the first Car object.
Write one line of code to print the state (current attribute values) of the first car object.
Write one line of code to print the state (current attribute values) of the second car object.
Include the recommended comments at the top of the code for filename, programmer, date etc.
'''
Python version : 3.6
Python program to create and implement a class Car
'''
class Car:
""" Class to define the attributes and function to represent a Car """
def __init__(self,year,make,model):
'''Initializes the objects of Car class with the passed values'''
self.__year = year
self.__make = make
self.__model = model
self.__speed = 0
def __str__(self):
''' Returns the string representation of the Car containing the attribute values'''
return("Year : "+str(self.__year)+" Make : "+self.__make+" Model : "+self.__model+" Speed : "+str(self.__speed))
def set_speed(self,speed):
""" Function to set the car's speed """
self.__speed = speed
def set_year(self,year):
""" Function to set the car's year of manufacture """
self.__year = year
def set_make(self,make):
""" Function to set the car's make """
self.__make = make
def set_model(self,model):
""" Function to set the car's model """
self.__model = model
def get_speed(self):
""" Function to return the car's speed """
return self.__speed
def get_year(self):
""" Function to return the car's year of manufacture """
return self.__year
def get_make(self):
""" Function to return the car's make """
return self.__make
def get_model(self):
""" Function to return car's model """
return self.__model
def stop(self):
''' Function to stop the car i.e set the speed of the car to 0 '''
self.__speed = 0
print(self.__model + ' stopped ')
def brake(self):
''' Function to apply brakes to the car i.e reduce the speed of the car by 5 '''
if self.__speed > 5:
self.__speed = self.__speed - 5
print(self.__model + ' slowing down')
def accelerate(self):
''' Function to accelerate the car i.e increase the speed of the car by 5 '''
self.__speed = self.__speed + 5
print(self.__model + ' speeding up')
def main():
# create two Car objects.
car1 = Car("2014","Toyota","Tacoma")
car2 = Car("2017","Ford","Focus")
# print the docstring for the class.
print(Car.__doc__)
# print the state (current attribute values) of the first car object and second car object
print(car1)
print(car2)
# print the docstring of any accessor method.
print(Car.get_make.__doc__)
# print the docstring of any mutator method.
print(Car.set_make.__doc__)
# set the speed of the first Car object to 50
car1.set_speed(50)
# set the speed of the second Car object to 65
car2.set_speed(65)
# print the state (current attribute values) of the first car object and second car object
print(car1)
print(car2)
# stop the second Car object.
car2.stop()
# print the state (current attribute values) of the first car object and second car object
print(car1)
print(car2)
# accelerate the second Car object.
car2.accelerate()
# brake the first Car object.
car1.brake()
# print the state (current attribute values) of the first car object and second car object
print(car1)
print(car2)
# call the main method
if __name__ == "__main__":
main()
#end of program
Code Screenshot:
Output:
Lab 10C - Creating a new class This assignment assumes that you have read and understood...
1. Assume you have a Car class that declares two private instance variables, make and model. Write Java code that implements a two-parameter constructor that instantiates a Car object and initializes both of its instance variables. 2. Logically, the make and model attributes of each Car object should not change in the life of that object. a. Write Java code that declares constant make and model attributes that cannot be changed after they are initialized by a constructor. Configure your...
In Python Programming: Write a class named Car that has the following data attributes: _ _year_model (for the car’s year model) _ _make (for the make of the car) _ _speed (for the car’s current speed) The Car class should have an _ _init_ _ method that accepts the car’s year model and make as arguments. These values should be assigned to the object’s _ _year_model and _ _make data attributes. It should also assign 0 to the _ _speed...
java Write a class named Car that has the following fields: • yearModel: The yearModel field is an int that holds the car's year model. • make: The make field is a String object that holds the make of the car. • speed: The speed field is an int that holds the car's current speed. In addition, the class should have the following methods: • Constructor: The constructor should accept the car's year model and make as arguments. These values...
Car Class Background: Write a class named Car that will be used to store information and control the acceleration and brake of a car. Functionality: 1. Write a class named "Car” that has the following member variables: • year Model - an int that holds the car's year model • make-A string that holds the make of the car • speed - an int that holds the car's current speed 2. In addition the class should have the following member...
In C++ Write a program that contains a BankAccount class. The BankAccount class should store the following attributes: account name account balance Make sure you use the correct access modifiers for the variables. Write get/set methods for all attributes. Write a constructor that takes two parameters. Write a default constructor. Write a method called Deposit(double) that adds the passed in amount to the balance. Write a method called Withdraw(double) that subtracts the passed in amount from the balance. Do not...
*Python* INTRODUCTION: The goal of this programming assignment is to enable the student to practice object-oriented programming using classes to create objects. PROBLEM DEFINITION: Write a class named Employee that holds the following data about an employee in attributes: name, IDnumber, department, jobTitle The class should have 8 methods as follows: For each attribute, there should be a method that takes a parameter to be assigned to the attribute. This is known as a mutator method. For each...
python code? 1. Create a class called Person that has 4 attributes, the name, the age, the weight and the height [5 points] 2. Write 3 methods for this class with the following specifications. a. A constructor for the class which initializes the attributes [2.5 points] b. Displays information about the Person (attributes) [2.5 points] c. Takes a parameter Xage and returns true if the age of the person is older than Xage, false otherwise [5 points] 3. Create another...
(C++)This is the problem that I'm supposed to write a program for: This is the layout that we are supposed to fill out for the project: If anything is unclear, comment and I'll try to clarify. Thank you! Objective Reading Dala Files Project This is a variation of ng challenge 133 de Write a class named Car that has the following pelvate member variables. model Year An ie car's uodd year. make A string that bolds the make of the...
Assignment Requirements I have also attached a Class Diagram that describes the hierarchy of the inheritance and interface behaviors . The link to the PDF of the diagram is below MotorVehical.pdf Minimize File Preview User Define Object Assignment: Create a Intellij Project. The Intellij project will contain three user defined classes. The project will test two of the User Define Classes by using the invoking each of their methods and printing the results. You are required to create three UML...
Use Java and please try and show how to do each section. You are creating a 'virtual pet' program. The pet object will have a number of attributes, representing the state of the pet. You will need to create some entity to represent attributes in general, and you will also need to create some specific attributes. You will then create a generic pet class (or interface) which has these specific attributes. Finally you will make at least one subclass of...