Question

Write Python code to define a class called Student. The Student class should have the following...

Write Python code to define a class called Student.

The Student class should have the following private fields:

  1. name – to store the name of a student
  2. attend – to store how many times a student attend the class
  3. grades – a list of all student’s grades

The Student class should have the following methods:

  1. a constructor
  2. getter method for the name field
  3. attendClass method: that increments the attend field (should be called every time a student attends the class).
  4. addGrade method: adds a new grade to the grades list
  5. __str__ method: prints all student’s information

In the same file, write a main function to test the functionality of the Student class.

Homework Answers

Answer #1


class Student:
    def __init__(self, name='', attend=0, grades=[]):
        self.name = name
        self.attend = attend
        self.grades = grades

    def get_name(self):
        return self.name

    def attendClass(self):
        self.attend += 1

    def addGrade(self, grade):
        self.grades.append(grade)

    def __str__(self):
        result = 'Name: ' + self.name + '\n'
        result += 'Number of classes attending: ' + str(self.attend) + '\n'
        result += 'Grades are: ' + str(self.grades)
        return result


def main():
    s = Student('Ronaldo')
    s.attendClass()
    s.addGrade(97)
    s.attendClass()
    s.addGrade(92)
    print(s)


main()


Know the answer?
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
Similar Questions
In c++ create a class to maintain a GradeBook. The class should allow information on up...
In c++ create a class to maintain a GradeBook. The class should allow information on up to 3 students to be stored. The information for each student should be encapsulated in a Student class and should include the student's last name and up to 5 grades for the student. Note that less than 5 grades may sometimes be stored. Your GradeBook class should at least support operations to add a student record to the end of the book (i.e., the...
write code using python or repl.it 1. List 4 attributes that you'd create for class "student"....
write code using python or repl.it 1. List 4 attributes that you'd create for class "student". 2. List 2 setters and 2 getters for class student including their parameters 3. Write the complete definition for class student including attributes and methods created above using the correct Python syntax. 4. Create one object of class student and print the values of all its attributes. You can either call the getter method or access the attribute directly using the dot notation.
Write a Java class called Grades in a class file called Grades.java. 2. Grades reads from...
Write a Java class called Grades in a class file called Grades.java. 2. Grades reads from a text file containing a series of course grades (a value between 0.0 and 100.0) with one grade entry per line. However, the first line in the file is an integer value specifying how many grade entries are contained in the file. 3. The Grades class contains four static methods: a. A method called loadGrades() that opens the file, reads in the data and...
By using Python: a, Make a class called Book. The __init__() method for Book should store...
By using Python: a, Make a class called Book. The __init__() method for Book should store two attributes: a title and an author. Make a method called book_info() that prints these two pieces of information, and a method called book_available() that prints a message indicating that the book is available in the library. b. Create an instance called book1 from your class. Print the two attributes individually, and then call both methods.
* Python Programming * Write a class definition named Book. The Book class should have instance...
* Python Programming * Write a class definition named Book. The Book class should have instance variables for a book's title, the author's name, and the publisher's name. The class should also have the following: a) An __init__ method for the class. The method should accept an argument for each of the instance variables. b) An __str__ method that returns a string indicating the state of the object. Use the Car class file in resources/Margie's Notes/car.py as an example, but...
Write a program that utilizes a Professor class. A professor has a first name, last name,...
Write a program that utilizes a Professor class. A professor has a first name, last name, affiliation, easiness grade, and a helpfulness grade. Grades range from 1 (lowest) to 5 (highest). The Professor class has two constructors. The first constructor initiates the object with just the first name, the last name, and the affiliation. The default value for easiness and helpfulness grade is 3. The second constructor initiates the object using the first name, the last name, the affiliation, and...
3.Write a method, addItem, for the class Shop that adds an item to the shop, the...
3.Write a method, addItem, for the class Shop that adds an item to the shop, the items should be stored in a python type list. An example of usage is the following: -------------------------------------------------------------- >>> myShop = Shop("Bezza", "Salmiya", (10.5, 6.3, 20.1) ) >>> myShop.addItem("Rice") >>> myShop.addItem("Stew") >>> myShop.addItem("Shrimp") 2.Write a class, called Shop, that has a constructor, __init__, method that takes in the name of the shop, the location of the shop, and the dimensions of the shop being a...
java Create a program that defines a class called circle. Circle should have a member variable...
java Create a program that defines a class called circle. Circle should have a member variable called radius that is used to store the radius of the circle. Circle should also have a member method called calcArea that calculates the area of the circle using the formula area = pi*r^2. Area should NOT be stored in a member variable of circle to avoid stale data. Use the value 3.14 for PI. For now, make radius public and access it directly...
Part 1 - LIST Create an unsorted LIST class ( You should already have this code...
Part 1 - LIST Create an unsorted LIST class ( You should already have this code from a prior assignment ). Each list should be able to store 100 names. Part 2 - Create a Class ArrayListClass It will contain an array of 27 "list" classes. Next, create a Class in which is composed a array of 27 list classes. Ignore index 0... Indexes 1...26 correspond to the first letter of a Last name. Again - ignore index 0. index...
Define a class named Employee that has four data fields: name: String hoursWorked: double hourlyPayrate: double...
Define a class named Employee that has four data fields: name: String hoursWorked: double hourlyPayrate: double bonusRate: double The class has two constructors: 1. constructor without arguments: initialize name to empty string, and all other data fields to 0.0 2. constructor with a String type arguments and three double type arguments. Use them to initialize the data fields properly The class also has: 1. Getter method for each data field to return the value of the data field 2. Setter...