0% found this document useful (0 votes)
19 views2 pages

Python Class Practice Problems

Uploaded by

wonjunha12
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views2 pages

Python Class Practice Problems

Uploaded by

wonjunha12
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

[2024-2]

<Week 11> Practice problems

Create a Python script file (.py) for each problem, and compress them into a single zip file for
submission. Name the files as prob1, prob2, ..., prob5. Submit the compressed file to AJOU Black
board.
(각 문제마다 python script file(.py)을 만들고 하나로 압축하여 아주Bb에 제출하세요. 파일 이름은
prob1, prob2, ..., prob5로 만드세요.)

1. Let's write a class named Car that represents a car. The Car class should have two instance
variables: color which represents the car's color, and mileage which represents the car's mileage.
Create two Car objects using the class. The first object should have values {“blue”, 10000}, and
the second object should have values {“red”, 20000}. After creating the objects, print their
attributes as follows.

The mileage of the blue car is 10000.


The mileage of the red car is 20000.

2. Let's write a class called Box that represents a box. The Box class should have instance variables
for the length, width, and height of the box. The class should have the following methods:
ü A constructor function (__init__(self, l, h, d)) that takes parameters l, h, and d.
ü A method that returns the box's information as a string (__str__()).
ü Getter and setter methods for each variable (setLength, getLength, etc.).

Create an object of the Box class with length, width, and height values of 100, 100, and 100,
respectively. Print the information about the box and also calculate and print the volume of the
box.

3. Write a class called Dog that represents a dog. The Dog class should have the attributes: name,
age, and color. Create three dogs with the following values:
ü {"Molly", 10, "brown"}
ü {"Daisy", 6, "black"}
ü {"Bella", 7, "white"}
Print the attributes of each dog. Additionally, define a class variable count to store the number
of dogs that have been created and print that as well.

Dog#1 = "Molly", 10, "brown”


Dog#2 = "Daisy", 6, "black"
Dog#3 = "Bella", 7, "white"
Total number = 3
4. Copy and paste the Dog class from the program 3 and create an additional Witch class. The
Witch class should have a method younger(self, dog) that takes a dog object as a parameter and
subtracts 5 from its age. Create a Molly dog object, print its information, send it to the witch,
and then print the information again after the witch modifies its age.

5. Add the __gt__() special method to the Circle class so that Circle objects can be compared using
the > operator. The method should return True if the area of the circle is larger.

import math
# Defining a Circle class
class Circle:
def __init__(self, radius = 0):
[Link] = radius
def getArea(self):
return [Link] * [Link] * [Link]
def getPerimeter(self):
return 2 * [Link] * [Link]

You might also like