CodeGym – learning programming in Java

Old Level 02 - 1How about an entirely new way to study programming and get an education? That doesn’t look like anything you’ve seen before. How about learning, where you have an aim, means and result? I am happy to present you the new online programming courses for learning Java.

1 Training is an online-game

You take a task, execute it and get a reward. I think it is clear and common for you. Tasks are most various: reading code, solving tasks, videos-lessons, correction of errors in the code, adding new features, large tasks, writing games and a lot more.

2 Only the bare essentials

In order for the course not to last for 5 years, I threw away all useful and left only most necessary things from it. I analyzed tens of vacancies on the labor market. All topics newbie needs to know to get a Java Junior Developer job are included in the course.

3 I’ve approached your training thoroughly

A complete course contains 500 mini-lectures and 1200 (!) practical tasks. Tasks are small, but they are numerous. There is a great deal of them. Just that minimum, doing which, you will get such a valuable experience. There is also work in pairs, different games, large tasks, real projects and other types of practice.

4 You cannot finish the game and not become a developer

Course contains 40 levels. You can move to the next level, only if you’ve solved greater part of tasks on the current level. Starting with little and easy, ending with large and very useful. Each person who reaches the end will get 300-500 hours of practical experience. And this gives a great chance to win. And to get a job.

5 Goal- oriented job interview preparation

Last 10 levels are dedicated to writing a resume, to preparation for the interview and obtaining skills of team-work. The videos of job interviews and their analysis will be added. Well and, certainly, typical questions on interview with answers. Old Level 02 - 2

6 You can solve tasks directly on site

It’s very handy and effective. Having just analyzed a new task in lecture you’d have to make your own by analogy just right here, on the site. For those who want to do the task in IDE there is a plugin which enables you to receive a task in two clicks and hand it in for checking in one. Lectures, analysis of examples, solving tasks directly on a web-site, solving tasks in IDE - a gap between theory and practice is as thin as never before.

7 Instant task check (in less than second)

Are you familiar with situation, when you handed over a task/work for checking, and had to wait for results for a week, because the person who checks it is busy? This is just the case with most offline courses. In CodeGym you will get results of compiling and checking of your task solution in less than a second, after pressing an «Execute/Check» button.

8 CodeGym loves it, when you spend time on Facebook during study

On Facebook there is a page dedicated to the project. In it you can find interesting technical articles, motivational stories, CodeGym news and a lot of other useful information.

9 Coverage

In lectures there are many references to different web-sites, where you can read explanations of other lecturers. My aim is that you understand the material, not listen solely to me.

10 You will only need your brain and computer to become a developer

It will take you 3 to 6 months, depending on how much time you practice.

11 Support

Old Level 02 - 3No doubt questions will arise when you face thousands of tasks. We’ve established a special service, where you will be able to ask questions about tasks. Other CodeGym-students, moderators, volunteers and founding members of the service will be answering you.

12 Community

We believe that strength is in unity. Therefore we created the Community, where you can ask questions; discuss different topics, share articles and blog. Besides, a community is the ideal place to get a job on acquaintance. Therefore feel free to ask smart questions and give smart answers. The more active you behave and help other, the more chances, that other member of association will suggest you to join their project.

You have reached a new level

Level 2

Old Level 02 - 4

1 Elly, the basis of all

