Level 41

Old Level 04

Published in the Java Developer group

I am the best

1 The best is the enemy of the good

Old Level 04 - 1Being best means being better than the others, exceed them and be different. You cannot become the best doing what everyone else does. You need your own way. You cannot be best in everything: while you study everything, someone specializes on one thing. The one way to become the best is choosing a one very certain specialization and become a world-class specialist in it. If you dance ballet since you are five and work 8 hours a day there will always be someone who does it since three and 10 hours a day. By the time you are both fifteen his experience will exceed yours by 5000 hours. Plus, there are geniuses, every hour of whose work equals to your three. And there are the best tutors in the world, and you are, for example, a self-taught person. The only way to become the best without having you own way is working more than everyone else, be talented, have good teachers and rich parents. But that already isn’t “like everyone”, is it? Yet even the fastest and the most hard-working horse cannot outrun a car. You need your own strategy, your unique plan to become the best without sacrificing everything.

2 It’s a hard work becoming the best

There will always be someone who began earlier. Someone had rich parents, someone studied in the world’s best university. Someone got a job with his parents help. Don’t fret. It happens. It’s called different starting conditions. Yet people like these are minority, the world is filled with people who succeeded due to “thinking outside the box”, hardworking and the desire to learn all the time. Life is like playing card game. Everyone can win having all trumps on hand, but professional wins no matter what cards he has. He diminishes the trumps’ influence with his skill. No one comprehends it as fully as professional sportsmen do. Many of them only have a few years to grasp their chance and become successful.

3 There is always someone who works more then you

Old Level 04 - 2Such people are numerous. There are workaholics, perfectionists and people who simply love what they do. True, many of them sacrifice family, friends, and are eager to work 80 hours a week. Work is their life. That is not the way for you. But those people are still perfectly capable of pushing you down the career ladder. You are not able to spend 6 month on business trips in order to get promotion, but they are. An average Chinese student is more laborious then European one, and an employee from China is eager to do your job for quarter the price. Working a lot is not a key to success, but working little is the key to failure.

4 Unfriendly environment

Old Level 04 - 3There are so little places in the world where working hard and honest is encouraged. If you study a lot in college, spend all of your free time in library and pass exams on your own, you’ll be considered a nerd. And if you “had fun all the semester and passed” or, in other words, “beat the system”, well done! It’s hard to strive for success when society hates rich and successful people. Hates and envies them. Poor people, having greedily fallen upon money, start boasting it. It’s true that rich people behave differently: Bill Gates can wear a 10 bucks shirt, ‘cause with it or without it he is still Bill Gates.

5 To sum up

Still businessmen are the ones creating workspaces. Hired workers’ salary is the result of business competition for best workers. The more businesses there are in the country, the higher the salaries are. There is a huge rivalry on “work more” path, this way no longer works. You need to find another. You need to love your life and your time. Money is just the instrument which makes you independent financially. If you are financially independent, you may do what you want and not do what you don’t want. Be careful with “sacrifices” on the road to becoming best. Do not sacrifice the most important things: family, friends, health, job you love. Being financially successful in 50 without having family, friends, health and hating your job is no success. It’s a failure.

Level 4

Old Level 04 - 4

1 Risha, Scope of variables

