-
Notifications
You must be signed in to change notification settings - Fork 34
Create proposal.md (Coding for (Not Quite) Dummies: Object Oriented Programming) #255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
michaelloughnane
wants to merge
8
commits into
master
Choose a base branch
from
#CodingFor(NotQuite)Dummies-ObjectOrientedProgramming
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6a4e122
Create proposal.md
michaelloughnane 42a4e1f
Sample Code for Presentation
michaelloughnane 17dc4e2
Create blog.md
michaelloughnane 7759601
Update blog.md
michaelloughnane 1927ccb
Update blog.md
michaelloughnane 00ba70b
Update blog.md
michaelloughnane 1de64e7
Update Person.java
michaelloughnane b59566a
first edits
alliebailey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| *adapted from the google doc I wrote this on. The formatting might be slightly off.* | ||
|
|
||
| # Coding for (Not Quite) Dummies: Object Oriented Programming | ||
|
|
||
| ## Part 1: Introduction - Coding is Scary | ||
|
|
||
| In 1969, Martin M. Broadwell made one of the first references to a psychological learning model known as the four stages of competence. They comprise of: | ||
|
|
||
| 1. unconscious incompetence, | ||
|
|
||
| 2. conscious incompetence, | ||
|
|
||
| 3. conscious competence, and | ||
|
|
||
| 4. unconscious competence. | ||
|
|
||
|
|
||
| When it comes to programming, there are many, many different websites and articles aiming to quickly teach a person the basics, and try and convince them to take the plunge and commit to reaching those third and fourth stages. | ||
|
|
||
| If you’re here, you’ve been convinced too. | ||
|
|
||
| To progress in any significant manner beyond the second stage of competence requires a devotion to the task. Lesson writers know this, and as such don’t bother too much with trivial things like making higher level tutorials approachable - *why bother? At this point surely any prospective student knows what they’re getting into.* | ||
|
|
||
| That attitude is a convenient workload off the writer of the lesson, but not very helpful for newer students wanting to dip their toes into bigger concepts, who instead find themselves thinking they’re better off abandoning the endeavor altogether. | ||
|
|
||
| *An excerpt from Oracle’s official Java tutorials - This doesn’t scream “inviting,” now does it?* | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| This guide hopes to be different. If you’re reading this, you’re on your journey from incompetence to competence, and to this end hopefully have a working knowledge of the coding fundamentals - variables, loops, basic syntax, etc. (if not, a website like [Codecademy](https://www.codecademy.com/learn/learn-java) will teach those basics within its first few lessons). This blog will discuss higher level concepts and techniques, and hopefully convince you that the past few hours you spent on Codecademy or another comparable website wasn’t a waste of time. Even making it to stage three in this context - competence with coding - is no simple task, and one that we all are taking steps to achieve, but with any luck this lesson can help guide you down that path. | ||
|
|
||
|
|
||
|
|
||
| ## Part 2: What is an Object? | ||
|
|
||
| **Object Oriented Programming** is a name that we give to certain coding languages because they make use of a type of code we call “objects.” Many major languages are Object Oriented, including (but not limited to) Python, C++, C#, Java, JavaScript, and many more. | ||
|
|
||
| *Quick note: For examples and sample code, I’ll be writing in Java because it’s my primary language. Object Oriented techniques and features hold across all these languages unless noted otherwise, but specific implementations may differ if you aren’t using Java.* | ||
|
|
||
| An object is obviously not a physical object, but a special variable type. It can do more than just store a number, though. Objects are variable types that we make ourselves, and both store data and utilize functions specific to the object. What do I mean by this? Here’s an example. Say we want to store the data of a bunch of people for a hypothetical classroom roster or something similar. We'll want to store their first name, last name, and age, we'll want to be able to access any of these pieces of data at any time, and we'll want to be able to change their age after the fact, seeing as people's age changes. | ||
|
|
||
| *Remember: This is Java-specific implementation may vary if you’re using a different language.* | ||
|
|
||
| To start, let’s make a public class called “Person” (since that’s the type of object we’re trying to make - people). We’ll also make some variables to store names and age, though we won’t assign any values to them, seeing as how this isn’t any one specific person we’re making right now (we’re making a general template that we’ll be able to use to create specific “person” objects with later, each with their own unique names and ages). | ||
|
|
||
|  | ||
|
|
||
| To clarify, we’re making this object in its own file. To actually create a “person” object, we’ll need to put the “Person.java” file in the same directory as the program that is using the object, then create an object in that main program. How do we actually create the object, though? That uses something we call a **constructor**. A constructor is a bit of code in the object class (the Person.java file) that lets you determine how exactly a Person object is created in your code. Here are two that I wrote: | ||
|
|
||
|  | ||
|
|
||
| So, what’s going on here? | ||
|
|
||
| Whenever we make an object, we’re making a unique Person object with its own firstName, lastName, and (optionally) age values. These lines of code mean that, when we want to make a Person in our code, we need to give it a first name, a last name, and, if we want, an age - like this: | ||
|
|
||
|
|
||
|
|
||
|  | ||
|
|
||
|
|
||
|
|
||
| Here’s an object in action. This line of code creates a new Person object, and runs the code inside the associated constructor for that object. In this case, we’re making a Person object named “John” with a first name input of “John,” a last name input of “Smith,” and an age input of 26. If we didn't know John's age, we could simply input "John" and "Smith" and the object would be created with the other constructor, since that one only needs a first and last name. In that case, the "age" value would be set to -1, which I'm using to indicate an unknown age. | ||
|
|
||
| *Remember that this is in a completely separate file. We’re just storing Person.java and PersonTest.java in the same directory.* | ||
|
|
||
| With that, we’re a step closer to coding everything we want this Person object to do, but we aren't finished. It can store that info, sure, but we still can't access those variables from outside the class*. We’ll have to write that in ourselves, via a method. | ||
|
|
||
| **There actually is indeed a trick for this, but it’s bad form. When we initially created those variables, we made them private. If they were instead public, we could then just access, and edit, them from anywhere. But this typically isn’t what we want, since granting full access to those variables can have unintended consequences. If you’re coding Person.java, do you want the people using it to be able to modify firstName or lastName freely? Probably not.* | ||
|
|
||
| You most likely already know what a method is, but just in case, a quick reminder: a **method** is just a fancy name for a function. You (optionally) input a value, it does something, and (optionally) outputs a value (the only required part of those three is that it does something). If we want methods to access the variables, we just need to write some public methods in Person.java that will return the desired values: | ||
|
|
||
|  | ||
|
|
||
| **public** here means that it can be accessed from outside Person.java. It would be a pretty unhelpful retrieval method if we couldn’t actually call it from outside the file, after all. | ||
|
|
||
| *Compare to the private age, firstName, and lastName variables - since they’re private,* *we can’t just print John.firstName in PersonTest.java* | ||
|
|
||
| The int/String written before the name indicates what variable type is being returned by the method. | ||
| You’ll notice I added a getName() method that returns the whole name, since I thought that could be useful. Here’s me using these methods in PersonTest.java: | ||
|
|
||
|  | ||
|
|
||
|
|
||
|
|
||
| Using the method of an object is as simple as [specific object instance].[method name]. Here’s what the above code outputs: | ||
|
|
||
|  | ||
|
|
||
|
|
||
|
|
||
| One last task for this is to modify the age of the person. This one will use methods too, but it raises an important question: How exactly do we want to approach this? Do we want to give free access to increment the age, or do we want to simply increment it by one? An easy answer is to do both! | ||
|
|
||
| #### Sublesson: Overloading | ||
|
|
||
| *Note: This does not apply to every language! I know it works for C++ and Java, for example, but not Python. Do a quick google search to see if this will work for you. If not, skip this.* | ||
|
|
||
| In some languages, two methods can be given the same name, if they take different inputs — as seen in the code example you’ll see in a second. This is called **overloading**. | ||
|
|
||
| I’ve gone ahead and made a function that sets the age to a new value, takes an input and adjusts the age accordingly, and another that takes no input and simply increments the age by one. With that, we have our complete (simple) Person.java file: | ||
|
|
||
|
|
||
|
|
||
|  | ||
|
|
||
|
|
||
|
|
||
| Our complete test program: | ||
|
|
||
michaelloughnane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|  | ||
|
|
||
| And finally, its complete output: | ||
|
|
||
|  | ||
|
|
||
|
|
||
|
|
||
| ## Part 3: Inheritance, or How to Make Your Code Look Cleaner | ||
|
|
||
| Inheritance is another important aspect of objects, and it can be a massive time saver when it comes to writing objects. Essentially, inheritance is implementing an object within another object. | ||
|
|
||
| To put it more simply, **inheritance** lets an object *inherit* the properties of another object. For example, say we've already coded a hypothetical “Wolf” object and a hypothetical “Animal” object. With this line of code in the Wolf object's header, | ||
|
|
||
|  | ||
|
|
||
| Wolf will now have all the characteristics (methods, variables, etc.) of Animal, in addition to whatever code is written in Wolf. Note that in Java, a given object (a **subclass**) can only inherit from one parent class (a **superclass**). This varies from language to language. For example, C# also only supports single inheritance, whereas C++ supports multiple inheritance (that is to say, a single subclass can inherit from multiple superclasses). | ||
|
|
||
| Another important item that's similar to a superclass is an interface. An **interface** is basically just a list of method titles that an object will **implement**, i.e. you'll code those methods from the interface into the object. | ||
|
|
||
| Here’s an example. Say we have this interface: | ||
|
|
||
|  | ||
|
|
||
| To our previous Wolf example, we could now write: | ||
|
|
||
|  | ||
|
|
||
|
|
||
|
|
||
| Now that we’ve implemented AlphaWolf, our code will not successfully compile unless we include: | ||
|
|
||
| * A "howl" method that takes no input and returns no output, | ||
| * a "fight" method that takes a string input and outputs a boolean (if the wolf won, maybe), and | ||
| * a "run" method that takes an input of the distance ran and returns no output. | ||
|
|
||
| *Note that the input and outputs of the method are relevant. You can’t just include methods with the same names.* | ||
|
|
||
| At this point, you might be wondering what the benefit of an interface is at all, seeing as how it doesn’t seem to do anything other than restrict the code you can write. Consider, though, in a group setting, with multiple people working on a project, it may be beneficial to outline to your teammates what exactly you want in the object. You can look at an interface and know exactly what methods you have to work with, without having to search through the entire code of the object (assuming you haven’t slacked off and put an empty method in just to satisfy the requirement!). | ||
|
|
||
|
|
||
|
|
||
| ## Part 4: The End of the Beginning | ||
|
|
||
| This is not all there is to Object Oriented Programming. It’s not even all there is to Objects. It is, however, a good introduction to the topic. With any luck, this guide gave you an understanding of objects. However, it’s not possible to cover everything, so eventually you’ll have to venture into the world of advanced computer science. Don't worry, it's not as threatening as it sounds. | ||
|
|
||
| The four stages of competence are something of a simplification, in my opinion. It’s not easy to draw a line defining where “conscious competence” stops and “unconscious competence” starts. However, the model does do an important thing of drawing a distinction between not knowing something, and not knowing but wanting to learn. Once you’ve reached that threshold, you’ve already taken the biggest step. The only thing to do from there is to not get dissuaded, and stick the landing as you continue your education. Once you’ve gotten to the point where nothing can dissuade you, then I think you can say you’ve finally reached that next stage. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # TEMPLATE | ||
|
|
||
| ## :fire: Do not edit this file - copy the template and create your own file. | ||
|
|
||
| **[Step-By-Step Technical Blog Guide](https://hq.bitproject.org/how-to-write-a-technical-blog/)** | ||
|
|
||
| ### :pushpin: Step 1 | ||
| **TITLE:** | ||
| Coding for (Not Quite) Dummies: Object Oriented Design | ||
|
|
||
| **TOPIC:** | ||
| Object Oriented Design | ||
|
|
||
| **DESCRIPTION (5-7+ sentences):** | ||
| This presentation is intended to help prospective students learn higher-level concepts related to Computer Science, and bridge the gap from "Khan Academy" to "Computer Scientist." Specifically, this presentation will focus on Object Oriented Design and teaching students the basic concepts and implementations of it in Java. They will learn about constructors, fields and methods, and inheritance, among other things. | ||
|
|
||
| ### :pushpin: Step 2 | ||
| :family: **TARGET AUDIENCE (3-5+ sentences):** | ||
| The target audience is students who know the basics of Java but not much beyond that. The course will assume the learners are familiar with syntax and basic functions (there are a variety of resources available for people to learn that) and build upon that knowledge. | ||
|
|
||
| ### :pushpin: Step 3 | ||
| > Outline your learning/teaching structure: | ||
| **Beginning (2-3+ sentences):** | ||
michaelloughnane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| A basic overview of the goals of this presentation, and the expected prerequisites. Also, a high level introduction to object oriented programming (no code! This isn't mean to scare them off!) | ||
|
|
||
| **Middle (2-3+ sentences):** | ||
| The meat and potatoes of the presentation. Showing the basic features of the concept, alongside sample code to demonstrate those features in action. Also, will have a project for learners to code on their own to ensure comprehension - the sample code given will be helpful to this end, but it won't just be a case of "copy down the code and you're done" | ||
|
|
||
| **End (2-3+ sentences):** | ||
| Give the solution to the sample code made in the prior section, alongside expanations in case any confusion arises. (The beginning bits of the code may be given earlier to make sure nobody's completely lost). Discuss where to go after this, and potentially lead into another lesson. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| public class Person | ||
| { | ||
| //Coding this will be an exercise for the students participating in the lesson; incomplete code fragments will be used to initially demonstrate the concepts. | ||
| //As this is a simple program, there isn't any comments within the body - the method and variable names are intended to be self-explanatory. | ||
|
|
||
| private String firstName; | ||
| private String lastName; | ||
| private int age; | ||
|
|
||
| public Person(String theirFirstName, String theirLastName){ | ||
| firstName = theirFirstName; | ||
| lastName = theirLastName; | ||
| age = -1; | ||
| } | ||
|
|
||
| public Person(String theirFirstName, String theirLastName, int theirAge){ | ||
| firstName = theirFirstName; | ||
| lastName = theirLastName; | ||
| age = theirAge; | ||
michaelloughnane marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| if(age < 0) | ||
| age = -1; | ||
| } | ||
|
|
||
| public int getAge(){ | ||
| return age; | ||
| } | ||
|
|
||
| public String getFirstName(){ | ||
| return firstName; | ||
| } | ||
|
|
||
| public String getLastName(){ | ||
| return lastName; | ||
| } | ||
|
|
||
| public String getName(){ | ||
| String fullName = firstName + " " + lastName; | ||
| return fullName; | ||
| } | ||
|
|
||
| public void setAge(int theirAge){ | ||
| if(theirAge >= 0) | ||
| age = theirAge; | ||
| } | ||
|
|
||
| public void increaseAge(){ | ||
| if(age > -1) | ||
| age++; | ||
| } | ||
|
|
||
| public void increaseAge(int ageIncrement){ | ||
| if(age > -1) | ||
| age += ageIncrement; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
|
|
||
| public class PersonTest | ||
| { | ||
| //A simple test file for Person.java. | ||
|
|
||
| public static void main(String[] args) | ||
| { | ||
| Person John = new Person("John", "Smith", 26); | ||
| System.out.println(John.getFirstName()); | ||
| System.out.println(John.getLastName()); | ||
| System.out.println(John.getName()); | ||
| System.out.println(John.getAge()); | ||
|
|
||
| John.increaseAge(); | ||
| System.out.println(John.getAge()); | ||
|
|
||
| John.increaseAge(3); | ||
| System.out.println(John.getAge()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of using an image of code, could you just write it in github?