Question

Python Q2) Create a Student class and initialize it with name and roll number. Make methods...

Python Q2) Create a Student class and initialize it with name and roll number. Make methods to : Display - It should display all informations of the student. setAge - It should assign age to student setMarks - It should assign marks to the student.

Homework Answers

Answer #1

class Student:
#method to initialize the object
def __init__(obj, name, rollNum):
obj.name = name
obj.roll = rollNum
obj.age = 0
obj.marks = 0
  
#method to set the age
def setAge(obj, ag):
obj.age = ag

#method to set the marks
def setMarks(obj, mk):
obj.marks = mk   
  
#method to display the student information
def display(obj):
print("Name: " + obj.name)
print("Roll Number: " + str(obj.roll))
print("Age: " + str(obj.age))
print("Marks: " + str(obj.marks))

#create object
s1 = Student("John", 101)

#method calling
s1.setAge(18)
s1.setMarks(70)
s1.display()

The screenshot of the above source code is given below:

OUTPUT:

Name: John
Roll Number: 101
Age: 18
Marks: 70


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
Create a class Student with the states (attributes): Name, address, phone number, Student ID. Also define...
Create a class Student with the states (attributes): Name, address, phone number, Student ID. Also define the behavior of student as learn, perform assignment, read, attendance, do presentation (Exemple: My name is {Name}). After that create 3 instances of the class Student and present the instance (My name is .....)  
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all...
Create a class called Student. Include the following instance variables: name, address, phone, gpa Create all of the methods required for a standard user defined class: constructors, accessors, mutators, toString, equals Create the client for testing the Student class (5 points extra credit) Create another class called CourseSection (20 points extra credit) Include instance variables for: course name, days and times course meets (String), description of course, student a, student b, student c (all of type Student) Create all of...
Write a Python class, Flower, that has 3 instance variables name, num_petals and price which have...
Write a Python class, Flower, that has 3 instance variables name, num_petals and price which have types str, int and float. Your class must have a constructor method to initialize the variables to an appropriate value, and methods for setting the value of each instance variable (set methods) and for retrieving the instance variable values (get methods). You can use the credit card code we looked at for assistance.
Design a class with the following requirements: 1- give the class a name from your choice....
Design a class with the following requirements: 1- give the class a name from your choice. 2- write some code that creates three instance variables, choose a name and type for each one of them. (must have two private and one public variables) 3- Initialize the variables using two different constructors. 4- Use mutator and accessor methods (set and get) for the private data members. 5- Display the value of each member variable using a method. 6- In the main,...
Create a new project in BlueJ. Create two new classes in the project, with the following...
Create a new project in BlueJ. Create two new classes in the project, with the following specifications: Class name: Runner Fields: bibId (int) event (String) age (int) 1 Constructor: takes parameters runnerBibId, raceEvent, and runnerRaceDayAge as parameters to initialize fields Methods: Write get-methods (getters) for all three fields. The getters should be named getBibId, getEvent, and getAge. Save the project as a jar file to submit.
JAVA Learning Objectives: To be able to code a class structure with appropriate attributes and methods....
JAVA Learning Objectives: To be able to code a class structure with appropriate attributes and methods. To demonstrate the concept of inheritance. To be able to create different objects and use both default and overloaded constructors. Practice using encapsulation (setters and getters) and the toString method. Create a set of classes for various types of video content (TvShows, Movies, MiniSeries). Write a super or parent class that contains common attributes and subclasses with unique attributes for each class. Make sure...
Write the following problem in Java Create a class Dog, add name, breed, age, color as...
Write the following problem in Java Create a class Dog, add name, breed, age, color as the attributes. You need to choose the right types for those attributes. Create a constructor that requires no arguments. In this constructor, initialize name, breed, age, and color as you wish. Define a getter and a setter for each attribute. Define a method toString to return a String type, the returned string should have this information: “Hi, my name is Lucky. I am a...
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String),...
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String), Monthly Salary (double)). Create a constructor that initializes these variables. Define set and get methods for each variable. If the monthly salary is not positive, do not set the salary. Create a separate test class called EmployeeTest that will use the Employee class. By running this class, create two employees (Employee object) and print the annual salary of each employee (object). Then set the...
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.
Create a program named admission that takes as Input the student name, GPA and the admission...
Create a program named admission that takes as Input the student name, GPA and the admission Test Score, the application should display if the student got accepted or rejected. The program should use a function named AcceptOrReject( gpa, testScore) to control the program. Acceptance is based on the following criteria: The student can get accepted in the following two cases : 1- if the GPA is less than 3.0, the test score has to be more than 80 to get...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT