Programming for beginners
Programming for Cats — Part #2
You don’t have to be an engineer or person who knows math and physics to write code and make programs. It ain’t so difficult as you think!
Welcome to the next lesson of this series ‘Programming for Cats’, which was created for teaching everybody and showing an understandable part of programming life and how to understand it using real examples, even if you are a kid. You can check out the previous lesson if you didn’t read it before.
Here we’ll look at something more interesting and more important in programming life. Objects and classes. Objects and classes are part of the programming paradigm of Object-Oriented Programming. It’s an approach when all variables and types in your program describe real objects. For example, object Car, object House, object Student, etc.
Like in real life, any object has its own characteristics. For example, if we are looking for a new smartphone on a website. We open a filter page and select the next options: screen size — 6.1 inch, memory — 128 GB, ram — 6 GB, etc.
So, we understood what is the object and its characteristics. Now we can create some of them using code! Let’s look at some simple examples.
We’ll create the simplest class that describes a Cat and we’ll use the same name for it. Highlight the next main parameters for our cat. Name, color, sex, age, weight.
In Kotlin language our Cat looks like this:

And created cat objects:

Here we got 3 cat objects: cat1, cat2, and cat3. With names Tomas, Grace, and Nelly. And with their descriptions. From the previous lesson, we have learned about data types. Here we used two data types: String and Int. String — is a type for text values. And Int is for numbers. In class Cat, we used String type for name, color, and sex fields. And Int for age and weight.
But wait!
If we use a type String for parameters name, color, and sex. Can we use the wrong data for them? Let’s check it out.

It works with wrong values. We put ‘Burger’ for the color parameter and wrong values for the sex parameter. How can we solve this problem?
First of all, we should limit available values for our parameters. For the sex parameter, we can use only ‘Male’ and ‘Female’. And for the color parameter, we can use ‘Gray’, ‘Black’, and ‘White’.
To accomplish this, we’ll use enum class. They look like this.

Enum classes describe a constant list of values for some exact parameters. So, in the final class Cat looks like this.

We changed types for sex and color for fields for our new types — Sex and Color. Now, our cat objects look like this.

Done! For fields color and sex we can use only defined values from our enum classes and only them!
And that’s it! If you have questions or wanna learn more, feel free to leave a comment.
I hope you enjoyed this article. In the next part of this series, where we’ll look at something more interesting in the programming world!
