Assignment 2 – Tortoise v. Hare, OOP edition

Dean Zeller, Chris Harris Due Friday, September 15th

CS200 20 points

Fall 2017

Objective The student will retool the Tortoise v Hare programs to implement Object Oriented Programming, including classes, attributes, and methods.

Background: Object Oriented Programming

The OOP methodology is a completely different way to organize and design code. Rather than focusing on an algorithm to solve a particular problem, OOP creates objects with underlying behavior. This primary advantage of OOP is encapsulation, where each object is one in itself, making it easier to debug and maintain code. An OOP programmer does not need to know exactly how an object does its job, just that it does it correctly. This is not the case in traditional algorithmic programming. Encapsulation makes it far easier to design, code, debug, and maintain very large projects.

Ironically, one disadvantage of OOP is that it is more difficult to analyze algorithmically. Flow of control changes so rapidly that a coherent mathematical analysis requires additional steps. For this reason, CS301 (Algorithms and Data Structures) typically does not use objects. As such, programers should be familiar with both methods.

Instructions

Follow these instructions below to complete the assignment. Before starting, read all instructions completely, so you know what is expected. This assignment assumes students will solve the problem in Python first, but that is not a requirement.

1. Review the Python and Java files from the previous assignment. Recall the algorithm used and techniques for solving the problem. It is not advised that you try to modify these tiles to include OOP, as the underlying design is quite different. Read over the object skeletons in Python and Java, so you can understand the object structure.

2. Design your program skeleton, and then use code as necessary from the first assignment as a guide on solving the problem in OOP. You may end up writing all new code, and not using copy-n-paste at all.

3. Create the Animal object in Python, or alternately, Contestant. This object will represent one participant in the race.

4. Create the following attributes for the Animal object:
name species speed
napStart napDuration currentPosition

5. Create the following methods in the Animal object:
constructor (__init__)
getName getSpecies
getSpeed setSpeed
getNapStart setNapStart
getNapDuration setNapDuration
getCurrentPosition setCurrentPosition
advancePosition displayInfo
You may create additional methods as necessary to solve the problem. (Note: in some OOP designs, this is not allowed.)

6. Create an AnimalTester object, with the sole purpose of thoroughly testing the Animal object. Instantiate several animals manually, and test them to ensure each of the methods work. Do not delete the tester file – it is part of the program submission.

7. Create the Race object in Python. This object will represent a single race.


8. Create the following attributes for the Race object:
eventName distance
contestants (either as separate variables or an array)
Again, you may create additional methods as necessary to solve the problem. (Note: in some OOP designs, this is not allowed.)

9. Create the following methods in the Race object:
constructor (__init__)
getEventName
runRace
displayInfo

10. Create a RaceTester object, with the sole purpose of thoroughly testing the Race object. Instantiate a race object and run a race with specified parameters. Do not delete this file – it is part of the program submission.

11. Repeat steps 1 thru 10, this time solving the problem using Java. Consult the Java object skeleton to get you started. As best as possible, follow the exact same logic, allowing for comparison purposes.

12. Double-check that both programs meet the criteria specified on the rubric at the end of the assignment.

13. Add features to both programs. Any features added should be available in both programs. (Note: if one of the features you are selecting is quite complex and/or language specific, it may be completed in just one of the programs.)

14. Have both programs up and running on a computer during class, and demonstrate your programs for the instructor or TA. They will evaluate the correctness of your work. Do not submit your work to Canvas until it has been evaluated.

15. Once it has been approved, submit your work to the appropriate dropbox on Canvas by the due date.

Documentation

Use a similar block documentation style as shown in previous assignments.

Grading

You will be graded on the following criteria:

Accuracy Correctly solving the problem as described, using OOP

Readability Block documentation, proper indentation and variable names, inline comments

Creativity Imagination and originality in the features implemented

Extra credit will be given for implementing additional or more complex features. A list of ideas is available in the Assignment 1 writeup.


PythonSkeleton.py

###########################################################################

