CodeGym /Courses /Java Core /Bonus task | Lesson 12 | Level 5

Bonus task | Lesson 12 | Level 5

Java Core
Level 5 , Lesson 12
Available

"Hey, soldier!"

"Good day, Captain!"

"I've got awesome news for you. Here are some exercises to reinforce your skills. Do them every day and your skills will grow at an insane speed. They were created especially for IntelliJ IDEA."

16
Task
Java Core, level 5, lesson 12
Locked
Different methods for different types
So, polymorphism. You haven't forgotten about it, have you? Good. Then let's complete the following task: Read data from the console until the word "exit" is entered. For each value (except "exit"), call the print method. The correct print method depends on the value. For example, if the value contains a period, then use the print method for Double. If the value is a number between 0 and 128, then use the method for short.
4
Task
Java Core, level 5, lesson 12
Locked
Brain training
Find the error in the logic: Duck should correctly implement the CanMove interface. Fix the error so you get the correct result.
8
Task
Java Core, level 5, lesson 12
Locked
OOP: Method overloading
Overload the Tree class's info(Object s) method twice to get 3 methods: info(Object s), info(Number s), and info(String s). Then study the info(Object s) method and create similar functionality in the new methods. For example, the result for the info(Number s) method might be: "Tree No. 123, Number method, parameter Short.
16
Task
Java Core, level 5, lesson 12
Locked
Reinforce the singleton pattern
Let's delve into the singleton pattern by considering the sun and planets of the solar system. First, do an Internet search and find some example of "lazy initialization" using the singleton pattern. Create three singleton classes "in its likeness and image": Sun, Moon and Earth. Then implement the Planet interface in them. And do a couple other operations.
8
Task
Java Core, level 5, lesson 12
Locked
Overloading constructors
In this task, we need to create the Solution class and its descendant SubSolution. We need to create constructors for this descendant and fix their access modifiers so that we get three constructors with different modifiers (except private).
8
Task
Java Core, level 5, lesson 12
Locked
Order of loading variables
Time for a useful debugging task. You have code you need to dig into: figure out what gets initialized and in what order. You can do this using the IntelliJ IDEA debugger and breakpoints. Then fix the initialization order so you get a specific result.
8
Task
Java Core, level 5, lesson 12
Locked
File in a static block
Initialize the variable Statics.FILE_NAME to the full path to a data file that contains several lines. Then, in the static block, read all the lines from the file named Statics.FILE_NAME, and add them separately to the List lines.
8
Task
Java Core, level 5, lesson 12
Locked
Debug, debug, and again debug
Again and again, we search for other people's mistakes and correct them... That's our lot in life as programmers. Our program again displays something wrong: An bug has crept in (just one!). Find it and neutralize it! Use the IntelliJ IDEA debugger to do this.
16
Task
Java Core, level 5, lesson 12
Locked
Request parser
Read a URL from the console. Then display a space-delimited list of all the parameters (The parameters follow the "?" and are separated by "&", e.g. "lvl=15"). The URL contains at least 1 parameter. If the obj parameter is present, pass its value to the relevant alert method.
8
Task
Java Core, level 5, lesson 12
Locked
OOP: The Euro is money too
Correct the Euro class to avoid a StackOverflowError. And, strange as it may seem, the Euro should inherit the Money class, which should implement the getAmount method.

"Those previous exercises were for rookies. I've added some more advanced bonus exercises for the old-timers. Just for veterans."

