Wednesday, July 31, 2013

Building Classes from Scratch Based on Known Inputs and Desired Outputs

Today I had the privilege to undertake a challenge on Udacity.com's Introduction to Programming course that was extremely well designed to put my knowledge and abilities of Classes to the test.  The challenge was optional, however I was determined to stretch my knowledge in order to deepen my understanding, as well as intuit ways to solve OOD problems.

The problem was loosely stated as follows:
The program would take in as input a "story" of a photography company's business processes.  Namely, these processes consisted of:
  • Hiring new photographers
  • Taking in new assignments from clients
  • Allocating photographers for each assignment
  • Reviewing the portfolio of the company's work
The "story" took on the format as follows:
Photographer Danny //hire a new photographer named "Danny"
Photographer Leslie
5 a parrot //top priority assignment to take a photo of a parrot
2 a waterfall
4 a mountain
3 Paris
GiveOutAssignments //assign each photographer with 1 assignment, 
4 a pony           //top priority first
GiveOutAssignments
GiveOutAssignments
1 the forest
2 a flower shop
GiveOutAssignments
3 a butterfly
4 a sprout in the rain
GiveOutAssignments //after the last line, review portfolio

Udacity gave some "cheat" code that read in the file, parsed, and provided the following information:
  • For "hire new photographer" lines, the following code is executed:
    • manager.hire(photographer)
    • i.e. the "hire" method is called on an instance of the Manager Class passing the parameter "photographer" of type String
  • For "new assignment" lines, the following code is executed:
    • manager.newAssignment(priority, description)
    • i.e. the "newAssignment" method is called on an instance of the Manager Class passing the parameters "priority" of type int and "description" of type String
  • For "give out assignments" lines, the following code is executed:
    • manager.giveOutAssignments()
    • i.e. the "giveOutAssignments" method is called on an instance of the Manager Class
  • When the end of the "story" file is reached, the following code is executed:
    • manager.reviewPortfolio()
    • i.e. the "reviewPortfolio" method is called on an instance of the Manager Class
Other than the code necessary to parse the file and call methods of an instance of Manager "manager" which was contained within a Class called Simulation which contained the "main" method, the construction of the rest of the solution was left up to the students.

My approach can be summarized as follows:
  1. Determine the tasks involved in the solution
  2. Flesh out the inner-workings of those tasks
    1. Determine other Classes that could assist in the solution
    2. Determine instance variables that would also be necessary
    3. Loosely consider the public methods that would be required to exchange information between classes
  3. Start writing methods for each task
    1. Write accessor methods to other Classes as if those methods already existed
    2. Keep a list of accessor methods required to implement in other Classes.
  4. Debug
  5. Test
In detail, my method went as follows:
1) Determine the tasks involved in the solution:
  • hire photographer
  • create assignments
  • give out assignments
  • display portfolio
Already I had a keen sense that I would require the following helper Classes at minimum:
  • Photographer (would contain the photographer's name, "take pictures")
  • Assignment (would contain the assignment's priority, along with description)
  • Portfolio (would contain the pictures taken by all photographers)
2) Flesh out the inner-workings of those tasks:

Hire photographer:
  • needs to create a new Photographer object (pass String "name" to Photographer's Constructor
  • Manager Class would contain an ArrayList of Photographers, add() Photographer
  • Photographer Class would have a Constructor that passes the "name" to the Photographer object
Create Assignments:
  • needs to create a new Assignment object (pass int "priority" and String "description to Assignment's Constructor)
  • Manager Class would contain an ArrayList of Assignments, add() Assignment
  • Assignment Class would have a Constructor that passes the "priority" and "description" to the Assignment object
  • In order to ease the "pain" of keeping track of where the highest priority Assignments are in the assignments ArrayList, each new Assignment will be added by descending priority
Give out assignments:
  • For each Photographer, give out the next highest priority Assignment (first item in sorted ArrayList)
    • Do this by calling Photographer's "take photo" method
    • Pass photo to Portfolio, along with the photographer's name
Display portfolio:
  • Call the portfolio's draw method
At this point I got a sense that the Portfolio Class would contain an ArrayList of Pictures.  More specifically, I would want those Pictures to have the photographer's name displayed along with the picture.  From this, I gleaned that I would require another Class I would dub FinishedPhoto, which would contain both a Picture and Text object containing the photo and the photographer's name, respectively.

3. Start writing methods for each task:

As already included in the summary, I went about this by first constructing the various Constructors of my Manager and helper Classes, followed by fleshing out the methods of the Manager class.  I kept my perspective completely locked on the Manager Class while writing Manager methods, assuming I had complete access to the other helper classes via methods that I assumed were already fully implemented. 

After completing each Manager method, I would review the code to determine which public methods I needed to write for my various helper methods which I wrote on a piece of paper.  After writing all of those down, I switched my focus over to the helper Classes and wrote the methods that the Manager Class would need for the Manager methods, fleshing out instance variables as needed along the way.

Rinse and repeat until I implemented all of the Manager Class methods, along with the corresponding helper Class methods.  I made some anticipated syntax mistakes that threw the compiler for a loop, however I was very pleased when the test case passed on the first try!  See my completed code here.

No comments:

Post a Comment