- The professor still stands pat. Those old lecturing wrinkles are all the same. All he tells you is written in books. Bear in mind that no one has ever learned to swim after a dozen of lectures. Lectures help when you definitely understand a subject and know it a little less than a professor himself. - Professor’s lectures are of avail indeed. - Yeah... or rather, we hope they are. The more points of view on the same stuff you hear, the more you wonder how things really are. With just one point of view, you either believe it or not. Okay, let’s get down to business. - Take a look at the picture I gave you before: Old Level 04 - 51 A variable declared in a method exists / is visible from the start of declaration to the end of the method. 2 A variable declared in a code block exists to the end of this code block. 3 Variables - method arguments - exist until the end of the method quit. 4 Class/objects variables exist during the entire lifetime of their object. Access modifiers determine their visibility. 5 Static class variables exist at all time of the program runtime. Their visibility is also determined by the access modifiers. - Yeah, I remember this picture. - That’s fine. Let me remind you about some points. - All the variables declared inside methods exist / can be accessed (seen) from the line of declaration to the last method line (example: 1). - If a variable is determined/declared in some code block, it exists until the end of this code block (example: 2). - If a variable is a function argument, then it exists / can be seen accessed (seen) from the first line of method to the last (example: 3). - If a variable is a class variable (example: 4), then it is bound to a specific object, and exists all the time until there is an object of this class. If there is no object, then there is no variable. The variable is accessible (variable is visible) to all methods of the class. It doesn’t matter whether methods are declared before it or after it. For each object of class its own variable is created. This variable is independent of other objects. Static methods have no access to the variable. - If a variable is declared static (labeled by keyword “static”), it exists all the time its class exists. Usually, the JVM loads the class into memory at its first use, at the same time static variables are initialized. Old Level 04 - 6- In the example above we’ve declared class Cat, which has 4 variables: a,b,s are regular, and count is a static one. If you create several objects of this class (3 for example), each of them will have its own copy of regular class variables. But all these objects share the static variable. Actually, this static variable is not inside any objects, because it exists even when there is no object of Cat class. - That’s what happens if we declare s as static variable: Old Level 04 - 7- Yeah. I kind of got it. - Can I declare identical variables? - Within a method, you can’t. All the variables declared within a method must have unique names. Method arguments are also considered as its variables. - How about class variables? - Class variables must also have unique names within each particular class. However, there is an exception: the name of method variable and the name of class variable can match. - What if we declared two variables “count”, one in class and one in method, and then changed count variable? Which of them will change? - If several variables are visible (available) within the method body, for example, a class variable and a method variable, then the method variable will be accessed. Old Level 04 - 8- In this code two count variables are declared: in line 4 a class variable and in line 9 a method variable. - That’s what happens when a run method is executed: - A class variable is accessed in line 8. Value 15 is assessed to it. - In line 9, a new method variable (count) is declared (created). It covers the class variable. Any further code in the method will access exactly the method variable. - I get it. - The method variable covers the class variable. That is, the method variable will be accessed. However, the class variable can also be accessed, only in a more complex manner. Old Level 04 - 9- You’ve mentioned static methods in the beginning of the lecture. What are these static methods? - Static methods and variables are bound not to class objects, but to a class itself. So, if we create 10 objects of the class Variables from the example at the beginning of the level, we’ll have 10 classVariable variables, one for each object, and just one common variable TEXT. - I’ve got a question. - What’s the difference between static and non-static methods? - Let’s look at how an ordinary non-static method works: Old Level 04 - 10- When you call a method of the form «object» point «method name», you actually call a class method, to which the very same object is hiddenly passed with the first argument. Inside the method this object is given a name this. All the things are done precisely with this object and its data. - Jeez! So that’s how it all works! - And that’s how a static method works: Old Level 04 - 11 - No object is passed when you call a static method. I mean, this equals null, so a static method doesn’t have access to non-static variables and methods (it has nothing to pass to such methods as this). - Umph. I think I understand. But still, not everything is clear. - Here comes unc Diego…

2 Diego, Tasks for visibility of variables

- Hey, Amigo. - Hey, Diego. - I brought you a couple of tasks for visibility of variables Old Level 04 - 12

3 Risha, A command and a command block

- Now I’ll tell you what the command and command block are. It’s quite simple. The method body consists of commands. Each command ends with a semicolon. Old Level 04 - 13- A command block contains several commands joined together by curly braces. The method body is also a block of commands. Old Level 04 - 14- Here is the rule that is good for every situation: where you can write a single command, you can write a command block as well. We’ll see that in the examples for the tasks below.

4 Elly, Conditional operator

- Hey, Amigo. Today I’ll explain you conditional operators. - The value of a program lies in its ability to act differently in different situations, otherwise it's useless. In Java, the mentioned ability is realized by means of a «conditional operator». It is a special keyword that allows you to execute different command blocks depending on the trueness of a condition. - Conditional operator consists of three parts: «condition», «command 1» and «command 2». If the condition is true, then «command 1» is executed, otherwise «command 2» is executed. Commands are never executed at the same time. This operator looks as follows: Old Level 04 - 15- It’s exciting! I think programming is a lot more interesting with an operator like this. - Yeah. Here are some examples: Old Level 04 - 16