- Hey, Amigo. Today I want to tell you how a typical Java program is structured. The point is that every Java program consists of classes and objects. - I already know what classes are. And what are objects? - By analogy with building of a small ship, first, you need to make a drawing, and then give it to the shipyard. Based on this drawing a ship will be assembled. Or five ships. Actually, as many as you wish. Dozens of identical ships are built based on one drawing, that’s what it’s all about! - All things in Java programming are exactly the same. - The programmer is like a design engineer. And if the design engineer has to make drawings, the Java programmer has to write classes. Then, parts are made based on the drawings, and objects are made based on classes. - First, we write classes (do drawings), and later at program runtime, the Java Virtual Machine creates objects using these classes. In just the same way ships are made. There’s one drawing and a lot of ships. There are different vessels that have different names and carry various cargoes. However, they are very much alike: they all have the same structure and can perform the same tasks. - In the ships’ case, everything’s clear. Give me a couple more comparisons to understand exactly what it’s all about? - Sure. For instance, bees … Old Level 02 - 5- Oh no, wait a moment, bees remind me of something bad. Think better of an anthill. - An anthill is a good example of object interaction. In the simplest anthill, there are three classes of ants: a queen, soldiers and worker ants. There’s a difference in number of ants for each class. The queen is alone in the nest; there are scores of soldiers and hundreds of working ants. So, we can say, there are three classes and hundreds of objects. Ants communicate with each other, with the same ants and ants of other classes according to strict rules. - This is a perfect example. For a typical program, it’s all the same. A main object creates objects of all the other classes. The objects begin to interact with each other and "the outer world" of a program. Objects’ behavior is hardcoded inside of them. - I don’t quite understand. Rather, don’t understand at all. - These two explanations are the different sides of the same coin. The truth is somewhere in between. The first example (the one with drawing and ships) shows the relationship between the class and the objects of this class. The resemblance is very close. The second example (the one with the anthill) shows the relationship between written classes and objects that exist at the runtime. - Do you mean that first we need to write classes for all the objects existing in a program and what is more, describe their interaction? - Yes, but it’s easier than it seems. In Java, all things at program runtime are objects, and when you write the program, you should describe different ways of object interaction. Objects just call methods of each other and pass the necessary data to them. - It’s not obvious, but almost clear. - But how do you know what methods to call and what data you need to pass to methods? - Each class has its description to specify what the class was created for. Each method has likewise its description of what it does and what data must be sent to it. To use the class, you need to know in general what it does. Besides, you need to know exactly what each method of this class does. But you don’t have to know how it’s done. It’s a kind of magic. - Humph. Sounds so.   - Here, have a look at a class code that copies a file: Old Level 02 - 6- It’s not that easy, but I already get the picture. - Fine. See you later, alligator!

2 Risha, Variables and primitive types

- Hey you, free labor. - I mean, «Hi, Amigo». I want to acquaint you with the internal structure of variables. You already know that each variable has a piece of memory associated with it, where the variable stores its value. - Yeah. You told it last time. - Fine. It’s a good thing that you remember it. Then I’ll continue. - classes. When we describe a new class in a program, this means that we declare a new complex compound type, which data will be either other complex types or primitive types. Old Level 02 - 7- It’s still clear for a wonder. - Since large (complex) types consist of a lot of small ones (primitive), their objects use a lot of memory. More than the usual primitive type variables do. Sometimes a lot more. Assignment of complex types variables took much time and required copying large amounts of memory. That’s why complex type variables store not the object itself, but a mere object reference (that is a four-byte address). This is enough to access the object data. JVM takes on all the difficulties related thereto. - I don’t get it. - We’ve already discussed that the variable is like a box. To store 13 in it, you can write the number on a sheet of paper and put it in a box. - Now imagine that you have to store in a box (variable) something a little bigger. For instance, a dog, a car, or your neighbor Joe. So as to simplify you can do an easy thing: put the dog’s picture instead of the dog, the car’s number plate instead of the car, and Joe’s phone number written on a sheet of paper instead of Joe himself. - Now we take a piece of paper and write Joe’s phone number on it. It’s like an object reference. If we pull out of the box a sheet Joe’s number is written on, make copies and put in a couple of boxes, references to Joe will increase, but there can be only one Joe. - Advantage of such data storage is that there can be many references and only one object. - It’s exciting. By the way, I almost understood. Only answer one more question: what happens if I assign a complex type variable to another complex type variable? - Then these two variables will contain the same address. And, therefore, if you change the data stored in one complex type variable, the data of the other one will also be changed. Because you know, there really is only one object the variables store references to. Nevertheless, there may be a great many of variables that store references to the object. - And what is stored in complex type variables (reference/class type) while there are no object references? Can there really be such a thing? - Yes, Amigo. You’ve took the words out of my mouth. That can be. If there is no object reference in the reference (complex) type variable, it stores null, a special «null reference». In fact, it simply stores the address of an object equal to 0. But Java Virtual Machine never creates objects with zero address, therefore it always knows that if reference variable is 0, then there’s no object there. Old Level 02 - 8Do I get it right? The variables are divided into two types - primitive and reference. A primitive type variable stores a value while a reference type variable stores an object reference. Primitive types are int, char, boolean, and some more. The rest are reference variables, they’re formed using classes. - That’s right, boy.

3 Elly, What objects are

