Close
Close

Practice questions on Your class?

Level 1

1.
Create a Cricle class and intialize it with radius. Make two methods getArea and getCircumference inside this class.
class Circle():
  def __init__(self,radius):
    self.radius = radius
  def  getArea(self):
    return 3.14*self.radius*self.radius
  def getCircumference(self):
    return self.radius*2*3.14

2.
Create a Temprature class. Make two methods :
1. convertFahrenheit - It will take celsius and will print it into Fahrenheit.
2. convertCelsius - It will take Fahrenheit and will convert it into Celsius.
class Temprature():
  def  convertFahrenhiet(self,celsius):
    return (celsius*(9/5))+32
  def convertCelsius(self,farenhiet):
    return (farenhiet-32)*(5/9)

3.
Create a Student class and initialize it with name and roll number. Make methods to :
1. Display - It should display all informations of the student.
2. setAge - It should assign age to student
3. setMarks - It should assign marks to the student.
class Student():
  def __init__(self,name,roll):
    self.name = name
    self.roll= roll
  def display(self):
    print self.name
    print self.roll
  def setAge(self,age):
    self.age=age
  def setMarks(self,marks):
    self.marks = marks

4.
Create a Time class and initialize it with hours and minutes.
1. Make a method addTime which should take two time object and add them. E.g.- (2 hour and 50 min)+(1 hr and 20 min) is (4 hr and 10 min)
2. Make a method displayTime which should print the time.
3. Make a method DisplayMinute which should display the total minutes in the Time. E.g.- (1 hr 2 min) should display 62 minute.
class Time():

  def __init__(self, hours, mins):
    self.hours = hours
    self.mins = mins

  def addTime(t1, t2):
    t3 = Time(0,0)
    if t1.mins+t2.mins > 60:
      t3.hours = (t1.mins+t2.mins)/60
    t3.hours = t3.hours+t1.hours+t2.hours
    t3.mins = (t1.mins+t2.mins)-(((t1.mins+t2.mins)/60)*60)
    return t3

  def displayTime(self):
    print "Time is",self.hours,"hours and",self.mins,"minutes."

  def displayMinute(self):
    print (self.hours*60)+self.mins

a = Time(2,50)
b = Time(1,20)
c = Time.addTime(a,b)
c.displayTime()
c.displayMinute()

Level 2

Level 3

Ask Yours
Post Yours