CodeGym /Courses /Java Core /Encapsulation exercises

Encapsulation exercises

Java Core
Level 1 , Lesson 8
Available

"Hello, Amigo! Today you will become a distinguished encapsulation expert. Try these exercises:"

3
Task
Java Core, level 1, lesson 8
Locked
Private encapsulated cat
In the lesson on encapsulation, Rishi explained that occasionally several classes will interact with the same object, making the object's data inconsistent. Here we have a cat. We need to make each cat instance retains its integrity, because nobody likes inconsistent cats! Do you know what you need to do?
3
Task
Java Core, level 1, lesson 8
Locked
Our cat is too public!
Foes (with crooked upper limbs) don't waste any time testing the robustness of your code, including your Cat class. We'll protect the cat by hiding some of its internal variables - just the ones that will still be accessible via a method. Cats need some way to interact with the outside world after all.
3
Task
Java Core, level 1, lesson 8
Locked
The unapproachable cat
The cat on our spaceship is very sensitive. He doesn't want anyone prying into his private life. He prefers the darkness of depression over the company of fun-loving programmers. Let's help hide all the Cat class's internal variables as well as any methods capable of changing the internal state of Cat objects.
3
Task
Java Core, level 1, lesson 8
Locked
Like cats and dogs
Cats and dogs aren't the best of friends, but they can get bored without one another. Let's not get in the way of their stormy relationship. We'll leave public the corresponding methods of the Cat and Dog classes. We'll hide the remaining methods as well as the internal variables - from sight and mischievous paws.
3
Task
Java Core, level 1, lesson 8
Locked
Don't forget to encapsulate
Encapsulation is kinda like placing an object in a capsule to protect its integrity. When writing this task, we started creating capsules, or special methods, but we forgot to add the fields that needed encapsulating. Study the code and correct our oversight, so the capsules can fulfill their destiny.
Comments (12)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Hoist Level 36, San Diego, United States
22 August 2023
the joy is in the surprise and struggle -- same brain wake-up techniques in Head First Java book(s) -- which was original Java Ranch. Very clever and smart people at Code Gym. Trust the process .
Hoist Level 36, San Diego, United States
4 January 2023
Kinda foggy on this still --- the Method has the same name as the Class --- ie Cat. The class method is mounting up some properties to give it a shape. Not an instance. If you create an object -- each has its own name field. -- from thomas (Germany) >> The scope of the field is the entire class, or better, you can access this field in all methods. If you access a field in a method, you can prefix it with this fieldName. That way you see at once, that it's a field and it won't give problems, if you have parameters with the same name e.g. in the constructor. Is not a variable -- but a field or attribute of the class Cat.
Максим Василенко Level 44, Kiev, Ukraine
14 November 2022
why all tasks have status closed?? And also they already contain solutions.
John Squirrels Level 41, San Francisco, Poland
18 November 2022
If your task is closed, this means it's been more than 3 days since you solved it.
Slawek Level 16, Wroclaw, Poland
27 February 2021
I think that is the way of code gym to learn and repeat. Sometimes I was confuse and some topics are over leveled. But in that way (like this lesson) I'm not worries that I misunderstand some key topics of Java. Let's see what happen next...
26 August 2020
noice!
Renat Mukhametshin Level 16, Pervouralsk, Russain Federation
29 August 2019
ok it's good
Ewerton Level 30, Belo Horizonte, Brasil
29 June 2019
That lesson about getters and setters kinda explained all this.
Kevin Duc Level 17, Denver, United States
31 August 2019
Seems like the lessons are kinda backwards. I'm using codegym as a refresher and I was using techniques in the syntax lessons that we are now supposedly learning. Not sure how anyone who just started Java would be able to solve some of the earlier problems
Thomas Sixberry Level 16, Rochester Hills, United States
7 February 2020
A lot of griping and complaining along with searching stackoverflow, geeks2geeks, etc. Also, the help section was nice as well.
Sela Level 20, Poland
12 July 2020
i did not come here to start from scratch - I came here for tasks
Brad Reed Level 19, Hartford, United States
12 July 2020
I however did come from scratch little under a month ago, there's a lot of re-enforcement of the idea that this is a community effort it would seem.