Struggling with a uni project that i'm re-sitting

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

61 Replies - 2723 Views - Last Post: 07 June 2011 - 12:44 PM Rate Topic: -----

#1 langers_2004  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 28
  • Joined: 06-June 11

Struggling with a uni project that i'm re-sitting

Posted 06 June 2011 - 05:15 AM

Hi,

I am studying at uni and am re-sitting my python module. Which is different to the first year and im struggling a fair bit.

Ok im not looking for anyone to do the work for me but with the second and third section i have no idea where to start. The first section i have done the following. Anyway i will post and if you need more or less information just let me know. To begin with i will put up the first assignment and what i have done.

Assignment 1

Q1
Create new Python files/modules called main.py, publication.py, book.py and
disc.py. The modules will provide some simple facilities for representing different types of
publications.
In the modules above, create three new classes: Publication, Book and Disc in appropriate
files. Book and Disc should both be subclasses of Publication. Publication should be a
subclass of Python’s special class object (remember, if your class doesn’t inherit from any other
class, then you should make it inherit from object). The Publication class should store the title,
publication date and publicationID of a publication. A default ID of xxxx should be given to all
publications when created.

Q2
Next, the Book class should also have a private pagecount attribute of type integer and Disc
should have a private playingtime (in minutes) attribute of type integer. Each of these three
classes should have a public displayInfo() method that prints the details of the object to the
terminal.