Old Level 02 - 9- Here is your favorite teacher again. Since we get ahead fast, I’ll tell you what objects are and how deal with them. - To create any object, you need to write the object type name (class) and the keyword new before it. Say we have a class Cat, then: Old Level 02 - 10- What happens if you just create an object and don’t assign it [its reference] to any variable? - If you do so, Java Virtual Machine will create the object and immediately declare it garbage (unused object). After a while, it will delete that object during garbage collection. - How can I destroy the object if I no longer need it? - You can’t. As soon as there is no variable to store an object reference, the object will be flagged as garbage and JVM will destroy it during the next garbage collection. As long as there’s at least one object reference, it will be considered live and won’t be destroyed. When you want to quickly destroy the object, set all its references to null, that is, assign a null value to all the variables that refer to that object. - Got it. It seems not difficult at all after previous lectures. - Okay, then here are few tasks for you to improve your skills. These are also on System.out. But later there is going to be more difficult ones. And you could show us of what steel you are made.
Tasks
1 Write a program that displays: «Step by step and the thing is done.».
2 Write a program that displays 7 times: «It’s kind of fun to do the impossible.».
3 Write a program that displays: «Always desire to learn something useful.».

4 Risha, Visibility of variables.

- Hi, my favorite student. Now I’ll tell you about the visibility of variables. - Why, are there invisible variables, too? - No, variables are visible in places of the code where that variable can be accessed. Some variables can be accessed from anywhere in a program, other ones only within their class, and some variables can be accessed only within one method. - For example, you can’t refer to the variable before it’s declared. - It’s clear. - Here are some examples: Old Level 02 - 111 A variable declared in a method exists / is visible from the beginning 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 anywhere within the scope of the method. 4 Class / objects variables exist during the entire lifetime of their object. Special access modifiers public and private additionally control their visibility. 5 Static class variables exist at the program runtime. The access modifiers also determine their visibility. - I love pictures, everything falls into place. - Good for you, Amigo. Always knew you are a smart fellow. - I’ll also tell you about access modifiers. Don’t be so scared, it’s not rocket science. These are the words public and private that you see. - I’m not scared, it’s just cold here. - Yeah, sure. You can control access (visibility) of methods and variables in your class from the other classes. For each method or variable, you may specify only one access modifier. 1 Modifier public A variable, method or class flagged with a modifier public can be accessed from anywhere in the program. It’s the highest degree of openness, without restriction. 2 Modifier private A variable or method flagged with a modifier private can only be accessed from the class where it’s declared. For all other classes, a flagged method or variable are invisible, as if they don’t exist. This is the highest degree of closedness (visible from the same class only). 3 Modifier default If any modifier doesn’t flag a variable or method, it’s considered to be a modifier default. Variables or methods with that modifier (that is, without any at all) are visible to all the classes of the package they’re declared in. Only in the same package. This modifier is sometimes called package to imply that access to variables and methods is possible for the entire package, which their class belongs to Explanation tab: Old Level 02 - 12

5 Diego, Tasks for creating a bunch of variables

- Hey, buddy. If need be, I keep a copy of the contract for you. That sneaky cheapskate Risha is blissfully unaware. You should have seen the amounts in my contract. Heh, heh. - Well done, Diego. I think you teach me real useful things. - Sure, Amigo. No gain without pain. But still someone suffers the pain with no gain. - Let’s better turn hand to study. Now I’ll show you how to create a bunch of variables in different ways: Old Level 02 - 13- Wow, Diego! You’re so smart. - Heh, heh! Thanks, Amigo. - I think you’ve already missed my tasks. So here are some of them. Those humans, aliens don’t know anything about our robo-life. Who but me will teach you that?
Tasks
1 Write a program that displays 3 times: «Many people are alive only because it’s illegal to shoot them.».
2 Write a program that displays: «If I want your opinion, I'll ask you to fill out the necessary forms.».
3 Write a program that displays 20 times: «I never said most of the things I said.».

6 Elly, Passing by reference. Reference variables

- Hey, Amigo, it’s me again, Elly. Sorry I’m always say this, but it’s just the way the things are done on Earth in the 31 century. I’ll explain you the features of reference variables and how to pass referential arguments to functions (methods). - I’m ready. - Well then, reference variables are variables of all types except primitive ones. These variables contain just the address of the object (object reference). - Variables of primitive types store a value and type, and class variables store references to the objects of the same class, well, or null. Am I right? - Absolutely. - So, what’s a reference? - Object and its reference are linked, let us say, like a man and his phone number. A phone number isn’t a man, but it can be used to call a man to ask him for something, guide or command him. A reference is also used to interact with an object. All objects interact with each other via references. - It’s as if people communicate with each other over the phone? - That’s right. When you assign a primitive object, its value is copied (duplicated). When you assign a reference variable, only the object address (phone number) is copied, and not the object itself. - ОK, I got it. - A reference offers another advantage: you can pass an object reference to any method, and this method will be able to modify (change) our object using its reference by calling its methods and accessing data within the object. Old Level 02 - 14- The values of variables a & b are interchanged inside the swap method. When you call the swap method, the variables a & b receive copies of the values m & n. Therefore, the values of variables m & n remain the same when you change the values of a & b. It is clearly visible in the code of the right column. - To be honest, I got nothing of that. Can I have a couple more examples? - In the case of the reference type, you can do so: Old Level 02 - 15- Variables a and b are assigned references to emma and alice, respectively, a and b values change within objects emma and alice. - So it’s possible to declare classes within other classes, too? Wow! - As for the rest, it’s not clear. - Not all at the same time.

