Level 41

Old Level 05

Published in the Java Developer group

Can I become a software developer?

Old Level 05 - 1Marine principle states: if you find physically suitable man or woman, you can make him or her a unique fighter, provided them the right training and education. Programming is just the same skill as playing guitar, swimming or riding a bike. People are not born bikers. When I see my friends, who work twice as hard as I do and get four times less money, I start the following conversation: — Would you like to work as a programmer? You’re really smart. Perhaps, you are simply not on your place?

Why software development?

Before starting to teach a person programming, we’d better find out, what it gives him or her.

1 Simple and interesting job.

Software engineer is a simple and interesting job. It has great room for creativity. I love it. At first I went crazy at the thought I do what I like and get paid for it. But then I got used to it eventually.

2 It’s well paid.

I simply enjoy watching my friends buying themselves cars and houses in 5 years’ work.

3 Flexible hours.

A strict work schedule is a nasty thing. Any person who had ever been in a traffic jam during rush hour or got fined for being 5 minutes late can confirm that. And what about being able to arrive at work at 11 am and leave at 5 pm? For most programmers it is a usual schedule. Just do your work and no one will say a cross word. You may even work home in most of the companies. You can always come to a reasonable agreement with your employer.

4 Professional growth.

In most companies you need to climb the career ladder in order to get good payment. A software developer only has to be a software developer. You won’t have to re-qualify from a developer to manager or try to take a leading position. All you need to do is grow professionally. Developers who have 5 to 10 years of experience get paid royally.

5 High international mobility.

There are three most highly paid professions in the world: a lawyer, a doctor and a software developer. It is a real challenge for lawyers working abroad: different laws, case-law etc. A doctor has to learn the language, other medical standards and pass exams. A developer won’t have to learn anything additional. Same language. Same standards. For most times even clients are the same.

Why Java?

The total of three following factors made me retrain people for Java developers.

1. Java — is one of the easiest language for learning.

A person who‘ve just finished school is able to learn it in 3 to 6 month, depending on the base knowledge and quantity of hours put into study.

2. High demand on labor market.

You can get a job without prior experience. Companies employ interns eagerly and keep educating them.

3. Highest salaries in the field.

One of the highest. This is especially important for junior developers.  

Programming is a skill

Old Level 05 - 2You can’t read a book and become a computer programmer. You need 500 hour of practice at least. Just like boxing. You can’t simply watch matches and become a professional. You need to train for years. (That is why CodeGym has so many practical tasks). All offers of teaching you how to program in 10 hours are the identical to offers of teaching you to box in 10 hours. And then throw you in a boxing ring! Don’t do that. Sometimes when a newbie asks on the forums how to become a software developer, he is told to invent tasks for himself and do them. That won’t work. The thing is that a person can’t make a task beyond his knowledge. You either know something, or you don’t. Only a person well-versed on subject is capable of creating a sequential tasks set for you, each of which will teach you new things and won’t take weeks to solve. That is what I did in CodeGym.

New innovational teaching method

CodeGym education is made in a different way than college education. You’ll notice it quite soon. It’s much more effective. Your studies in college were probably like this: long lectures and practice to nail down what you’ve learned. Following this approach you improve your knowledge, not skills. To be honest, you skills acquired this way are practically worth nothing. I have another approach. I believe a person should ask questions first, and only then get answers on them. An answer before a question has no value. My lectures are answers on your questions. So first I give you practical tasks that are hard to solve with your current knowledge. These tasks raise questions and then you receive my answers that are knowledge and lectures. I present new knowledge to you in three stages:
  1. Introduction (Minimum theory and a few practical tasks)

  2. Principal block knowledge (You should understand the topic fully)

  3. Details and nuances (I fill gaps here)

This way you’ll come across one and the same topic at least thrice. And it’s not even worth mentioning that everything is interrelated greatly and it is simply impossible to fully explain one topic without having explained the one before it, at least partially. Some students even resent that some tasks are too hard, saying they haven’t yet studied that. Such tasks present a pause for thought: how do you solve them using your current level of knowledge? You can spend an hour or two solving these tasks, but later you’ll be very glad to find a new original approach or elegant solution. In fact, in real life you’ll first be given a task at work, and only later you’ll find knowledge of solving it. That’s real life, kids. Thus, the sooner you get used to such an approach, the better.

Level 5

Old Level 05 - 3

1 Elly talks about classes

- Hey, Amigo! - Hi, Elly! - Today I want to explain you what the classes are. - Explanation # 1. I’ll start with an analogy. All things in our universe are made of atoms. They can be of different types: hydrogen, oxygen, iron, uranium, ... Combining atoms enables creating various things or objects. - The same is true to the universe of Java. Here programs consist of objects of different types (where class is a type): Integer, String, File, Object, … Combining objects enables creating various web-services or programs. - Different atoms have different internal structure. They contain a number of electrons, protons and neutrons. - Different classes (object types in Java) have different internal structure too. They contain various variables and methods. - Yes, I have a general idea of the atom structure. I’m a robot, aren’t I? - Let’s look at the program as a whole: the objects are like building blocks that make up the program. Classes are types of those blocks. Blocks of different types are objects of different classes. - I kind of got it. - Explanation # 2. We create a new class when we need a new type of object. Within this class we describe the desired behavior of objects. - Well, I’ve understand something, but I’m not sure of this. - Considering internal structure, the class consists of class methods that do something and of class variables where methods store shared data. - Simply said, the class is a set of methods? - Pretty much, more specifically, the class is a group of methods that work together and variables in which methods store different values to share. - Yeah. To create a new class, we need to write these methods … - Yep. We also need to decide which variables are shared by different methods, and then take out variables from a method to the class: turn method variables into class variables. - Classes are created on the following pattern: 1 The programmer decides what other objects he needs. 2 The programmer divides these objects into different types depending on what they do. 3 The programmer writes a separate class for each type. 4 In the class, he declares the necessary methods and variables. 5 Commands have to be written in each method in order the method does what the programmer wants it to do. 6 The class is ready, now you can create its objects. - Awesome! It’s an interesting scheme. I’ll remember it. - You have to memorize it, it will be of use. Programming approach, in which the program is divided into objects, is called object-oriented programming (OOP). - Java is a classic example of OOP approach, because in Java everything is objects. - Learning Java consists of two major tasks: learning to write your own classes and learning to use other people’s classes. Today we start with the easiest things. You’ll learn to write simple classes and, of course, create their objects. Objects are often called instances of the classes. These are synonyms, either way is correct. - Got it. - To sum up I can say that the class is a miniprogram: a set of data and functions that do something with this data. An important feature of classes is the ability to create instances of these classes (objects). - To create a class object, you need to write in the code «new class_name()». Old Level 05 - 4- A class object has two interesting features: - First. Each class object stores its own copy of the class variables. So, if variables x, y are declared in the class and 10 objects of this class are created, then each object has its own variables. Changing variables of an object doesn’t affect variables of the other object. - Second. When a new object is created, various parameters can be passed to it. These are so-called «startup values». It’s almost like giving a name at birth. Many objects cannot be created without passing such parameters. - I understand a little. And what did you say about the class variables? - Every object has its copy of the data (class variables). Old Level 05 - 5

2 Risha talks about packages

- Hey, Amigo! Now I tell you about the packages. - Files in the computer are grouped into folders. Classes in Java (each class is in a separate file) are grouped by packages that are folders on a disc. This is nothing new. But there are two remarks. - First, «a unique full class name» is the «package name» + «class name». Examples: Old Level 05 - 6- The full class name is always unique! - It’s very hard to write a long name, such as java.util.ArrayList, every time. Therefore, in your code you can use short names of the other classes. But for that you need to use some magic. - What magic? - You can use the statement «import java.util.ArrayList;» - In the beginning of the class, immediately after the package is declared, you need to specify the full name of ArrayList class used in your code. Let’s say you use java.util.ArrayList, sun.generic.ArrayList and classes in your code. You can specify one of them in imports (e.g. sun.generic.ArrayList;) and use its short name. It regards one class only. - Why is this so complex? Can there be classes with identical names? - Yes, in different packages there can be classes with the same name. But we can’t import two classes with the same name to our class, so we have to apply a full name to one of them. - Here’s another analogy. You have Bill in your team and there isn’t any problem with communication, because everyone knows who it is. But if there were three Bills, then you would have to use their full unique names to distinguish between them. - Second, it’s better to put classes into packages and not in the src root folder. There is no problems when there are few classes, but it’s very easy to get confused when there are a lot of them. Therefore, always create classes in packages only. The rule in Java is to give classes and packages self-explanatory names. Many companies produce their libraries (class sets) and, to avoid confusion, name packs of these classes after the company / website: Old Level 05 - 7

3 Kim show video tutorials

- Hey, Amigo! Here are a couple of video tutorials, how to create classes and packages:
- We already did it! - You will do it thousand times in your robot life. So get used to it. And now, you need to practice! Creating Java classes Creating Packages

4 Elly, objects creation, object references

