image-ru-00-10

Amigo was nervous. His thoughts were scattered. His eyes twitched when he thought about the previous night. Those strange creatures he met yesterday wanted something from him. Something so incomprehensible and unimaginable that even he, who considered himself to be the smartest and the bravest among his peers, started nervously chewing punch cards at the very thought of it.

They want to teach him how to program! To program in Java. What nonsense!

Even the most naive robots know that robots first appeared as part of the Creator’s divine design.

"And the Creator took metal and from
it formed a robot in His own image and likeness.
And He created Java programs – the souls
of robots, and loaded them onto the robots and
made them living souls".

Operating Instruction,
Section 3, Article 13.

Even worse, they aren’t just claiming it’s theoretically possible. They’re actually planning to do it. And he… he consented! He agreed! Why?

He’s going to be a Java programmer. They’re going to, what, turn him into the Creator? Why would they do that? Just because?

Is this just a mean trick? Will he later have to suffer from bugs till his battery finally gives out? But the temptation was too great, and he couldn’t resist. He had always been ambitious, and wanted more. But nobody could have been prepared for an offer like that. Of course, he’d tried to buy himself more time, but then the aliens threatened they would choose someone else.

Maybe it is just as prank? No, it seemed genuine. He’d seen proof. It had really happened to him, and he gave his consent. If the aliens weren’t lying, he’d really become a Java programmer. The first robot-programmer ever!

He is the chosen one. That’s what this all means. He’ll learn to code and start writing programs. His own programs. Any programs he wants! He’ll bring light to places where darkness has always reigned.

He will be revered; all will bow down before him. And anyone who refuses...

"Hello, Amigo! My name is Rishi. I'm going to help you learn Java."

A quiet voice pulled Amigo from his thoughts, returning him to cold reality. He was sitting in the very center of the aliens’ spaceship. Isn’t this a little intense for a robot only in 7th grade?

The alien was still talking. Well, the die is cast. He was here, so he might as well learn. He would study diligently, starting by simply listening.

"I have been working on the Galactic Rush for many years, but this is the first time I’ve encountered such a planet. I’d like to know more about you. To begin, would you please tell me how you learn. You do learn, don’t you?"

"Yes, we share knowledge. We have preachers, or lecturers. They lecture, and we listen. Sometimes we write things down. Everyone just comes up to the robo-lecturer and explains his understanding of what he heard. If the robo-lecturer likes the answers, he records the knowledge of the sermon."

"How absurd! No wonder your civilization is ignorant."

"We're not ignorant. Where did you get that idea?"

Amigo was taken aback by his impudence. Arguing with the aliens? How impolite! He had just promised himself that he would listen attentively!

"It's often hard to distinguish advanced technology from magic," Rishi continued, ignoring Amigo’s remark.

"Considering your level… I believe all technology may seem magical to you. Tell me, what happens inside a program?"

"Java programs are divine creations. How can we comprehend them?"

"We can, Amigo, we can. And faster than you think. If you don’t know something, it can seem complicated and even unattainable. But if you have a good teacher who explains everything as simply as possible, you’ll be surprised how such simple concepts could have seemed so complicated."

"Knowledge is not important. Principles and skills are important. I have extensive knowledge, but above all I’m a bureaucrat. A 16th-generation Bureaucrat, with a capital B."

"And this is super cool! My bureaucratic skills have helped me create the best Java courses for you. They include tasks, programs, games, exercises, pictures and even lessons."

"Even lessons?!" asked Amigo, genuinely surprised.

"Yes. As was proven in the 22nd century, a good lesson is just a bit more effective than a good book. And mediocre lessons are worse than a mediocre book. However, as we're currently very limited in our teaching tools and can't put you through a standard 28th-century educational simulator, we had to resort to much more primitive methods. We made up a real hellish cocktail of games, tasks, pictures, lessons and cartoons."

"This is intriguing."

"I hope so. Interest and intrigue are the basis of any learning process."

"If a student becomes bored, the teacher shall be beaten with a stick — This is a quote from a law on education from the 24th century."

"What a good law!"

"What did you expect? Let’s imagine a movie that has bad ratings. In this case, the director is to blame, not the audience. If he makes interesting movies, he’ll always fill the theaters."

"I totally agree! I’m ready to listen!"

"Very good. Let's start then."

Rishi’s voice was spellbinding, and Amigo tried not to miss a single word.

