Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Faiha - Lesson 8
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
# Object Oriented Programming ''' 1. Create a Class called Student 2. Instantiate an object of the class (create an example of the class) 3. Student(): attribute: first name attribute: last name attribute: email address Class: Student() Object: std_1, std_2, ... Constructor: def __init__(self, ...) Attributes: first, last, email, UIN Methods: __init__(self, ...), fullname() ''' # University, Chemistry Course [40 students] class Student: def __init__(self, first, last, UIN): # constructor self.first = first self.last = last self.email = first + '.' + last + '@email.com' self.UIN = UIN def fullname(self): return '{} {}'.format(self.first, self.last) # 'std_1' is an object for my class 'Student' std_1 = Student('Ali','Ahmed',100) std_2 = Student('Noor','Fatima',201) std_3 = Student('Shaikha','Ahmed',152) std_4 = Student('James','White',183) std_5 = Student('John','Hutch',104) std_6 = Student('Jim','Rogers',405) std_7 = Student('Little','Stuart',206) #print (std_1.fullname() ) ## -------------------------------------------------------------- ''' Answer the following: 1. What's the name of the class? Employee 2. What's the constructor? __init__ 3 a. How many arguments does the constructor take? 4 b. How many arguments are passed to the object of the class? 3 4. How many attributes are there? List them out. first, last, email, pay 5. Fill up the missing statements from lines 41-44 6. Create an object of the class. And run the code. emp_1 = Employee('Faiha','Maryam', 1000) 7. Who is the object for the class? emp_1 ''' class Employee: def __init__(self, first_name, last_name, salary): self.first = first_name self.last = last_name self.email = first_name + '.'+ last_name +'@email.com' self.pay = salary def GetName(self): print (self.first, self.last) def GetEmail(self): print (self.email) # write a method to display salary def GetSalary(self): print (self.pay) # write a method to raise the salary by some percentage def RaiseSalary(self, amt): self.pay=self.pay+(self.pay*amt/100) # write a method to display the gender def GetGender(self): print (self.gender) def SetGender(self, gender): self.gender = gender # write a method that gives the nationality of the employee def SetNationality(self, nationality): self.nationality=nationality def GetNationality(self): print(self.nationality) # Getters (Accessors) -> return/print something # Setters (Mutators) -> Setting/calculating something ''' Challenges 1. Print the name 2. Raise the salary 3. Include the gender of the employee ''' emp_1 = Employee('Faiha','Mariam', 10000) emp_1.set_gender('F') emp_1.set_nationality('Qatari') emp_1.disp_nationality() # First data structure: Stack
[
+
]
Show input
Absolute running time: 0.33 sec, cpu time: 0.24 sec, memory peak: 6 Mb, absolute service time: 0,34 sec
edit mode
|
history
|
discussion
Qatari