Q3
Its a good idea to test your classes to make sure they work. To save you some time, a test function has
been provided. You can copy and paste the code from this document into the main.py module
(which, don't forget, imports the publication, book and disc classes).
def test():
book1=Book('Lord of the Kings','Science Fantasy',987)
book2=Book('Love Never Dies','Romance',176)
book3=Book('Ten to Midnight','Crime Thriller',235)
disc1=Disc('Hello Dolly','Comedy',115)
disc2=Disc('Lord of the Rings','Science Fantasy',542)
disc3=Disc('Needles and Pins','Medical Reference',120)
print '\nTesting...\n'
book1.displayInfo()
book2.displayInfo()
book3.displayInfo()
disc1.displayInfo()
disc2.displayInfo()
disc3.displayInfo()
if __name__=='__main__':
test()
print'\nHave a nice day'
Save the screen output of the test function above to test your program and prove that it works.

Q4
Add a new method called updateID() to the publication class. This should prompt the user for input
and then assign the value entered to the publicationID private attribute. publicationID
should be of type string.

Q5
Modify the test() method so that it uses a list of Book and Disc objects. Loop through the list
and print each object’s information to the screen using the displayInfo() method.
You'll need to figure out where to initialise your list. It could look something like:
publist = [Book('Lord of the Rings','Science Fantasy' ),
Book('Love never dies','Romance'),
Book('Use of Weapons','Science Fiction'),
Disc('Hello Dolly','Comedy'),
Disc('Lord of the Rings','Science Fantasy')]

Q6
Briefly describe the following concepts: classes, inheritance, superclasses, subclasses

(I have done this question, just thought id leave it on here for reference)

The Code so far

#main py

def test():
book1=Book('Lord of the Kings','Science Fantasy',987)
book2=Book('Love Never Dies','Romance',176)
book3=Book('Ten to Midnight','Crime Thriller',235)
disc1=Disc('Hello Dolly','Comedy',115)
disc2=Disc('Lord of the Rings','Science Fantasy',542)
disc3=Disc('Needles and Pins','Medical Reference',120)
print '\nTesting...\n'
book1.displayInfo()
book2.displayInfo()
book3.displayInfo()
disc1.displayInfo()
disc2.displayInfo()
disc3.displayInfo()
if __name__=='__main__':
test()
print'\nHave a nice day'


# Book Class Definition

class Book:
    def__init__(self,title,genre,number):
        self.title=title
        self.genre=genre
        self.number=number

        
def __init__(self,title,genre,number):
Publication.__init__(self,title,genre)
return


# Disk Class Definition

class Disk:
    def__init__(self,title,genre,number):
        self.title=title
        sefl.genre=genre
        self.number=number

def __init__(self,title,genre,number):
Publication.__init__(self,title,genre)
return


# Publication class definition

class(object)

class publication(object):

    def__init__(self,title,genre)
        self.title=title
        self.genre=genre
        self.defaultID=xxxx
        return


Right any help will be greatly appreciated.

This post has been edited by langers_2004: 06 June 2011 - 05:16 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Struggling with a uni project that i'm re-sitting

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Struggling with a uni project that i'm re-sitting

Posted 06 June 2011 - 06:05 AM

I guess my question becomes, what parts are you having trouble with? Asking specific questions will help you get answers.
Was This Post Helpful? 0
  • +
  • -

#3 langers_2004  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 28
  • Joined: 06-June 11

Re: Struggling with a uni project that i'm re-sitting

Posted 06 June 2011 - 06:43 AM

Um well to be honest it is what to do next.

Im having help from an extra tutor too as i have learning difficulties but its not really helping.

Im just not too sure what it is really asking me to do and how to do it. I think ive created the classes properly (supposidly the easy bit) just trying to work out what comes now is where im struggling at the moment.

does this help, or do i need to be more specific.

thanks scott
Was This Post Helpful? 0
  • +
  • -

#4 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Struggling with a uni project that i'm re-sitting

Posted 06 June 2011 - 07:00 AM

You need to be more specific, for example:

"I need to do <......>. What I have done so far is <......> but I am hung up because <......>. Do you know why <......> isn't working for me? I'm getting <......> error."
Was This Post Helpful? 1
  • +
  • -

#5 langers_2004  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 28
  • Joined: 06-June 11

Re: Struggling with a uni project that i'm re-sitting

Posted 06 June 2011 - 07:19 AM

Right ok thanks.

I think i need to know what im actually trying to do myself then as i just dont get what Q2 is asking.

Is there a tuition service online do you know as this subject is torturing me, and my uni just aren't providing me the extra help i need...
Was This Post Helpful? 0
  • +
  • -

#6 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Struggling with a uni project that i'm re-sitting

Posted 06 June 2011 - 07:25 AM

Problems I see:
In both Book and Disk, you've defined __init__ twice. Neither of them inherit from Publication (see how Publication inherits from Objject). Once they do, you won't need to redefine title or genre, as these are already taken care of in the parent class. Finally, you will need to define a new function in all three classes, called displayInfo().
Was This Post Helpful? 1
  • +
  • -

#7 langers_2004  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 28
  • Joined: 06-June 11

Re: Struggling with a uni project that i'm re-sitting

Posted 06 June 2011 - 07:40 AM

Ok so to begin with change it to look like this?

# Book Class Definition

class Book:
    def__init__(self,title,genre,number):
        self.title=title
        self.genre=genre
        self.number=number

class publication(object):
        
    def __init__(self,title,genre,number):
        Publication.__init__(self,title,genre)
        return


Was This Post Helpful? 0
  • +
  • -

#8 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Struggling with a uni project that i'm re-sitting

Posted 06 June 2011 - 08:26 AM

Mine would look like:

# Book Class Definition

class Book(Publication):
    def __init__(self,title,genre,number):
        Publication.__init__(self,title,genre)
        self.number=number

    def displayInfo(self):
        ...


Was This Post Helpful? 1
  • +
  • -

#9 langers_2004  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 28
  • Joined: 06-June 11

Re: Struggling with a uni project that i'm re-sitting

Posted 06 June 2011 - 08:42 AM

View PostMotoma, on 06 June 2011 - 08:26 AM, said:

Mine would look like:

# Book Class Definition

class Book(Publication):
    def __init__(self,title,genre,number):
        Publication.__init__(self,title,genre)
        self.number=number

    def displayInfo(self):
        ...




Why did you drop genre and title?

does the def displayinfo(self):

Do the same thing as the code before changing?
Was This Post Helpful? 0
  • +
  • -

#10 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Struggling with a uni project that i'm re-sitting

Posted 06 June 2011 - 08:46 AM

On line 3 Book is inheriting from Publication.
On line 5 Book is invoking Publication's initializer, which will set title and genre appropriately.
On line 8 I have defined a function displayInfo().
On line 9 you will write the code for printing the details as per Q2!

This post has been edited by Motoma: 06 June 2011 - 08:46 AM

Was This Post Helpful? 1
  • +
  • -

#11 langers_2004  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 28
  • Joined: 06-June 11

Re: Struggling with a uni project that i'm re-sitting

Posted 06 June 2011 - 08:54 AM

Great thanks, So would this be correct?

# Book Class Definition

class Book(publication):
    def__init__(self,title,genre,number):
        Publication.__init__(self,title,genre)
        self.number=number

    def displayInfo(self):
        print self.title
        print self.genre
        print self.number
        return


Was This Post Helpful? 0
  • +
  • -

#12 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Struggling with a uni project that i'm re-sitting

Posted 06 June 2011 - 09:00 AM

Depending on the version of Python you are using, yes.

If you are using 2.x, that is fine, if you are using 3.x you will need to surround your print arguments with parentheses:
    def displayInfo(self):
        print(self.title)
        print(self.genre)
        print(self.number)


And you only need a return statement when you are either a) short circuiting the code or B) sending back a value to the calling code.
Was This Post Helpful? 1
  • +
  • -