5 Bilaabo, Comparing with Pascal

- Hey, Amigo. Do you remember, we use more advanced Pascal on our planet. That’s what it all would look like in Pascal. Old Level 04 - 17

6 Diego, Tasks

- I’d like to tell you a little bit about the comparison of variables in Java. - You already know about the simplest comparison operators less-than (<) and greater-than (>). - Yeah. - There are also an "equal-to" (==) and "not-equal-to" (!=) operators. And there are "less-than-or-equal-to" (<=) and "greater-than-or-equal-to" (>=) operators as well. - Oh, that’s more exciting thing. - Note that there are no operators «=>» and «=<» in Java! - The sign «=» is used for the assignment operator, so we have to use double equals signs «==» to indicate equality. To check that the variables are not equal, use «!=». - Fair enough. - A comparison of two variables in Java using an operator «==» is a comparison of what these variables contain. - That is, values are compared in primitive type variables. - In reference type variables references are compared. So, if the objects are identical inside, but their references are different, the comparison shows that they are not equal: the result of the comparison is false. The reference comparison result is true, only if both references point to the same object. - A special method equals is used to compare objects by their content. The compiler adds this method (and all the methods of the Object class) to your class, even if you don’t declare it. Let me explain it through examples: Old Level 04 - 18- By the way, here are a few tasks before I forget:
Practical tasks
1 Minimum of two numbers
Write a program that reads two numbers from keyboard and displays to the screen the minimum of these numbers.
2 Maximum of four numbers
Write a program that reads four numbers from keyboard and displays to the screen the maximum of these numbers.
3 Sort three numbers
Write a program that reads three numbers from keyboard and displays them in descending order.
4 Compare names
Write a program that reads two names from keyboard, and if the names are the same, displays «Names are identical».
Display «Name lengths are equal» if the names are different, but their lengths are equal.
5 18+
Write a program that reads a name and age from keyboard. If the age is less than 18 display «Grow up a little»
6 18 is enough
Write a program that reads a name and age from keyboard.
If the age is more than 20 display «18 is enough»

7 Kim talks about a boolean type

- Hey, Amigo. I want to tell you about a new data type. It’s a boolean type. Variables of this type сan take just two values: true and false. - How to use it? - This type is hiddenly used in many places. Just like a number is the result of any addition, a boolean type – true or false – is the result of any comparison. Examples: Old Level 04 - 19- Another examples: Old Level 04 - 20- How can I write such expression: 0<a<b? - In Java there are no expressions that include three operators, so you may use this structure: (0<a) AND (a<b) - That’s what should I write? AND? - Do not rush, I’ll explain it to you. - In Java, there are three logical operators: AND, OR and NOT. You can use them to build conditions of varying complexity. These operators can only be applied to a boolean expression. So you may not write (a+1) AND (3), but you may write (a>1)AND (a<3). - NOT is an unary operator. It applies only to the expression located to the right. It looks more like a minus before a negative number, than a multiplication sign. - Using boolean (logical type) variables you can perform various operations. - What kind of operations? - Take a look at them right now: Old Level 04 - 21- How about more examples? - Sure: Old Level 04 - 22

8 Elly, While Loop

- Hi. - Hi, Elly! - It’s… time to learn about loops! They are just as simple as conditions, but more interesting. A loop enables any command or command block to be executed several times. A loop looks as follows: Old Level 04 - 23- It’s that simple. A command or command block is executed repeatedly until the loop condition is true. First the condition is checked, then the loop body (a command block) is executed. Then again, the condition is checked and the loop body is executed. And so on until the condition becomes false. - And what if it’s always true or always false? - If it’s always true, the program will never stop and will always execute a loop. If it’s always false, the loop body is never executed. - Examples: Old Level 04 - 24- Comparing to conditions it’s not that hard. I wanna try it now.

9 Bilaabo, Comparing with Pascal

- Hi, bud. Bilaabo will now tell you what it all would look like in Pascal. Old Level 04 - 25- Well, the comparison with Pascal is useful only for those who know it. - Don’t you know? It’s my favorite language! - All right. I believe it’s an awesome language. And if I already knew it, it would have helped me.

10 Diego, Loop tasks