"A program is a set (list) of commands. First, you execute the first command, then the second, then the third and so on. Once all the commands are executed, the program is finished."

"What kinds of commands are there?"

"Commands depend on what is executing them. On the types of commands the actor knows (and understands)."

"You can give a command to a dog: 'Sit!', 'Bark!'; to a cat: 'Shoo!'; to a human: 'Freeze, or I’ll shoot!'; or to a robot: 'Work! Work, roboscum!'"

"What else?" Amigo was finally starting to have fun.

"Programs written in Java are executed by the Java virtual machine (JVM). The JVM is a special program that knows how to execute programs written in Java."

"The list of its commands is quite extensive. For example, this command could be used to display 'Robots are friends to humans' on the screen."

Here's a super simple command:
System.out.println("Robots are friends to humans");

"O_O"

"Rather than starting with commands, we’ll begin with a couple of simple principles."

"Knowing a couple of principles can replace knowledge of many facts."

"Here’s the first principle."

"In the Java programming language, each command is written on its own line. A semicolon must be placed at the end of a command."

"Suppose we want to display 'Humans and robots are friends forever' on the screen three times. This is how it would look:"

The program is made up of three commands:
System.out.println("Humans and robots are friends forever");
System.out.println("Humans and robots are friends forever");
System.out.println("Humans and robots are friends forever");

"The second principle."

"A program can’t consist of nothing but commands."

"Imagine a room in an apartment. A room can’t exist on its own. It’s part of some apartment. An apartment also can’t exist on its own. It’s part of some building."

"On the other hand, we can say that the building is divided into apartments and an apartment is divided into rooms."

"Everything is clear so far."

"A command is like a room. In the Java programming language, a command can't exist on its own. It's part of a function (in Java, 'functions' are also called 'methods'). A method is part of a class. In other words, a class is divided into methods and methods are divided into commands."

"So a class is an apartment building, a function/method is an apartment, and a command is a room. Did I get that right?"

"Yes, that's absolutely correct."

Amigo looked at Rishi in awe. This human was explaining to him the basics of programming using the divine Java language! And he, Amigo, just understood (had guessed all by himself!) that programs consist of classes, classes consist of methods, and methods consist of commands!

Amigo still didn't know why he needed it, but he was certain that this knowledge would make him the most powerful robot on the planet.

Meanwhile, Rishi went on:

"Programs in Java consist of classes. There might be tens of thousands of classes. A minimal program is one class. For each class, a separate file is created. The name of the file matches the name of the class."

"Suppose you decide to create a class that describes a home. You’ll need to create a Home class that will be saved in the file Home.java."

"If you want to describe a cat in the program, then you’ll have to create a file Cat.java and declare the Cat class in it, etc."

"The files contain code (text) written in the Java programing language. Usually a class’s code consists of the 'class name' and 'class body'. The class body is written in curly brackets. This is how the Home class (file Home.java) should look:"

public class Home
{


Class body



}

"I understand so far."

"Great. Let’s go on then. The class body may contain variables (also known as data) and methods ('functions')."

public class Home
{
    Variable A


    Variable Z



Method 1




Method N


}

"Would you please give me an example?"

"An example? Of course!"

public class Home
{
    int a;
    int b;

    public static void main(String[] args)
    {
        System.out.print("1");
    }

    public static double pi()
    {
        return 3.14;
    }
}

"Are int a and int b variables, and main and pi methods?"

"Yep."

"Can classes exist without variables?"

"Yes."

"And without methods?"

"Yes. But a minimal program must contain at least one class that must include at least one method/function to get the program running. This method must be named 'main'. A minimal program looks like this:"

public class Home
{
    public static void main (String[] args)
    {
    }
}

"I can see the Home class here. I can see the 'main' method, but where are the commands?"

"A minimal program doesn't have any commands. That’s why it's called 'minimal'."

"I see."

"The class that starts the program can have any name, but the 'main' method used to start the program must always look the same:"

public class Home
{
   //Unchangeable part
   public static void main(String[] args)
   {


Code for the method



   }
}

"I think I understand everything. At least, it seems so right now."

"Brilliant. Let’s take a little break then. Shall we have some coffee?"

"Robots don’t drink coffee. Water make us rust fast."

"What do you drink then?"

"Beer, whiskey, 100-year-old alcohol."

"That’s even better. Shall we have some beer then?"