Question

Add three methods to the Student class that compare twoStudent objects. One method (__eq__) should test...

Add three methods to the Student class that compare twoStudent objects. One method (__eq__) should test for equality. A second method (__lt__) should test for less than. The third method (__ge__) should test for greater than or equal to. In each case, the method returns the result of the comparison of the two students’ names. Include a main function that tests all of the comparison operators.
Current code is below:

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program code screenshot:

# Create a class with the name of # student and initialize with an object class Student (object): # Define a init function as# method (eq) should test for equality. def_eq__(self, student): # return the equality marks student return self.name == stud# Call the object student student2 = Student(Peter, 5) # display the student2 score print(student2) # Display the third stuSample output:

Name: Peter Scores: 00000 Name: Peter Scores: 100 100 100 100 100 Second student Name: Peter Scores: 00000 Third student Name

Program code to copy:

# Create a class with the name of

# student and initialize with an object

class Student(object):

# Define a init function as like constructor

def __init__(self, name, number):

    # Create an object of name

    self.name = name

    # Create an object with the score

    # list

    self.scores = []

    # for loop to count the number

    for count in range(number):

      # add the score at the end of

      # the list

      self.scores.append(0)

# Create a getName function  

def getName(self):

    # return the self name

    return self.name

# Create the setScore function

# to compute the score

def setScore(self, i, score):

    # set the evaluvate score

    self.scores[i - 1] = score

# Create the getScore

def getScore(self, i):

    # return the get it self.score

    return self.scores[i - 1]

# Create the get average function

# to compute the average

def getAverage(self):

    # return the student average score

    return sum(self.scores) / len(self._scores)

   # Create a functioon to get the

   # student highest score

def getHighScore(self):

    # return the maximum score

    return max(self.scores)

# method (__eq__) should test for equality.

def __eq__(self,student):

    # return the equality marks student

    return self.name == student.name

# method (__lt__) should test

# for less than.

def __lt__(self,student):

     # return the student who student

     # less than the score

    return self.name<student.name

# The third method (__ge__)

# should test for greater than

# or equal to

def __ge__(self,student):

    # return the student as per the

    # question requirement

    return self.name == student.name or self.name>student.name

# Create a string file function

# to create the student name

def __str__(self):

    # Return the student name and score

    return "Name: " + self.name + "\nScores: " + \

" ".join(map(str, self.scores))

   

# Create a main function of the program

def main():

# Create one student as a Name

# of Peter You can assign the

# name as per your program need

student = Student("Peter", 5)

# Display the student name

print(student)

# for loop to access the range

for i in range(1, 6):

    # set the student score format

    # 1 to 100 and call by object

    student.setScore(i, 100)

# Display student scores

print(student)

# Display the second student name

print("\nSecond student")

# Call the object student

student2 = Student("Peter", 5)

# display the student2 score

print(student2)

# Display the third student

print("\nThird student")

# Create an object of second student

student3 = Student("Adam", 5)

# display the student 3 as a no. 2

# student

print(student3)

# Display student 1 is equal 2 student 2

# or not

print("\nChecking equal student1 and student 2")

# Display the student equivalent

print(student.__eq__(student2))

# Display the student1 is equal to the

# student 3

print("\nChecking equal student1 and student 3")

# Display the student equivalent

print(student.__eq__(student3))

# Display the message which student is greater than or not

print("\nChecking greater than equal student1 and student 3")

# check the student greater than

print(student.__ge__(student3))

# Display the student which student is less than

print("\nChecking less than student1 and student 3")

# call the object student less

print(student.__lt__(student3))

# create the main of the main program

if __name__ == "__main__":

# call the main class

main()

Add a comment
Know the answer?
Add Answer to:
Add three methods to the Student class that compare twoStudent objects. One method (__eq__) should test...
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
  • I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student...

    I need code in java The Student class: CODE IN JAVA: Student.java file: public class Student {    private String name;    private double gpa;    private int idNumber;    public Student() {        this.name = "";        this.gpa = 0;        this.idNumber = 0;    }    public Student(String name, double gpa, int idNumber) {        this.name = name;        this.gpa = gpa;        this.idNumber = idNumber;    }    public Student(Student s)...

  • In Java programming language Please write code for the 6 methods below: Assume that Student class...

    In Java programming language Please write code for the 6 methods below: Assume that Student class has name, age, gpa, and major, constructor to initialize all data, method toString() to return string reresentation of student objects, getter methods to get age, major, and gpa, setter method to set age to given input, and method isHonors that returns boolean value true for honors students and false otherwise. 1) Write a method that accepts an array of student objects, and n, the...

  • Must be in Python 3 exercise_2.py # Define the Student class class Student():    # Ini...

    Must be in Python 3 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...

  • Registration Language Java Step 1: Design a class called Student. The Student class should contain the...

    Registration Language Java Step 1: Design a class called Student. The Student class should contain the following data: firstName lastName studentID totalCredits gpa Your class should implement the “sets” and “gets” for the class. Include methods: equals, compareTo, toString. Change the toString method (from class) to put each member variable on a new line. Step 2: Write a driver program to test that Student works correctly. Test is with 3 students as given in class. Step 3: Write a “registration”...

  • // Client application class and service class Create a NetBeans project named StudentClient following the instructions...

    // Client application class and service class Create a NetBeans project named StudentClient following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. Add a class named Student to the StudentClient project following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. After you have created your NetBeans Project your application class StudentC lient should contain the following executable code: package studentclient; public class...

  • 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...

  • java This lab is intended to give you practice creating a class with a constructor method,...

    java This lab is intended to give you practice creating a class with a constructor method, accessor methods, mutator methods, equals method , toString method and a equals method. In this lab you need to create two separate classes, one Student class and other Lab10 class. You need to define your instance variables, accessor methods, mutator methods, constructor, toString method and equals method in Student class. You need to create objects in Lab10 class which will have your main method...

  • Write a Student class that stores information for a Montclair student. The class should include the...

    Write a Student class that stores information for a Montclair student. The class should include the following instance variables id, an integer identifier for the student lastName, a string for the student's last name creditsEarned, an integer representing the number of course-credits the student has earned courseLoadCredits, an integer representing the current number of credits in progress. status, an integer representing the status of the student (1 means freshman, 2 means sophomore, 3 means junior, 4 means senior). This status...

  • Java programming The purpose of this problem is to practice using a generic Urn class. NOTE:...

    Java programming The purpose of this problem is to practice using a generic Urn class. NOTE: Refer to the code for the ArrayStack class from Chapter 12. Use that code as a starting point to create the Urn class, modifying it to remove the methods in the ArrayStack class (push, pop, etc) then add the methods described below. Also change the variable names to reflect the new class. For example the array name should NOT be stack, instead it should...

  • The object class should be a Student with the following attributes: id: integer first name: String...

    The object class should be a Student with the following attributes: id: integer first name: String last name: String write the accessors, mutators, constructor, and toString(). In your main test class you will write your main method and do the following things: Create an array of Student objects with at least 5 students in your array. Write a sort method that must be your original work and included in the main class. The sort method will sort based on student...

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