## Blog Pages

Showing posts with label Class. Show all posts
Showing posts with label Class. Show all posts

### program to find number of days between two given dates

class Date:

def __init__(self, d, m, y):

self.d = d

self.m = m

self.y = y

monthDays = [31, 28, 31, 30, 31, 30,

31, 31, 30, 31, 30, 31]

def countLeapYears(d):

years = d.y

if (d.m <= 2):

years -= 1

return int(years / 4) - int(years / 100) + int(years / 400)

def getDifference(dt1, dt2):

n1 = dt1.y * 365 + dt1.d

for i in range(0, dt1.m - 1):

n1 += monthDays[i]

n1 += countLeapYears(dt1)

n2 = dt2.y * 365 + dt2.d

for i in range(0, dt2.m - 1):

n2 += monthDays[i]

n2 += countLeapYears(dt2)

return (n2 - n1)

dt1 = Date(13, 12, 2018)

dt2 = Date(25, 2, 2019)

print(getDifference(dt1, dt2), "days")

### Program to find the maximum and minimum value node from a circular linked list

class Node:

def __init__(self,data):

self.data = data;

self.next = None;

class CreateList:

def __init__(self):

self.tail = Node(None);

newNode = Node(data);

self.tail = newNode;

else:

self.tail.next = newNode;

self.tail = newNode;

def minNode(self):

print("List is empty");

else:

while(True):

if(minimum > current.data):

minimum = current.data;

current= current.next;

break;

print("Minimum value node in the list: "+ str(minimum));

def maxNode(self):

print("List is empty");

else:

while(True):

if(maximum < current.data):

maximum = current.data;

current= current.next;

break;

print("Maximum value node in the list: "+ str(maximum));

cl = CreateList();

cl.minNode();

cl.maxNode();

### Car Class Program

# define the Vehicle class
class Vehicle:
name = ""              # initial value setting inisde the clas iteself
kind = "car"           # initial value setting inisde the clas iteself
color = ""             # initial value setting inisde the clas iteself
value = 100.00         # initial value setting inisde the clas iteself
def description(self): # creating function inside the class amd return as string
desc_str = "%s is a %s %s worth \$%.2f." % (self.name, self.color, self.kind, self.value)
return desc_str

car= Vehicle()             # creates a object [car1] for class vehicle
print car.description()    # using created object calling the function inside class

car1 = Vehicle()           # creates a object car for class vehicle
car1.name = "Fer"          # using object[car1] variables on the class vehicle is assigned
car1.color = "red"         # using object[car1] variables on the class vehicle is assigned
car1.kind = "convertible"  # using object[car1] variables on the class vehicle is assigned
car1.value = 60000.00      # using object[car1] variables on the class vehicle is assigned

car2 = Vehicle()
car2.name = "Jump"
car2.color = "blue"
car2.kind = "van"
car2.value = 10000.00

print car.description()    # Using object [car] created calling the functions[description()]inside the class
print car1.description()   # Using object [car1] created calling the functions[description()]inside the class
print car2.description()   # Using object [car2] created calling the functions[description()]inside the class

### Simple Class Program in Python

class MyClass:          # class is created
variable = "blah"   # variable is created and assigned the value

def function(self): # funtion is created iniside the class as self
print "This is a message inside the class."

myobjectx = MyClass()     # object[myobjectx] is created for class
myobjectx.variable        # using created object variable in the class is accessed

myobjecty = MyClass()          # object[myobjecty] is created for class
myobjecty.variable = "yackity" # using created object variable in the class new value is assigned

print myobjectx.variable       # using corresponding object and variable the value is displayed
print myobjecty.variable       # using corresponding object and variable the value is displayed
print MyClass.variable         # using corresponding object and variable the value is displayed

myobjectx.function()           # using corresponding object and function inside the class is accessed