#13 langers_2004  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 28
  • Joined: 06-June 11

Re: Struggling with a uni project that i'm re-sitting

Posted 06 June 2011 - 09:06 AM

Brilliant thanks. Im using 3.1 so my code now looks like this.

# Book Class Definition

class Book(publication):
    def__init__(self,title,genre,number):
        Publication.__init__(self,title,genre)
        self.number=number

    def displayInfo(self):
        print (self.title)
        print (self.genre)
        print (self.number)
        return

    



Ok, I'll try running the code in question 4 and print that here. Im assuming i just copy it in and run it.

In question 2 it also mentions pagecount and playing time. Are these done in the same way as the publication.__init__
Was This Post Helpful? 0
  • +
  • -

#14 langers_2004  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 28
  • Joined: 06-June 11

Re: Struggling with a uni project that i'm re-sitting

Posted 06 June 2011 - 09:23 AM

Ok updated modules:

# Book Class Definition

from publication import

    __pagecount=0

class Book(publication):
    def__init__(self,title,genre,number):
        Publication.__init__(self,title,genre)
        self.number=number

    def displayInfo(self):
        print (self.title)
        print (self.genre)
        print (self.number)
        return



# Disk Class Definition

from publication import

class Disk(publication):

    __playingTime=0
    
    def__init__(self,title,genre,number):
        self.title=title
        sefl.genre=genre
        self.number=number

def __init__(self,title,genre,number):
Publication.__init__(self,title,genre)
return



# Publication class definition

class(object)

class publication(object):

    def__init__(self,pubtitle,pubdate)
        self.title=title
        self.genre=pubdate
        self.pubID=xxxx
        return
    
    def displayInfo(self):
        print "Title: %s - Date: %s"%(self.title,self.date)
        return




How is that looking now?
Was This Post Helpful? 0
  • +
  • -

#15 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

Re: Struggling with a uni project that i'm re-sitting

Posted 06 June 2011 - 09:24 AM

Change number to pageCount:

# Book Class Definition

class Book(publication):
    def__init__(self,title,genre,pageCount):
        Publication.__init__(self,title,genre)
        self.pageCount=pageCount

    def displayInfo(self):
        print (self.title)
        print (self.genre)
        print (self.number)
        return



And then copy the structure for Disk.
Was This Post Helpful? 1
  • +
  • -

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »