Question

Must be in Python 3

1. Implement a Student class that has three attributes: ID, name, and mark. 2 Implement a comparison method as part of the St

exercise_2.py

# Define the Student class

class Student():

  

# Initialize the object properties

def __init__(self, id, name, mark):

# TODO

# Print the object as an string

def __str__(self):

return ' - {}, {}, {}'.format(self.id, self.name, self.mark)

# Check if the mark of the input student is greater than the student object

# The output is either True or False

def is_greater_than(self, another_student):

# TODO

# Sort the student_list

# The output is the sorted list of students

def sort_students(student_list):

# TODO

if __name__== "__main__":

# Read the data

students_file = open('student_list.txt', 'rt')

students_data = students_file.readlines()

student_list = []

for student in students_data:

# Create a student object

fields = student.split(', ')

id = fields[0]

name = fields[1]

mark = fields[2]

student_list.append(Student(id, name, int(mark)))

# Print the original data

print('Original data:')

for student in student_list:

print(student)

# Sort the students

sorted_students = sort_students(student_list)

# Print the sorted data

print('Sorted data:')

for student in sorted_students:

print(student)

student_list.txt

1292467, George Daniel, 89
1398976, Amir Jahani, 76
1392567, Sarah Kylo, 90
1368989, Breanne Lipton, 82
1409916, Robert George, 95
1267756, Jeff Anderson, 84
1367814, Xialing Liu, 86
1458879, Ali Gendi, 77
1407756, Mary Simon, 35
1428867, Georgina Moore, 88
1491228, Brian Johnson, 42
1398875, Samuel Picard, 55
0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM :

class Student:

# Initialize the object properties
def __init__(self, id, name, mark):
self.id = id
self.name = name
self.mark = mark

# Print the object as a string
def __str__(self):
return '- {}, {}, {}'.format(self.id, self.name, self.mark)

# Check if the mark of the input student is greater than the student object
def is_greater_than(self, another_student):
if(self.mark > another_student.mark):
return True
else:
return False

# Sort the student_list
def sort_students(student_list):
return sorted(student_list, key=lambda student: student.id)

if __name__ == "__main__":
students_file = open('student_list.txt', 'rt')
students_data = students_file.readlines()

student_list = []
for student in students_data:
# Create a student object
fields = student.split(',')
id = fields[0]
name = fields[1]
mark = fields[2]

student_list.append(Student(id, name, int(mark)))

# Print the original data
print("Original data:")
for student in student_list:
print(student)

# Sort the students
sorted_students = sort_students(student_list)
len(sorted_students)

# Print the sorted data
print("Sorted(on id) data:")
for student in sorted_students:
print(student)

OUTPUT :

Original data:
- 1292467, George Daniel, 89
- 1398976, Amir Jahani, 76
- 1392567, Sarah Kylo, 90
- 1368989, Breanne Lipton, 82
- 1409916, Robert George, 95
- 1267756, Jeff Anderson, 84
- 1367814, Xialing Liu, 86
- 1458879, Ali Gendi, 77
- 1407756, Mary Simon, 35
- 1428867, Georgina Moore, 88
- 1491228, Brian Johnson, 42
- 1398875, Samuel Picard, 55
Sorted(on id) data:
- 1267756, Jeff Anderson, 84
- 1292467, George Daniel, 89
- 1367814, Xialing Liu, 86
- 1368989, Breanne Lipton, 82
- 1392567, Sarah Kylo, 90
- 1398875, Samuel Picard, 55
- 1398976, Amir Jahani, 76
- 1407756, Mary Simon, 35
- 1409916, Robert George, 95
- 1428867, Georgina Moore, 88
- 1458879, Ali Gendi, 77
- 1491228, Brian Johnson, 42

SCREENSHOT :

Program:

if name main : studentsfile open (student list.txt, rt) students_data students_file.readlines () student_list- [] for st

OUTPUT :

original data: - 1292467, George Daniel, 89 1398976, Amir Jahani, 76 -1392567, Sarah Kylo, 98 - 1368989, Breanne Lipton, 82 -

