Thời gian thực hiện: 4 giờ Số dòng code: 88 Bài viết: 1 Python
Tạo gói thông tin nhân sự và kiểm tra Mô tả bài toán: Định nghĩa lớp Người People với các thuộc tính: tên, giới tính, tuổi; Từ People tạo lớp Sinh viên Student, thêm thuộc tính: mã sinh viên, thời gian nhập học, điểm nhập học; Từ People tạo lớp Giáo viên Teacher, thêm thuộc tính: chức vụ, phòng ban, thời gian làm việc; Từ Sinh viên tạo lớp Nghiên cứu sinh Graduate, thêm thuộc tính: hướng nghiên cứu và giảng viên hướng dẫn, định nghĩa hàm khởi tạo và hàm hiển thị. Tạo các đối tượng của các lớp này và thực hiện kiểm tra. Sử dụng giao diện để tạo lớp Nghiên cứu sinh đang làm việc GradOnWork.
Mã nguồn:
class Person:
def __init__(self, name='WangJianMin', gender='nam', age=45):
self.name = name
self.gender = gender
self.age = age
def show(self):
print("Tên: " + self.name + "\nGiới tính: " + self.gender + "\nTuổi: " + str(self.age))
class Student(Person):
def __init__(self, student_id="20223250", entry_date="2022-09-01", score="100.0"):
Person.__init__(self, name='ZhangSan', gender='nam', age=20)
self.student_id = student_id
self.entry_date = entry_date
self.score = score
def show(self):
print("Tên: " + self.name + "\nGiới tính: " + self.gender + "\nTuổi: " + str(self.age) + "\nMã sinh viên: " + self.student_id + "\nNgày nhập học: " + self.entry_date + "\nĐiểm: " + str(self.score))
class Teacher(Person):
def __init__(self, position="Trưởng khoa", department="Khoa Công nghệ phần mềm", work_date="2008-12-03"):
Person.__init__(self, name='WangJianMin', gender='nam', age=45)
self.position = position
self.department = department
self.work_date = work_date
def show(self):
print("Tên: " + self.name + "\nGiới tính: " + self.gender + "\nTuổi: " + str(self.age) + "\nChức vụ: " + self.position + "\nPhòng ban: " + self.department + "\nNgày làm việc: " + self.work_date)
class ResearchStudent(Student):
def __init__(self, research_area="Trí tuệ nhân tạo", advisor="Mr.Liu"):
Student.__init__(self, student_id="20193250", entry_date="2019-09-01", score="100.0")
self.research_area = research_area
self.advisor = advisor
def show(self):
print("Tên: " + self.name + "\nGiới tính: " + self.gender + "\nTuổi: " + str(self.age) + "\nMã sinh viên: " + self.student_id + "\nNgày nhập học: " + self.entry_date + "\nĐiểm: " + str(self.score) + "\nHướng nghiên cứu: " + self.research_area + "\nGiáo viên hướng dẫn: " + self.advisor)
class WorkingResearchStudent(Teacher, ResearchStudent):
def __init__(self):
Teacher.__init__(self, position="Trợ lý", department="Khoa Công nghệ phần mềm", work_date="2008-12-03")
ResearchStudent.__init__(self, research_area="Trí tuệ nhân tạo", advisor="Mr.Liu")
def show(self):
print("Tên: " + self.name + "\nGiới tính: " + self.gender + "\nTuổi: " + str(self.age) + "\nChức vụ: " + self.position + "\nPhòng ban: " + self.department + "\nNgày làm việc: " + self.work_date + "\nMã sinh viên: " + self.student_id + "\nNgày nhập học: " + self.entry_date + "\nĐiểm: " + str(self.score) + "\nHướng nghiên cứu: " + self.research_area + "\nGiáo viên hướng dẫn: " + self.advisor)
person = Person()
person.show()
student = Student()
student.show()
teacher = Teacher()
teacher.show()
research_student = ResearchStudent()
research_student.show()
working_research_student = WorkingResearchStudent()
working_research_student.show()
Thiết kế hệ thống hình học 3D dựa trên lớp Hình tròn Mô tả bài toán: Thiết kế hệ thống hình học 3D, yêu cầu sau: Thiết kế giao diện chức năng hình học 3D, bao gồm các phương thức tính chu vi, diện tích, thể tích; Định nghĩa lớp Điểm, chứa các thành viên dữ liệu x, y, phương thức lấy và thiết lập tọa độ, phương thức hiển thị; Từ lớp Điểm, kế thừa lớp Hình tròn, thêm thành viên bán kính, phương thức thiết lập và lấy bán kính, ghi đè phương thức hiển thị, có thể tính chu vi và diện tích; Từ lớp Hình tròn, kế thừa lớp Cầu, Hình trụ, Hình nón; yêu cầu các lớp con Cầu, Hình trụ, Hình nón đều có phương thức nhập và xuất hiển thị; có thể tính diện tích, chu vi. Tạo các đối tượng của các lớp này và thực hiện kiểm tra.
Mã nguồn:
import math
class Shape:
def calculate_perimeter(self):
pass
def calculate_area(self):
pass
def calculate_volume(self):
pass
class Point(Shape):
def __init__(self, x=0, y=0):
self.x = x
self.y = y
def set_x(self, x):
self.x = x
def set_y(self, y):
self.y = y
def get_x(self):
return self.x
def get_y(self):
return self.y
def display(self):
print('({}'.format(self.x) + ',{})'.format(self.y))
class Circle(Point):
def __init__(self, radius):
Point.__init__(self, x=0, y=0)
self.radius = radius
def set_radius(self, radius):
self.radius = radius
def get_radius(self):
return self.radius
def calculate_perimeter(self):
perimeter = round(2 * 3.14 * self.radius, 2)
return perimeter
def calculate_area(self):
area = round(3.14 * self.radius ** 2, 2)
return area
def display(self):
print('Chu vi hình tròn:', self.calculate_perimeter())
print('Diện tích hình tròn:', self.calculate_area())
class Ball(Circle):
def __init__(self, radius):
Circle.__init__(self, radius=10)
def calculate_volume(self):
volume = round((4 / 3) * 3.14 * (self.radius ** 3), 2)
return volume
def calculate_area(self):
area = round(3.14 * self.radius ** 2 * 4, 2)
return area
def display(self):
print('Thể tích hình cầu:', self.calculate_volume())
print('Diện tích hình cầu:', self.calculate_area())
class Cylinder(Circle):
def __init__(self, radius, height):
Circle.__init__(self, radius=10)
self.height = height
def calculate_volume(self):
volume = round(3.14 * (self.radius ** 2) * self.height, 2)
return volume
def calculate_area(self):
area = round(3.14 * 2 * self.radius * self.height + 2 * 3.14 * self.radius ** 2, 2)
return area
def display(self):
print('Thể tích hình trụ:', self.calculate_volume())
print('Diện tích hình trụ:', self.calculate_area())
class Cone(Circle):
def __init__(self, radius, height):
Circle.__init__(self, radius=10)
self.height = height
def calculate_volume(self):
volume = round((1 / 3) * 3.14 * (self.radius ** 2) * self.height, 2)
return volume
def calculate_area(self):
area = round(3.14 * self.radius * (self.radius + math.sqrt(self.radius ** 2 + self.height ** 2)), 2)
return area
def display(self):
print('Thể tích hình nón:', self.calculate_volume())
print('Diện tích hình nón:', self.calculate_area())
point = Point(6, 2)
point.display()
circle = Circle(4)
circle.display()
radius = float(input("Nhập bán kính hình cầu: "))
ball = Ball(radius)
ball.display()
radius_cylinder = float(input("Nhập bán kính đáy hình trụ: "))
height_cylinder = float(input("Nhập chiều cao hình trụ: "))
cylinder = Cylinder(radius_cylinder, height_cylinder)
cylinder.display()
radius_cone = float(input("Nhập bán kính đáy hình nón: "))
height_cone = float(input("Nhập chiều cao hình nón: "))
cone = Cone(radius_cone, height_cone)
cone.display()