8
Task
Java Core, level 5, lesson 12
Locked
Mastering the static block
Let's create Plane and Helicopter classes that implement the CanFly interface. Then in the Solution class's static method, we need to read a String from the console. If it's equal to "helicopter" ("plane"), we'll assign a Helicopter (Plane) object to the static CanFly field result. In the static block, initialize the CanFly field result by calling the reset method.
8
Task
Java Core, level 5, lesson 12
Locked
Template pattern
Let's create an algorithm for baristas in the DrinkMaker class. It will choose a suitable cup, add ingredients, and pour liquid. Also, we'll inherit DrinkMaker to create highly specialized algorithms for tea and latte.
8
Task
Java Core, level 5, lesson 12
Locked
Factorial
On Planet Linear Chaos, infants are expected to find the factorial of two-digit numbers. Well, we'll behave like adults, like programmers, and write a method that calculates factorials up to the entered number.
Comments (38)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Hoa Nguyen Level 15, Australia Expert
9 February 2022
I have no clue why we need the modifier "volatile" in private static volatile Moon instance; I also don't understand the use of "synchronized" in the method. Not a good surprise. public static Moon getInstance() { if (instance == null) { synchronized (Moon.class) { if (instance == null) { instance = new Moon(); } } } return instance; }
Jonaskinny Level 25, Redondo Beach, United States
10 March 2022
volatile so it would not be cached (best practice but not key to this task) synchronized so if two threads come in, first thread checks if null, if it is, it synchronizes (at the class level since this is static), allowing the first thread to instantiate before releasing the lock, so the second thread cant instantiate a second instance before the first instance shows as not null on subsequent threads.
DarthGizka Level 24, Wittenberg, Germany
4 June 2021
task1521 (Different methods for different types) caveat: this one really takes the biscuit... Ignore anything that the task descriptions says about what the new info functions should/might print. The output must be *exactly* like that of the existing info function - down to the erroneous blank before the comma - except for the substitution indicated by the formal requirements (i.e. the bulleted list items).
remote87 Level 18, Sofia, Bulgaria
16 February 2021
YEAH!!!! Solved "Different methods for different types" in just 3 hours and just in 9 tries! I consider myself one of the smartest people on the universe!!! P.S. I love this feeling of solving a complex task and all the excitement after that! Please, do excuse me for being so ... ah, you know what I mean, I'm just too happy! :D
remote87 Level 18, Sofia, Bulgaria
16 February 2021
aaaaaand, saw Codegym's solution.... It seams I'm not that smart as I thought of myself pahahahah :D I wonder, will I be able to think like that anytime in the future?!
Maxim Raitin Level 20, Tomsk, Russian Federation
17 May 2021
Man, I read first time your message that you solved all of this tasks in 3 hours.. I was so frustrated..
Oliver Heintz Level 18, Mustang, United States
24 January 2021
I have now officially calculated 150 factorial. Thanks BigInteger. You make life swell.
20 December 2020
for me the factorial was brutal as it started spewing out negative numbers...but lots of fun and lots of learning, awesome.
Andrei Level 41
10 December 2020
Oh man, I spent about 3 days (4 h / day) on "Different methods for different types" and "Request Parser" .... man.. I'm telling you, these must have been the most brutal problems for me.. I just couldn't wrap my head around them, around CodeGym's requirements. If it wouldn't have been for long conversation with Nouser and Misiu in the help section for each problem I don't know if I would have been able to solve them so quickly. But the most important thing, I DID NOT GIVE UP. I just kept on trying, day after day, even if at one point I just dreaded trying to start and solve the problems. So, I believe you can do it, you just gotta keep pushin at it, keep trying. Best of luck and NEVER give up!
whoseunassailable Level 28, India, India
11 March 2022
i kind of felt disappointing after a hour and half. I then looked up to the solution and noticed my solution was exactly the same as codegym's. The only thing i didn't use was the try and catch block which i was gonna use but then decided to solve in some other way. I thought i am dumb enough to solve it.
Anonymous #10782038 Level 24, United States of America
15 April 2024
waste of time to spend that long on one problem. There is no shame in learning from the answer after you spend a decent amount of time on it initially.
Chandan Thapa Level 22, Dubai, United Arab Emirates
8 December 2020
learned a lot from these courses. just a thought -- sometimes if you have the urge to peek over other's solved answers to validate if you are going the right direction with your own code then I think probably you must not do that unless you are really stuck(check multiple answers in this case). Just do the way you are doing without looking until you reach the edge and ofcourse google search is very useful. Guilty is charged -- I was the victim of the above in 2 of the tasks above, just to find out that i was going the right way, i would have felt much better if i had solved those two without wandering around. Enjoyed it a lot!! :)
Lawson Level 29, Lagos, Nigeria
29 August 2020
Apart frm me , who else also copied and paste some of the answers? let me know whether to stop learning at this point
Artur Level 22, Moorends, United Kingdom
14 November 2020
i did some i admit but can't really choose if did so because of lazyness or because was too tired of trying... i do regret tho i might have omitted some parts that could teach me something important..
Clint Level 22, cincinnati, united states
27 November 2020
most definitely did
Agent Smith Level 38
29 August 2020
Had a lot of fun with Request parser task. It is quite convenient to store parameters as key-value pairs. But if you do it the same way, make sure to use LinkenHashMap, because order matters in this task.
Jonaskinny Level 25, Redondo Beach, United States
10 March 2022
You can also just build it in-line while processing, using only what is imported. I have been keeping solutions to only use what is imported (and java.lang default import of course) and order takes care of itself.
Max Khelm Level 22
6 May 2020
Completed Request Parser in 20 lines with just indexOf(), split() and substring(). Whoa! Awesome tasks and awesome course. Thx CodeGym!