# OOP Skeleton in Python #

# #

# Programmed by Dean Zeller (08-29-2017) #

# Class: CS200 #

# Instructor: Dean Zeller #

# #

# Description: This is a basic demonstration of objects in Python, #

# including a class, a constructor, attributes, and #

# methods. A similar skeleton was designed in Java for #

# comparison purposes. #

# #

###########################################################################

class Object: # class definition, name with capital letter

#######################################################################

# __init__ method #

# #

# This is the constructor method. It creates the attributes and #

# initializes them to the given parameters. #

#######################################################################

def __init__ ( # python constructor is special name __init__

self, # indicates that this is a method

stringParameter, # required parameter

intParameter=0 # optional parameter

):

print("--> Creating object",stringParameter+", in constructor")

print("----> Setting stringAttribute to",stringParameter,

"and intAttribute to",intParameter)

# Initialize attributes

self.stringAttribute = stringParameter

self.intAttribute = intParameter

return

#######################################################################

# getStringAttribute #

# #

# This method returns the stringAttribute. #

#######################################################################

def getStringAttribute(self):

print("--> In getStringAttribute")

print("----> Returning stringAttribute,",self.stringAttribute)

# return stringAttribute

return self.stringAttribute

#######################################################################

# setIntAttribute #

# #

# This method prints the stringAttribute intAttribute times. #

#######################################################################

def setIntAttribute(self, num):

print("--> In setIntAttribute")

print("----> Setting intAttribute to",num)

# set intAttribute to num

self.intAttribute = num

return


#######################################################################

# outputMethod #

# #

# This method prints the stringAttribute intAttribute times. #

#######################################################################

def outputMethod(self):

print("--> In outputMethod")

print("----> printing",self.stringAttribute,self.intAttribute,"times.")

# print output

print(self.stringAttribute * self.intAttribute)

return

PythonSkeletonTester.py

###########################################################################

# OOP Skeleton Tester #

# #

# Programmed by Dean Zeller (08-29-2017) #

# Class: CS200 #

# Instructor: Dean Zeller #

# #

# Description: This is a tester of the PythonSkeleton object. #

# #

###########################################################################

from PythonSkeleton import Object

# instantiate new objects

print("Instantiating two objects")

obj1 = Object("Object1", 5) # Both parameters supplied

obj2 = Object("Object2") # Only string supplied

# test getStringAttribute

print("Testing getStringAttribute")

print("Object 1 string attribute:",obj1.getStringAttribute())

print("Object 2 string attribute:",obj2.getStringAttribute())

# test outputMethod

print("Testing outputMethod")

obj1.outputMethod()

obj2.outputMethod()

# test setIntAttribute

print("Testing setIntAttribute")

obj1.setIntAttribute(3)

obj2.setIntAttribute(10)

# retest outputMethod

print("Testing outputMethod again, with changed intAttributes")

obj1.outputMethod()

obj2.outputMethod()

JavaSkeleton.java

(will be forthcoming)

JavaSkeletontester.java

(will be forthcoming)


Assignment 2 Grading Rubric Name: _____________________________

CS200 Instructor: Chris Harris Dean Zeller

Evaluated by: _______________________

General Requirements

Python Java

______ ______ Program runs correctly

No credit if code does not run, for any reason

______ ______ Block documentation

-1 to -2 for incorrect description or entries

-3 if omitted altogether

______ ______ Coding style

Descriptive variable names

Proper indentation and line spacing

-1 to -3 for style

______ ______ Submitted to Canvas after checked by instructor

-10 for missing Canvas submission

Assignment Requirements

Python Java

______ ______ OOP design, with tester files

______ ______ User input

String inputs

Numerical inputs

______ ______ Data Table

All relevant data

Nap indicator

Stops at appropriate distance

______ ______ Conclusions

Winner can be tortoise, hare, or a tie

Different messages, depending on winner

______ ______ Feature #1: _____________________________________________

______ ______ Feature #2: _____________________________________________

______ ______ Feature #3: _____________________________________________