- Hey, Amigo! - I heard you’ve learned loops. What if I give you some more tasks?
Loop tasks
1 10 numbers
Write a program that displays numbers from 1 to 10. Use “while” loop.
2 10 numbers in reverse order
Write a program that displays numbers from 10 to 1. Use “while” loop.
3 You can never have too much of a good thing
Write a program that reads from keyboard a string and number N.
Program should display to the screen the string N times. Use “while” loop.

Example input:
Example output:
4 S-square
Write a program that displays to the screen a square 10x10 of “S” characters. Use “while” loop.
Don’t separate characters in the same line.
5 Multiplication table
Write a program that displays multiplication table of 10 by 10. Use “while” loop.
Separate numbers by a space.

Example output:
1  2  3   4   5   6   7   8   9 10
2  4  6   8 10 12 14 16 18 20
3  6  9 12 15 18 21 24 27 30

11 Elly, For Loop

- I want to tell you about one more loop. It is called for. This loop is another record of the while loop. It’s just more compact and handy for programmers. Examples: Old Level 04 - 26- Oh. - These loops are equivalent. Whereas while contains one condition in brackets, for has three conditions. When program is compiling for loop transforms into while loop. - The first expression in for loop (highlighted in green) is executed once before the loop. - The second expression is executed every time before the execution of the loop body. It is similar to the while loop condition. - The third one is executed every time after the execution of the loop body. - And why do we need one more loop? The while is completely clear. - This is done for convenience of programmers. Loops very often occur in programming. It’s convenient when one line contains information about the startup value of the variable, its modifier condition and the command that changes the variable.

12 Bilaabo, Comparing with Pascal

- Amigo, believe it or not, but in Pascal there is loop For, too. It is actually in almost all programming languages. But it’s much easier to understand in Pascal. Look: Old Level 04 - 27

13 Diego, Tasks

- Today is my lucky day. I came up with 5 new tasks for you. My creativity’s too evident. Good luck to you my friend. You’ll need it…
“for” loop tasks
1 Even numbers
Write a program that displays even numbers from 1 to 100 separated by spaces or each on a new line. Use “for” loop.
2 Draw a rectangle
Write a program that reads from keyboard two numbers: m and n.
The program should display to the screen a rectangle composed of eights sized m by n. Use “for” loop.

Example: m=2, n=4
3 Draw a triangle
Write a program that displays a right triangle composed of eights with sides 10 and 10. Use “for” loop.

Draw lines
Write a program that displays
  • a horizontal line of 10 eights
  • a vertical line of 10 eights
Use “for” loop.
5 Everybody loves somebody
Write a program that reads from keyboard name. Use “for” loop to display 10 times the text:
«name» loves me.

Example text:
Pam loves me.
Pam loves me.

14 Professor

- Hi, Amigo. How is it going for you? - Things are going well, Professor Noodles. I’ve already learned “for” and “while” loops. - Great! It figures that Professor Noodles is world’s best teacher. Together we’ll triumph over all those narrow-minded who contend that we should start with practice. You’re my living proof, if I may say so about a robot. Click on the link, you’ll find there something useful: CodeGym Lecture 4 Discussion

15 Julio

- Hey, Amigo! I have to give you something today, but first let’s watch TV a little, and then proceed, OK?

16 John Squirrels. Captain John Squirrels

- Hello, soldier! - Good morning, sir! - I have some awesome news for you. Here’s a quick check to reinforce your skills. With every day practice you’ll enhance your skills real quick. Tasks are specially designed to do in Intellij IDEA.
Additional tasks to do in Intellij Idea
1 1. I’ll never work for peanuts
Write a program that displays hundred times a sentence:
«I’ll never work for peanuts. Amigo».
Use “for” loop.
2 2. Display an average
Write a program that reads from keyboard three numbers.
The program should display to the screen the average of these numbers, that is, neither the biggest nor the smallest one.
3 3. Calculate a sum
Write a program that reads from keyboard numbers and calculates their sum.
If the user enters -1, the program should display the sum and terminate. -1 should be included in sum.
4 4. My name’s ‘Joe’...
Write a program that reads from keyboard a string «name» and a date of birth (three numbers): y, m, d.
The program should display to the screen:
«My name’s «name»
I was born on d.m.y»

«My name’s Joe
I was born on 15.2.1988»