Classes and Objects in Ruby

Submitted by Karthikeyan on
Example #1

class Stud
  def getdata()
    puts "Enter Numer:"
    @sno = gets()
    puts "Enter name:"
    @sname = gets()
  end
  def putdata()
    puts "Your Number is #{@sno}"
    puts "Your Name is #{@sname}"
  end
end
class Smarks < Stud
  def initialize(m1,m2,m3)
    @m1 = m1
    @m2 = m2
    @m3 = m3
  end
  def total
    if @m1 >=50 and @m2>=50 and @m3>=50
      pass = 1
    else
      pass = 0
    end
      @total = @m1 + @m2+@m3
      @average = @total/3
        if @average >=80
          @cls="Distinction"
        elsif @average >=60
          @cls="First"
          elsif @average >=50
          @cls="Second"
        end

    dispdata(pass)
    end
    def dispdata(pass)
      puts "Student Details"
      puts "----------------"
      putdata
      puts "Total is #{@total}"
      puts "Average is #{@average}"      
    if pass==1
      puts "Result : PASS"
      puts "Class : #{@cls}"
      puts "----------------"
    else
      puts "Result : RE-APPEAR"
      puts "----------------"
    end

     end
end
a=Smarks.new(49,64,94)
a.getdata()
a.total
c=Smarks.new(52,64,78)
c.getdata()
c.total
Sample Ouput :-

Enter Numer:
15
Enter name:
raju
Student Details
----------------
Your Number is 15
Your Name is raju
Total is 207
Average is 69
Result : RE-APPEAR
----------------
Enter Numer:
75
Enter name:
Memo
Student Details
----------------
Your Number is 75
Your Name is Memo
Total is 194
Average is 64
Result : PASS
Class : First
----------------