- So, we’ve learned the classes last time. Today I’d like to tell you how to create objects. It’s very simple: write the keyword new and the class name for the object we want to create: Old Level 05 - 8- But I’ve heard it before. - I know, but please listen. - When a new object is created, various parameters can be passed in braces. I’ll explain it a bit later today. Let’s consider the class Cat: Old Level 05 - 9- What are these getters and setters? - Variables not accessible from other classes are common practice in Java. Normally, variables declared within a class have the modifier private. - To give an access to private variable, you should create two methods: get and set. The goal of the method get is to return the current value of the variable to one who has called it. The goal of the method set is to set a new value. - And what’s the point? - If we don’t want anyone to change values of our object variables, we can just write no method set, or make it private. You can also add to this method additional data check. If the new passed value is invalid, then don’t change anything. - That’s interesting. - Since there can be a lot of variables in the class, the names of methods get and set usually include the name of a variable they work with. - If a variable is named fullName, the methods should be named getFullName and setFullName. And so on similarly to it. - Got it! Overall it’s an understandable approach. - Here are a couple of examples of how to work with a newly created object: Old Level 05 - 10

5 Diego, Tasks for creation of own classes and objects

- Hey, Amigo! Here are some tasks to create classes and objects:
1 Create a class Cat
Create a class Cat. A cat must have its name (name, String), age (age, int), weight (weight, int), and strength (strength, int).
2 Implement method fight
Implement method boolean fight(Cat anotherCat): implement a fight mechanism depending on cats’ weight, age and strength. Make up a dependency by yourself. The method should determine, whether current cat (the object whose fight method was called) or anotherCat won fight, i.e. return true if current cat won and false, if it didn’t win. The following condition must be met:

if cat1.fight(cat2) == true, then cat2.fight(cat1) == false
3 Getters and setters for the class Dog<
Create a class Dog. A dog must have a name - String name and age - int age.
Create getters and setters for all the variables of the Dog class.
4 Create three objects of Cat type
Create three objects of type Cat in the method main and fill them with data.
Use the class Cat of the first task. Do not create the class Cat.
5 Hold three twosome fights between cats
Create three cats using the class Cat.
Hold three pairwise fights between cats.
Do not create the class Cat. For the fight, use the method boolean fight(Cat anotherCat).
Display the result of each fight.

6 Risha talks about a object initialization

- I want to tell you about the object initialization. When an object is created, it’s necessary to assign startup data to its variables, in order to avoid a situation when you try access an object, and it has no data required to work properly. - Let’s consider an object of the type File. The minimum necessary information for the file is its name. A file without a name is a nonsense. - Suppose you are writing your own version of File class (MyFileClass for example) to work with files. What information is needed for each object of this class? - The name of the file which this object will work with? - That’s right. That’s why we add the method initialize() to our class. It’ll look like this: Old Level 05 - 11- We’ve added the method initialize, in order to be able to work with an object - call its methods. This can be done immediately after the method initialize is called. If you can’t work with an object, it’s called invalid, otherwise it’s valid. The main objective of the method initialize – is to pass all the necessary data to the object to make it valid. - Got it! - Now let’s complicate the task. Or rather simplify. Depending on how you look at it. Imagine that for another programmer who’ll use our class it’s easier to pass not the full file name, but a directory and a short file name. We can implement this functionality using another method initialize (Java enables to create multiple methods with identical names). Then our class will look like this: Old Level 05 - 12- What’s more, it’s often necessary to create a temporary file copy next to the current one. - Can we make a method for this case? - Sure, take a look at this: Old Level 05 - 13- So, I can make as many of these methods as I wish? - Within reason, of course. But, actually, as many as you wish. - And when is it necessary to call the method initialize? - Immediately after the object is created, to turn it into a valid one: Old Level 05 - 14- And what’s this method getFolder()? - You can see from its name that this method is to return a string containing the name of the folder where our file is located. But we didn’t write its implementation here, it’s just for show.

7 Diego, Object initialization tasks

- Hey, Amigo! I’m bored without our lessons. Here are some object initialization tasks:
1 Create a class Friend
Create a class Friend with three initializers (three methods initialize):
- Name
- Name, age
- Name, age, sex
2 Create a class Cat
Create a class Cat with five initializers:
- Name
- Name, weight, age
- Name, age (standard weight)
- Weight, color, (name, address and age are unknown, it’s an alley cat)
- Weight, color, address (it’s someone else’s house cat)
Initializer’s task is to make an object valid. For example, if the weight is unknown, you need to specify some average weight. A cat can’t have any weight at all, likewise age. But it can have no name (null). The same applies to the address - can be null.
3 Create a class Dog
Create a class Dog with three initializers:
- Name
- Name, height
- Name, height, color
4 Create a class Circle
Create a class Circle with three initializers:
- centerX, centerY, radius
- centerX, centerY, radius, width
- centerX, centerY, radius, width, color
5 Create a class Rectangle
Create a class Rectangle. Its data will be top, left, width, and height. Write for it as much initialize (...) methods as possible

- 4 parameters should be set: left, top, width, height
- width/height are not set (both equal 0)
- height are not set (equal to width), create a square
- create a copy of another rectangle (it’s passed in the parameters)

8 Elly talks about constructors

- It’s time to tell you about constructors. It’s very simple: programmers invented a shorthand notation for object creation and initialization: Old Level 05 - 15- But I’ve just understood those initializers… - Don’t be upset. Take a closer look. Using constructors is more handy and space-saving. - Yeah, it looks much better. But there’s a question: I know how to write a method initialize in the class, but how am I to write a constructor within the class? - First, look at a simple example: Old Level 05 - 16- It’s very easy to declare the constructor in the class. The constructor is like the initialize method, but there are two differences:
  • The name of the constructor method is the same as the class name (instead of initialize).
  • The constructor method has no return type (no type is specified at all).
- In fact, it’s the same method initialize, but with a few differences. I kind of got it.

9 Diego, Constructor tasks

- You’ve had some rest, I guess. Fine. Here are some constructor creation tasks:
1 Create a class Friend
Create a class Friend with three constructors:
- Name
- Name, age
- Name, age, sex
2 Create a class Cat
Create a class Cat with five constructors:
- Name,
- Name, weight, age
- Name, age (standard weight)
- Weight, color, (name, address and age unknown. It’s an alley cat)
- Weight, color, address (it’s someone else’s house cat)
Initializer’s task is to make an object valid. For example, if the weight is unknown, you need to specify some average weight. A cat can’t have any weight at all, likewise age. But it can have no name (null). The same applies to the address - can be null.
3 Create a class Dog
Create a class Dog with three constructors:
- Name
- Name, height
- Name, height, color
4 Create a class Circle
Create a class Circle with three constructors:
- centerX, centerY, radius
- centerX, centerY, radius, width
- centerX, centerY, radius, width, color
5 Create a class Rectangle
Create a class Rectangle. Its data will be top, left, width and height. Create for it as much constructors as possible:

- 4 parameters are set: left, top, width, height
- width/height are not set (both equal 0)
- height are not set (equal to width), create a square
- create a copy of another rectangle (it’s passed in the parameters)

10 Professor, Classes and constructors

- It’s me again. Our lectures are simply great. I won’t give you links to boring lectures. Here’s a link to excellent stuff! - Are you still here? Go quickly, read, and I must go to the lab. CodeGym Lecture 5 Discussion

11 Julio

- Hey, Amigo! I’m a little tired. Let’s rest a bit, and then start the lesson. I’ve found a new episode:

12 John Squirrels

- Hello, soldier! - Good morning, sir! - I have some awesome news for you. Here are tasks to reinforce your skills. Do it every day, and you’ll enhance your skills quick. Tasks are specially designed to do in Intellij IDEA.
Additional tasks to do in Intellij Idea
1 1. Three classes
1. Create class Cat and Dog by analogy with the class Duck.
2. Think of what the toString method should return in the classes Cat and Dog.
3. In the method main create two objects in each class and display them.
4. The Duck class objects are created and displaying.
2 Man and Woman
1. Create public static classes Man and Woman within the class Solution.
2. Classes must have fields: name(String), age(int), address(String).
3. Create constructors to pass all the possible parameters to.
4. Use the constructor to create two objects of each class with all the data.
5. Display the objects in format [name + " " + age + " " + address].
3 3. Create public static classes Dog and Cat.
Add three fields to each class at your option.
Create objects for the Tom and Jerry cartoon characters, as much as you remember.

Mouse jerryMouse = new Mouse(“Jerry”, 12 (height, cm), 5 (tail length, cm))
4 4. Display the current date
Display to the screen the current date in a form similar to «21 02 2014».
5 5. Read numbers from keyboard and calculate their total
Read numbers from keyboard and calculate their total until the user enters the word «total». Display to the screen the total.
- Those tasks were for greens. I added bonus tasks of higher complexity. For top guns only.
Bonus tasks
1 1. Program doesn’t compile and run. Fix it.
Task: The program should read from keyboard two numbers and display their total.
2 2. Add new functionality to the program.
Old Task: add a new function that reads from keyboard two numbers and displays their minimum.
New task: add a new function that reads from keyboard five numbers and displays their minimum.
3 3. Learning and practicing algorithm.
Task: Write a program that
1. reads from the console number N that is greater than zero
2. then reads N numbers from the console
3. displays the maximum of entered N numbers.