<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://www.waterwaysinwhittlesea.org/index.php?action=history&amp;feed=atom&amp;title=Worksheet_7_-_OOP_Class_Car_example</id>
	<title>Worksheet 7 - OOP Class Car example - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://www.waterwaysinwhittlesea.org/index.php?action=history&amp;feed=atom&amp;title=Worksheet_7_-_OOP_Class_Car_example"/>
	<link rel="alternate" type="text/html" href="http://www.waterwaysinwhittlesea.org/index.php?title=Worksheet_7_-_OOP_Class_Car_example&amp;action=history"/>
	<updated>2026-07-05T15:40:35Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.4</generator>
	<entry>
		<id>http://www.waterwaysinwhittlesea.org/index.php?title=Worksheet_7_-_OOP_Class_Car_example&amp;diff=10317&amp;oldid=prev</id>
		<title>EdmondLascaris: Created page with &quot;= Car Class= Here’s a simple Python program demonstrating object-oriented programming (OOP) concepts. This program defines a basic Car class with attributes and methods to simulate a car's functionality. The program allows for easy expansion and modification.  Simple Python Program: Car Class  &lt;syntaxhighlight lang=&quot;python&quot;&gt; # Simple Car Class Example  class Car:     def __init__(self, make, model, year):         &quot;&quot;&quot;Initialize the attributes of the car.&quot;&quot;&quot;         self...&quot;</title>
		<link rel="alternate" type="text/html" href="http://www.waterwaysinwhittlesea.org/index.php?title=Worksheet_7_-_OOP_Class_Car_example&amp;diff=10317&amp;oldid=prev"/>
		<updated>2024-10-27T10:25:02Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;= Car Class= Here’s a simple Python program demonstrating object-oriented programming (OOP) concepts. This program defines a basic Car class with attributes and methods to simulate a car&amp;#039;s functionality. The program allows for easy expansion and modification.  Simple Python Program: Car Class  &amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt; # Simple Car Class Example  class Car:     def __init__(self, make, model, year):         &amp;quot;&amp;quot;&amp;quot;Initialize the attributes of the car.&amp;quot;&amp;quot;&amp;quot;         self...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;= Car Class=&lt;br /&gt;
Here’s a simple Python program demonstrating object-oriented programming (OOP) concepts. This program defines a basic Car class with attributes and methods to simulate a car's functionality. The program allows for easy expansion and modification.&lt;br /&gt;
&lt;br /&gt;
Simple Python Program: Car Class&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
# Simple Car Class Example&lt;br /&gt;
&lt;br /&gt;
class Car:&lt;br /&gt;
    def __init__(self, make, model, year):&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;Initialize the attributes of the car.&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
        self.make = make          # The manufacturer of the car&lt;br /&gt;
        self.model = model        # The model of the car&lt;br /&gt;
        self.year = year          # The manufacturing year of the car&lt;br /&gt;
        self.odometer_reading = 0 # Initialize the odometer reading to 0&lt;br /&gt;
&lt;br /&gt;
    def describe_car(self):&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;Return a neatly formatted descriptive name.&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
        return f&amp;quot;{self.year} {self.make} {self.model}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
    def read_odometer(self):&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;Print a statement showing the car's mileage.&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
        print(f&amp;quot;This car has {self.odometer_reading} miles on it.&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
    def update_odometer(self, mileage):&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;Set the odometer to the given value.&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
        if mileage &amp;gt;= self.odometer_reading:&lt;br /&gt;
            self.odometer_reading = mileage&lt;br /&gt;
        else:&lt;br /&gt;
            print(&amp;quot;You can't roll back an odometer!&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
    def increment_odometer(self, miles):&lt;br /&gt;
        &amp;quot;&amp;quot;&amp;quot;Add the given amount to the odometer.&amp;quot;&amp;quot;&amp;quot;&lt;br /&gt;
        if miles &amp;gt; 0:&lt;br /&gt;
            self.odometer_reading += miles&lt;br /&gt;
        else:&lt;br /&gt;
            print(&amp;quot;You can't increment by a negative value!&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
if __name__ == '__main__':&lt;br /&gt;
    # Create an instance of the Car class&lt;br /&gt;
    my_car = Car('Toyota', 'Corolla', 2020)&lt;br /&gt;
&lt;br /&gt;
    # Print the description of the car&lt;br /&gt;
    print(my_car.describe_car())&lt;br /&gt;
&lt;br /&gt;
    # Update the odometer and read the mileage&lt;br /&gt;
    my_car.update_odometer(15000)&lt;br /&gt;
    my_car.read_odometer()&lt;br /&gt;
&lt;br /&gt;
    # Increment the odometer&lt;br /&gt;
    my_car.increment_odometer(500)&lt;br /&gt;
    my_car.read_odometer()&lt;br /&gt;
&lt;br /&gt;
    # Attempt to roll back the odometer (this will show an error)&lt;br /&gt;
    my_car.update_odometer(10000)  # This should show a warning&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Explanation of the Code ==&lt;br /&gt;
=== Class Definition: Car ===&lt;br /&gt;
The class is defined using the class keyword.&lt;br /&gt;
The __init__ method initializes the car’s attributes, such as make, model, year, and odometer_reading.&lt;br /&gt;
&lt;br /&gt;
=== Methods: ===&lt;br /&gt;
* '''describe_car(self):''' Returns a string describing the car in a formatted way.&lt;br /&gt;
* '''read_odometer(self):''' Prints the current mileage of the car.&lt;br /&gt;
* '''update_odometer(self, mileage):''' Updates the odometer reading if the new mileage is greater than or equal to the current reading.&lt;br /&gt;
* '''increment_odometer(self, miles):''' Increases the odometer reading by a specified number of miles, only if the value is positive.&lt;br /&gt;
&lt;br /&gt;
=== Main Script: ===&lt;br /&gt;
* An instance of the Car class is created (named my_car).&lt;br /&gt;
* The program prints the description of the car, updates the odometer, and shows how to use the various methods defined in the class.&lt;br /&gt;
* The program also demonstrates error handling when attempting to roll back the odometer.&lt;br /&gt;
&lt;br /&gt;
== Expanding the Program ==&lt;br /&gt;
Here are some ideas on how to expand this program:&lt;br /&gt;
&lt;br /&gt;
=== Add More Attributes:===&lt;br /&gt;
* Include attributes like color, fuel_type, or mileage_per_gallon.&lt;br /&gt;
* Create Derived Classes: Create subclasses, such as ElectricCar, that inherit from the Car class and add new methods or attributes (e.g., battery capacity).&lt;br /&gt;
* Implement a Maintenance Log: Add methods to track maintenance activities or repairs.&lt;br /&gt;
* Add User Input: Allow users to input car details when creating a new car object.&lt;br /&gt;
&lt;br /&gt;
This simple program provides a solid foundation for learning OOP in Python and can easily be expanded upon as you gain more experience!&lt;/div&gt;</summary>
		<author><name>EdmondLascaris</name></author>
	</entry>
</feed>