Add a comment
Know the answer?
Add Answer to:
Must be in Python 3 exercise_2.py # Define the Student class class Student():    # Ini...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN PYTHON Write the script test3.py that creates and populates a list of OnlineStudent objects. It inputs a requested s...

    IN PYTHON Write the script test3.py that creates and populates a list of OnlineStudent objects. It inputs a requested student, then prints the fields for that student, and also the username of each student in the student's study group. See the pseudocode in Item 3 below. Use the new type of for loop that you can use for reading from a file: # Instead of writing a loop like this fin = open("input-file", "r") line = fin.readline( ) while line...

  • 1. Do the following a. Write a class Student that has the following attributes: - name:...

    1. Do the following a. Write a class Student that has the following attributes: - name: String, the student's name ("Last, First" format) - enrollment date (a Date object) The Student class provides a constructor that saves the student's name and enrollment date. Student(String name, Date whenEnrolled) The Student class provides accessors for the name and enrollment date. Make sure the class is immutable. Be careful with that Date field -- remember what to do when sharing mutable instance variables...

  • I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to...

    I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to guess its because I'm not using the swap function I built. Every time I run it though I get an error that says the following Traceback (most recent call last): File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 146, in <module> main() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 122, in main studentArray.gpaSort() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py",...

  • PYTHON 3 Object Oriented Programming ***a9q3.py file below*** class GradeItem(object): # A Grade Item is anything...

    PYTHON 3 Object Oriented Programming ***a9q3.py file below*** class GradeItem(object): # A Grade Item is anything a course uses in a grading scheme, # like a test or an assignment. It has a score, which is assessed by # an instructor, and a maximum value, set by the instructor, and a weight, # which defines how much the item counts towards a final grade. def __init__(self, weight, scored=None, out_of=None): """ Purpose: Initialize the GradeItem object. Preconditions: :param weight: the weight...

  • Write this code on python 3 only. Suppose class Student represents information about students in a...

    Write this code on python 3 only. Suppose class Student represents information about students in a course. Each student has a name and a list of test scores. The Student class should allow the user to view a student's name, view a test score at a given position, view the highest test score, view the average test score, and obtain a string representation of the student's information. When a Student object is created, the user supplies the student's name and...

  • Create a Java program that will store 10 student objects in an ArrayList, ArrayList<Student>. A student object con...

    Create a Java program that will store 10 student objects in an ArrayList, ArrayList<Student>. A student object consists of the following fields: int rollno String name String address Implement two comparator classes to sort student objects by name and by rollno (roll number). Implement your own selection sort method and place your code in a separate Java source file. Do not use a sort method from the Java collections library.

  • ( Object array + input) Write a Java program to meet the following requirements: 1. Define...

    ( Object array + input) Write a Java program to meet the following requirements: 1. Define a class called Student which contains: 1.1 data fields: a. An integer data field contains student id b. Two String data fields named firstname and lastname c. A String data field contains student’s email address 1.2 methods: a. A no-arg constructor that will create a default student object. b. A constructor that creates a student with the specified student id, firstname, lastname and email_address...

  • Using Java: Create an array with the size of 10 and assign student details (Name, ID,...

    Using Java: Create an array with the size of 10 and assign student details (Name, ID, age and TotalMarks) to the array. Perform the Heap Sort to sort the students in ascending order based on their total marks. Steps: • Create the student list (use Random class in java to generate the age (15- 25) and total (0-100)) • Print the Student List in a table format • Perform Heap sort based on the total marks of the students •...

  • this is written in python Goal: Implement and test the Onlinestudent class as shown in the Student/OnlineStudent UML Dia...

    this is written in python Goal: Implement and test the Onlinestudent class as shown in the Student/OnlineStudent UML Diagram Relevant Examples: EmployeeHierarchy PersonArray Pet Input Files: students.txt study-groups.txt The study_group instance variable is a list that contains the username values of the other members in the student's study group Details: 1. Implement the onlinestudent class in the module onlinestudent.py as specified by the Student/OnlineStudent UML Diagram. Onlinestudent is the derived class of the base class student. 2. The_1t__method for an...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT