Hi, my professor is asking a project.

Overview:
Get x number of students for a simple survey. Make a data analysis, including
[1] name
[2] gender
[3] ID
[4] Question 1 and data analysis
- probability
- %
- distribution
In this project, each student is an "object"

I am in introductory computer science course (1000 course), I do not really understand what he meant by "object". I know it has to do with OOP, use classes. But I do not really know what I should do. This is done in Python.

The first thing I would do is generate a cvs file and use the cvsreader interface to read a cvs file.

I will ask him whether he wants me to use existing interface, or create our own class to read a file.

So what is this "object" for each student? A class....

anyone has any recommendation on how to start it?

Just say the simplest way, have a bunch of data written inside the python script, and let the scripts read them. (forget about cvs for now)

Now, how do we make a class so that each interruption, each analysis, that student is an object?

I appropriate any feedback. Thanks for any reply.

Recommended Answers

All 8 Replies

Well, in Python just about anything is an object not just the typical OOP stuff. I assume that your instructor forgot about that and actualy means creating a class Student where each instance is a particular student.

Yes, you could create a general Student class and then go about creating instances of the class. Each instance would represent a different student and would contain the necessary data to individualize and represent the student's details.

If you look around this forum you'll find examples of "shapes" and "pets" classes that others have provided. They are very basic examples of making a class and then creating instances of said class. That should get you well on your way

Don't let the object paradigm confuse you. It is actually rather easy to understand...maybe tricky to code at first, but it is not a confusing concept.

'object' means different things in different programming languages, and as mentioned before, in python almost everything is an object.

An object basically is something (in the scene that it is a 'thing') that has methods and attributes. So, it is like this or like that and it can do this or that.

A class is really just a blueprint for a 'thing' (lets rather say 'type'...you will probably read stuff like Bruce Eckel's books at some point.) Forget about dogs, with four legs and barking (when I first started to try to get my head around it, all these dogs and tables just made it seem more mysterious and confusing.)

Anyway, stick to this idea of a blueprint. Basically it says 'let the programmer think about his problem' not 'let the programmer think about bits and loops and the structure of the machine.'

A blueprint basically means that you want to look at your problem and create bits that fix it.

So, in your problem, your class would need to create methods or variables for each thing you need from students.

Here is a start:

#! /usr/bin/python
#  jlm699.py

class StudentDataEngine:
    def __init__(self, name, gender, ID, moreStuff):
        namelist = name.split()
        for a in range(len(namelist)):
            namelist[a] = namelist[a].capitalize()
        self.name = " ".join(namelist)
        self.gender = gender
        self.ID = ID
        self.moreStuff = moreStuff
    def PrintOut(self):
        print "Name".ljust(15) + ":" + self.name
        print "Gender".ljust(15) + ":" + self.gender
        print "ID".ljust(15) + ":" + self.ID
        print "moreStuff".ljust(15) + ":" + self.moreStuff + "\n"
        
    def WriteToFile(self):
        """Create a method to print to a txt file"""
    def WriteToDatabase(self):
        """Create a method to print to a database"""
    def SendByEmail(self):
        """Create a method to print to an email and send it"""
    def AndOtherThings(self):
        """You get the picture"""

if __name__ == "__main__":
    # If you run this from the command line, this will run
    # And it is great for testing stuff out.
    bob = StudentDataEngine('bob smith', 'male', '234 432 234', 'he is mad, totally crazy')
    bob.PrintOut()
    sue = StudentDataEngine('sue zingzong', 'female', '565 656 565', 'The guys think she is hot')
    sue.PrintOut()

Do you see what I mean about each 'object' being a thing, and that you can think more about what you want it to do, rather than on the flow of the program. Make it do what you want, and then call it when you need it.

Thanks for the help. A few questions came up with my attempt:

class Cat(object):
    def __init__(self, num=1, age=1):
        self.num = num
        self.age = age
    def costTotal(self, cost=100):
        self.cost = cost
        self.costTotal = self.cost + self.cost * self.num + self.cost * self.age
    def printIt(self):
        print self.num, self.age
        print self.costTotal

cat = Cat(2,3)
cat.printIt()

[1] I cannot get the second printIt worked. It showed <bound method .....
I got self.num, self.age working but not the second line. What did I do wrong?

[2] cost=100
I see how you have private and public in the following code
http://www.daniweb.com/forums/post882964.html#post882964
Is this cost=100 defined as x=0
I want to say
x=0
user may input x = something else

Thanks for any help.

class Cat(object):
    def __init__(self, num=1, age=1):
        self.num = num
        self.age = age
    def costTotal(self, cost=100):
        self.cost = cost
        self.costTotal = self.cost + self.cost * self.num + self.cost * self.age
    def printIt(self):
        print self.num, self.age
        print self.costTotal

cat = Cat(2,3)
cat.printIt()

[1] I cannot get the second printIt worked. It showed <bound method .....
I got self.num, self.age working but not the second line. What did I do wrong?

You need to call the function costTotal and provide the parameters if needed. So your line that says print self.costTotal should actually be print costTotal() ... then in your costTotal function I believe the last line should be return self.cost + self.cost * self.num + self.cost * self.age . The way it is now, you're effectively "erasing" the function costTotal and replacing it with the value of that calculation.

I don't understand your second question.

You need to call the function costTotal and provide the parameters if needed. So your line that says print self.costTotal should actually be print costTotal() ... then in your costTotal function I believe the last line should be return self.cost + self.cost * self.num + self.cost * self.age . The way it is now, you're effectively "erasing" the function costTotal and replacing it with the value of that calculation.

I don't understand your second question.

Hi, thanks for the correction.

My second question is:\

def costTotal(self, cost=100):

what exactly is this cost=100?
is it the same thing as
x = 0
x = input("Enter a number: ")
or I set
x= 0
print x

My second question is:\

def costTotal(self, cost=100):

what exactly is this cost=100?

In a function declaration, the items inside the parenthesis are called parameters. These are basically variables that are "passed" to the function. By assigning a value (of 100) to a parameter you are making it optional. By optional, I mean that you aren't required to pass that value to the function in order for it to work. When making it optional you provide a default value. So you can call the function two ways:

self.costTotal()

Inside the function, if you were to add a print cost as the first line you'd see that cost is equal to 100 (the default value). Now if you called the function like this:

self.costTotal(250)

Your print statement would show that cost is now equal to 250.

Hope that clears it up.

It really makes sense.

Thanks jim. As you might notice from my other post (on object), I am attemping writing classes.

Thanks for this introductory help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.