CS441 Assignment 2 Solution and Discussion

Assignment No. 02
SEMESTER Fall 2019
CS441- Big Data Concepts
Total Marks: 20

Due Date: 28/09/2019

Please read the following instructions carefully before submitting assignment:
It should be clear that your assignment will not get any credit if:

o Assignment is submitted after due date.
o Submitted assignment does not open or file is corrupt.
o Assignment is copied (From internet/students).

Software allowed to develop Assignment

  • Python editor

Objectives:
To enable students to write, execute a program in Python. Moreover to familiarize students with the concepts of:

• Classes and objects
• Constructor
• Object declaration
• Methods/functions calling

Assignment Submission Instructions
You have to submit only.py file on the Assignments interface of CS441 at VULMS. Assignment submitted in any other format will not be accepted and will be graded zero marks.

Lectures Covered: This assignment covers Weeks # 03 - 05
Deadline
Your assignment must be uploaded/submitted on or before 28/09/2019.

Question statement:

Write a Python program that will create a class named Product.

This class will have 4 data members:

  1. Name
  2. Brand
  3. Price
  4. Quantity

The Product class will have the following setter functions:

setName (), which takes a single parameter
setBrand(), which takes a single parameter
setPrice(), which takes a single parameter
setQuantity(), which takes a single parameter

The Product class will have the following getter functions:
getName()
getBrand()
getPrice()
getQuantity()

You are required to write a parameterized constructor for this class.

In parameterized constructor, you will initialize all the data members with the values passed to it as an arguments. The message “Parameterized constructor called” should be displayed whenever an object is created with the parameterized constructor.

Now, create an object by using the parameterized constructor and display the values of data members through calling getters functions.

Then set the values of the data members by calling the setters functions and then display its values by calling getters functions.

Sample Output:
557f4778-6ab5-4dd1-b364-f463824e31e7-image.png

To solve this assignment, you need to install latest Python compiler or you can run (check) your code through online compiler at the following link:

https://www.onlinegdb.com/online_python_compiler
=====================================Ended=======================================

For any query about the assignment, contact at [email protected]

GOOD LUCK

Solution:

class Product:
    
    Name = "NULL"
    Brand= "NULL"
    Price= 0
    Quantity= 0

         
        
    def __init__(self, na, br, pr, qty):
        print
        print "--------------------------------"
        print "Pametarized Constructor called:"
        print "--------------------------------"
        self.Name = na
        self.Brand = br
        self.Price = pr
        self.Quantity=qty
        
              
    def setName(self, n):
        self.Name = n
        
    def setBrand(self, b):
        self.Brand=b
        
    def setPrice(self, p):
        self.Price=p
        
    def setQuantity(self, q):
        self.Quantity=q

        

    def getName(self):
        return self.Name
    
    def getBrand(self):
        return self.Brand

    def getPrice(self):
        return self.Price
    
    def getQuantity(self):
        return self.Quantity
    

P1= Product("Computer","DELL",48000,5)

print
print "Object values initialized through Constructor:"
print
print "Name: " , P1.getName()
print "Brand: ", P1.getBrand()
print "Price: ", P1.getPrice()
print "Quantity: ", P1.getQuantity()


P1.setName("Cloths")
P1.setBrand("Bonanza")
P1.setPrice(4500)
P1.setQuantity(10)
print
print "-------------------------------------------------"
print
print "Object values assigend thorugh setter functions:"

print
print "Name: " , P1.getName()
print "Brand: ", P1.getBrand()
print "Price: ", P1.getPrice()
print "Quantity: ", P1.getQuantity()

Solution:

class Product:
    
    Name = "NULL"
    Brand= "NULL"
    Price= 0
    Quantity= 0

         
        
    def __init__(self, na, br, pr, qty):
        print
        print "--------------------------------"
        print "Pametarized Constructor called:"
        print "--------------------------------"
        self.Name = na
        self.Brand = br
        self.Price = pr
        self.Quantity=qty
        
              
    def setName(self, n):
        self.Name = n
        
    def setBrand(self, b):
        self.Brand=b
        
    def setPrice(self, p):
        self.Price=p
        
    def setQuantity(self, q):
        self.Quantity=q

        

    def getName(self):
        return self.Name
    
    def getBrand(self):
        return self.Brand

    def getPrice(self):
        return self.Price
    
    def getQuantity(self):
        return self.Quantity
    

P1= Product("Computer","DELL",48000,5)

print
print "Object values initialized through Constructor:"
print
print "Name: " , P1.getName()
print "Brand: ", P1.getBrand()
print "Price: ", P1.getPrice()
print "Quantity: ", P1.getQuantity()


P1.setName("Cloths")
P1.setBrand("Bonanza")
P1.setPrice(4500)
P1.setQuantity(10)
print
print "-------------------------------------------------"
print
print "Object values assigend thorugh setter functions:"

print
print "Name: " , P1.getName()
print "Brand: ", P1.getBrand()
print "Price: ", P1.getPrice()
print "Quantity: ", P1.getQuantity()