


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An assignment overview for creating a python program that defines a custom class with attributes, constructor, accessor methods, mutator methods, and a main program to use the class. The assignment is worth 50 points and is due on the crashwhite.polytechnic.org server.
Typology: Schemes and Mind Maps
1 / 4
This page cannot be seen from the preview
Don't miss anything!



In this assignment you’ll be creating a short program called oop.py, which will define a class with attributes, accessor methods, and mutator methods, and include a brief example of using an object of that class in a main program. This assignment is worth 50 points and is due on the crashwhite.polytechnic.org server at 23:59:59 on the date given in class. BACKGROUND Object-oriented programming (OOP) is a style of programming that organizes data, and the programs that manipulate that data, into objects that are organized into classes. Each object has attributes —values that describe the object—and methods —things that the object can "do". Methods may be accessor methods that allow you to examine the object’s attributes, or mutator methods which cause a change in an object’s attributes. Object-oriented programming is built into Python—print "Hi there".capitalize() is a quick example of using an object, where the string object "Hi there" is retrieved by the accessor method .capitalize(). You may not have known you were using an object, and that’s partly the point of OOP —a well-written program will allow you to manipulate objects without having to worry too much about what’s happening behind the scenes, hidden in the "black box" of the class. This ability to conceal some of the underlying details of coding is called encapsulation. Python provides ready-made classes (like int, str, and list) that can be manipulated using ready-made methods (like float, len(), and .pop() ). The real power of objects, however, comes when a programmer is able to custom-design a class, often to represent a model of some Real World object. Some examples:
oop.py You should keep a working copy of this file in your home folder on the server and a backup copy of the file elsewhere. To submit your assignment for grading, copy your file to your /home/userID/forInstructor directory at crashwhite.polytechnic.org before the deadline. ASSIGNMENT NOTES ■ Here’s a full example of a program that creates a class Frog and then shows both how to use that class to create and manipulate models of a couple of different frogs. #!/usr/bin/env python """Demonstration on how to create and use an object in Python Richard White, 2013-03-20""" class Frog: """The Frog class demonstrates the use of attributes, constructor methods, accessor methods, and mutator methods.""" def init(self, commonName = "bullfrog"): """This is the constructor method that constructs the Frog when we first create it.""" self.name = commonName # assigns parameter to attribute self.location = [0,0] # establishes initial X,Y position self.age = "egg" # establishes initial age def getName(self): # Accessor methods used to return self.name # look at state of attribute def getLocation(self): return self.location def getAge(self): return self.age def grow(self): # Mutator method ages = ["egg","tadpole","adult","dead"] if self.age != "dead": self.age = ages[ages.index(self.age)+1] def vocalize(self): if self.name == "bullfrog": return "Brrrup!" elif self.name == "peeper": return "Peep!" else: return "Ribbit!" def jump(self,distance=[1,1]): self.location[0] += distance[0] self.location[1] += distance[1]