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
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.
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.
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...
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...
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...
1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class...
1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class called Student which implements Duty. Class Student adds 1 data field, id, and 2 methods, getId and setId, along with a 1-argument constructor. The duty of a Student is to study 40 hours a week. b. Write a class called Professor which implements Duty. Class Professor adds 1 data field, name, and 2 methods, getName and setName, along with a 1-argument constructor. The duty...
Part 1 Given the following code: public class MyClass { private double score; private String studentID;...
Part 1 Given the following code: public class MyClass { private double score; private String studentID; //code of the class ..... } //end of MyClass Code a default constructor and an overloaded constructor of MyClass that will assign values to its two instance fields: Please write in JAVA Part 2 Continue from question about, code an mutator of instance field called studentID:
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all four classes. Shape2D class For this class, include just an abstract method name get2DArea() that returns a double. Rectangle2D class Make this class inherit from the Shape2D class. Have it store a length and a width as fields. Provide a constructor that takes two double arguments and uses them to set the fields. Note, the area of a rectangle is the length times the...
Consider the following code snippet: Employee programmer = new Employee(10254, "exempt"); String str = programmer.toString(); Assume...
Consider the following code snippet: Employee programmer = new Employee(10254, "exempt"); String str = programmer.toString(); Assume that the Employee class has not implemented its own toString() method. What value will the variable str contain when this code is executed? Group of answer choices the variable will become a null reference the code will not compile because Employee has not implemented toString() the default from Object, which is the class name of the object and its hash code the string representations...
From the Examples in this module that I have posted, Write a class called ‘Student’ inheriting...
From the Examples in this module that I have posted, Write a class called ‘Student’ inheriting ‘Person’ class with the following properties and methods Degree GPA Getters and Setters Write another class called ‘Faculty’ inheriting ‘Employee’ with the following properties and methods Department Specialization Getters and setters. (customer.py has the classes you need) *** IN PYTHON PLEASE***
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT