avatarLeonid Hass

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2230

Abstract

Student, etc.</p><p id="6e73">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.</p><p id="356b">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.</p><p id="b405">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.</p><p id="a0c2">In Kotlin language our Cat looks like this:</p><figure id="d65e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*kcnkp_0mAcc6lIP0otNEXg.png"><figcaption>Screenshot of author code — was created in IntelliJ Idea software, without any copywriting</figcaption></figure><p id="4500">And created cat objects:</p><figure id="26ce"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*tHwuXcJMofBnSwGq6hT8yg.png"><figcaption>Screenshot of author code — was created in IntelliJ Idea software, without any copywriting</figcaption></figure><p id="deb9">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.</p><h1 id="2072">But wait!</h1><figure id="3aec"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*XkNFXpJQgBIDfwOh"><figcaption>Photo by <a href="https://unsplash.com/@nci?utm_source=medium&amp;utm_medium=referral">National Cancer Institute</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p id="d36f">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.</p><figure id="b771"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Rvcf6V-ix_2kyhxbvPfiOw.png"><figcaption>Screenshot of author code — was c

Options

reated in IntelliJ Idea software, without any copywriting</figcaption></figure><p id="b465">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?</p><p id="4524">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’.</p><p id="eb62">To accomplish this, we’ll use enum class. They look like this.</p><figure id="9151"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*SAk9kBxiNPCDjkMOkg-C7g.png"><figcaption>Screenshot of author code — was created in IntelliJ Idea software, without any copywriting</figcaption></figure><p id="e240">Enum classes describe a constant list of values for some exact parameters. So, in the final class Cat looks like this.</p><figure id="baa2"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*sp19SoiGZz81yRTTIWM8yg.png"><figcaption>Screenshot of author code — was created in IntelliJ Idea software, without any copywriting</figcaption></figure><p id="6ddb">We changed types for sex and color for fields for our new types — Sex and Color. Now, our cat objects look like this.</p><figure id="46d6"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*jQ9VS7VXzm4qgqAj8CBzbQ.png"><figcaption>Screenshot of author code — was created in IntelliJ Idea software, without any copywriting</figcaption></figure><p id="0d14">Done! For fields color and sex we can use only defined values from our enum classes and only them!</p><figure id="ec37"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*aIlDvy2kMmlW85cg"><figcaption>Photo by <a href="https://unsplash.com/@rayhennessy?utm_source=medium&amp;utm_medium=referral">Ray Hennessy</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p id="2a1c">And that’s it! If you have questions or wanna learn more, feel free to leave a comment.</p><p id="1f39">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!</p></article></body>

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!

Photo by Sereja Ris on Unsplash

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.

Photo by Tyler Nix on Unsplash

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:

Screenshot of author code — was created in IntelliJ Idea software, without any copywriting

And created cat objects:

Screenshot of author code — was created in IntelliJ Idea software, without any copywriting

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!

Photo by National Cancer Institute on Unsplash

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.

Screenshot of author code — was created in IntelliJ Idea software, without any copywriting

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.

Screenshot of author code — was created in IntelliJ Idea software, without any copywriting

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

Screenshot of author code — was created in IntelliJ Idea software, without any copywriting

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

Screenshot of author code — was created in IntelliJ Idea software, without any copywriting

Done! For fields color and sex we can use only defined values from our enum classes and only them!

Photo by Ray Hennessy on Unsplash

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!

Programming
Programming Languages
Cats
Technology
It
Recommended from ReadMedium