7 Elly, Function call, return value

- Well, then let’s turn around. We will talk on how the method call works, and then you try again to glance at the previous lecture, okay? - Deal! - Well, then I’ll tell you about the function/method call and values returned. - Commands are grouped into functions, so you could run them in a single block, as one complex command. To do this, you need write the name of the function (method) and then in parentheses following the name specify values and parameters. Old Level 02 - 16- In the above example, we wrote a function displaying the passed string 4 times. Then we called function print4 in line 6. - When it comes to the execution of line 6, the program will jump to line 9, and s variable will be assigned a value “I like to move it, move it.” - Then lines 11-14 will be executed, and finally, the function will be complete and the program will continue with line 7. - Gotcha. - You can not only pass arguments (parameters) to the function, but also return the result (value) of function’s operation. This is done by using the keyword return. Here’s what it looks like: Old Level 02 - 17- I think I’m catching it on. It’s the same code on the left and on the right. On the left, it’s just presented as a separate function. - The function calculates a value and passes it to those who called it using the return command. At least I think so. - Basically, it’s true. - And what’s the void type? - Some functions just do something, but don’t calculate and return any values, like our method main().For those functions it was created a special result type void (an empty type). - Why can we just specify nothing, if a function doesn’t return anything? - Just think of how every variable is declared: type and name. A function is declared with type, name, and parentheses. Function name followed by the parentheses is a function call! - So it was easier to come up with "empty type" than divide functions into two categories those returning a value and not returning a value? - Exactly! You’re a quick thinker, my boy. - And how can I return an empty type? - You can’t. Here’s how it works: when the JVM executes a return command, it calculates the value of the expression on the right of the word return, stores that value in a special memory area and immediately exits the function. It uses the saved value as the result of a function call at the place where the function was called. You can see it in the above example. - Do you mean the place where int m = min(a,b) transformed to m = m2? - Yes. After the function call everything continues to work as if instead of the function its result is written at the same place. Read that sentence once again and look at the code of the last example. - It just seems easy, but it’s difficult in reality. I’ve get just a little bit, that’s all. - It’s okay. On the first try you can understand only what you already know. The more you got into a new area for you, the more it’s unclear. And the result will be even more awesome. As time goes on, everything becomes clear. - Well, if so, then let’s keep going.

8 Diego

- Hey, Amigo. Here’s a couple of tasks for you. Now you are capable to do something more than System.out. Do your best! I believe in you!
Tasks
1 Minimum of two numbers
Write a function that returns a minimum of two numbers.
2 Maximum of two numbers
Write a function that returns a maximum of two numbers.
3 Minimum of three numbers
Write a function that returns a minimum of three numbers.
4 Minimum of four numbers
Write a function that returns a minimum of four numbers.
The function min(a,b,c,d) has to use (call) the function min(a, b)
5 String duplication
Write a function that displays three times the passed string. Each string should be on a new line.
6 Text display
Write a function that displays the passed string (word) three times in the same line.
Words must be separated by a space and don’t have to merge into one.

9 Diego, Full class name

Old Level 02 - 18- Hey, Amigo, I want to tell you about the full class names. - You already know that the classes are in the packages. So, a full class name is a name that covers all the packages separated by points, and a class name. Examples: Old Level 02 - 19- To use the class in your own code you need to specify its full name. However, you can use a short name - a class name only. To do this you need to “import” this class by specifying its name adding the word import before you declare a class. Classes of the java.lang (String, Integer,...) package are imported by default. You don’t need to specify them. Example: Old Level 02 - 20- An example of use of short names: Old Level 02 - 21- I got it. - Fine.

10 Professor, Lecture about classes and objects

Old Level 02 - 22- Hey, Amigo. Hope you understood everything Elly and Risha have told you today. Just to be safe, as I promised, here are links to further material: 1 My notes 2 Thinking in Java. That awesome book I’ve already told you about. It would be great if you read it. - I hope you’ll make time to glance it through. - Ahem. Sure I will!

11 Julio

- Hey, Amigo! Hope you’re for some hard and tiresome rest?