Why on Earth is that fun?
It is really a personal opinion at this point because I discovered this myself when I first started applying Object Oriented Programming (I did not learn it when I first began programming because I was teaching myself). Since I did not go with the usual route of learning, and it wasn’t until I am two years into this hobby (seriously). I am a middle school student (at the time of writing) so it is also kind of hard for me to just get right into it without worrying about anything else.
**Disclaimer: I cannot ensure this is going to be fun.
Get Started
Plan the Objects
So let’s began by planning out what are we going to do. In this article I want to design a person’s profile model with Object Oriented Programming, or OOP. So the goal of this project to create a custom object that can help you store information about a person like in a contact book, except in Swift. After you finish this article, you are free to extend on what you already have. This tutorial is only not going to teach you on how to make a full on Personal Registry, but a minimized version so it is easier to explain and to be understood.
What are we going to store?
- Name
- Birthday
- Sex
- Relatives
- Unique Identifier
Additional Information
Relatives and children are arrays that can allow us to store more people as relatives for a person, creating a tree system. By referencing direct people can help generating other cool things like a family ancestry tree and profile auto completion when creating a new person object. We are not going to dive in that deep yet, especially the part about auto completion, but by the end of this tutorial, with some prerequisites with Swift, you can definitely do it yourself.
Storing direct copies of Person
objects as an array in a main Person
object can have its advantages, but it is more optimized and suggested to just use one’s UniqueId
to prevent ambiguity between the copy and the actually object.
Time to Code the Object!
So let’s start by creating a Person
object:
// If you are not familiar with Swift, the Foundation framework is the basic support structure (hence the name Foundation) that allows us to do a lot of things in Swift.
import Foundation
class Person {
// Here we can list out the properties we are going to store about a person.
// We would want some value to be optional just in case when we want to save an incomplete profile.
var name: String?
// I made the birthday value as a string so it is flexible to convert and print.
var birthday: String?
// This is going to be changed since it is not a numerical value.
var sex: String?
// This is going to be in centimetres.
var relatives: [Person:String]?
// Keep in mind that we are changing this too.
var uniId: String
}
Before we get to the initializer part, something must be fixed first. Since we are storing the Person
objects inside a dictionary, they need to be Hashable
.
So change the code so it looks like this:
class Person: Hashable {
// ... the rest of the code remains the same.
// We want to make sure the Person object can be used as a key in a dictionary. The Unique Identifier is used here to help with that.
static func == (lhs: Person, rhs: Person) -> Bool {
// Here we are using the unique identifier to generate hash.
return lhs.uniId == rhs.uniId
}
func hash(into hasher: inout Hasher) {
hasher.combine(self.uniId)
}
}
Now, time for the initializer:
init(name: String?, birthday: String?, sex: String?, relatives: [Person:String]?, uniId: String) {
self.name = name
self.birthday = birthday
self.sex = sex
self.relatives = relatives
self.uniId = uniId
}
What now?
Time to move on to the testing stage to see how this runs:
let John = Person(name: "John", birthday: "03/02/1985", sex: "Female", relatives: nil, uniId: "John3srd")
Hmm, everything looks good so far. There is one small issue though: John does have a kid called James, we can fix that by creating James:
let James = Person(name: "James", birthday: "06/22/2009", sex: "Male", relatives: [John:"Parent"], uniId: "James1st")
Here is the question, we cannot add James to John’s profile yet, because John is already initialized and we did not provide any method to help us to that. Lucky for us it is fairly easy, add the below code to the Person class:
func addRelative(_ person: Person, relation: String) {
self.relatives?[person] = relation
}
Now, we can add James to John’s relatives list:
John.addRelative(James, relation: "child")
Moving On to Even More Programming
There you go, we got the basics down and now you can go ahead and save all of your family members (in a non-creepy way). Bonus! There is one more adjustment that we can make to make your life much easier when storing a large amount of people at once.
let John = Person(name: "John", birthday: "03/02/1985", sex: "Femle", relatives: nil, uniId: "John3srd")
Something is wrong isn’t it, the word female is spelled wrong and it will look very weird if you are trying to print them out. Worry not, there is a way to prevent problems like this one (that does not involve correcting your mistake by hand).
Create a new Enum
called sex
with the following values:
enum sexType {
case female
case male
}
Modify the Person
class with our new tool:
var sex: sexType?
init(name: String?, birthday: String?, sex: sexType?, relatives: [Person:String]?, uniId: String) { //... }
Now we can change both John’s and James’ initialization to this:
let John = Person(name: "John", birthday: "03/02/1985", sex: .female, relatives: nil, uniId: "John3srd")
let James = Person(name: "James", birthday: "06/22/2009", sex: .male, relatives: [John:"Parent"], uniId: "James1st")
Hooray We Made Our Very Own Object!
We did it! Now you can go and show all your friends how creepy you are by divulging your entire database full of their personal information (100% Object Oriented Programming by the way). If you are so completely lost (sorry about that, it’s my first article), you can find the source code here on Github.
If you are here for tutors that can help you through some difficult parts of AP CS, check out my profile!
One Last Challenge
Now do the same thing using C, it is going to be much more complicated. If you think it is too intimidating, try it with Java instead!