Object interaction

Java Syntax
Level 2 , Lesson 1

"Hi, Amigo. Today I'll tell you about a typical Java program. The big news is that every program written in Java consists of classes and objects."

"I already know what classes are. What are objects?"

"Let's start with an analogy. Suppose you want to build a small ship. You work on a design and then send the blueprint to a factory, where a ship will be assembled according to your design. Or a dozen ships, or as many ships as you want. My point is that dozens of identical ships can be made based on one blueprint."

"That's exactly how it works with Java."

"Java programmers are like design engineers, except instead of creating blueprints, they write classes. Ship parts are made based on blueprints, while objects are created based on classes."

"First, we write classes (make blueprints). Then, when the program is run, the Java machine creates objects based on these classes. It's exactly like how ships are built from a blueprint. One blueprint – many ships. The ships are different. They have different names and carry different cargo. But they are still similar. They all have an identical design, and are able to perform similar tasks."

"OK, I get your ship analogy. Could you give me a couple more to help me be sure I understand what you're saying?"

"Take, for example, bees..."

Object interaction - 1

"No, scratch that. I've had a bad experience with bees. Let's take ants."

"An ant colony is a good example of how objects interact. Any ant colony consists of three classes: the queen, soldiers, and worker ants. The number of ants in each class varies. Usually a colony only has one queen, dozens of soldiers, and hundreds of workers. Three classes, hundreds of objects. The ants follow strict rules as they interact with ants in their own class and ants that belong to other classes."

"This is the perfect example. A typical program works exactly like that. There is a main object that creates objects in all the classes. The objects interact with each other and with the external world. The objects' behavior is hardwired (programmed) internally."

"I don't quite get it. I mean, I don't get it at all."

"These two explanations are two sides of the same coin. The truth is somewhere in between. The first example (about blueprints and ships) shows us the connection between a class and its objects. It's a powerful analogy. The ant colony analogy demonstrates the relationship between objects, which are described by classes and exist only while a program is running."

"You mean we need to write classes for all objects used in a program, and then describe their interactions?"

"Yes, but it's easier than it sounds. In Java, while a program is running, all entities are objects. Writing a program amounts to describing the various ways that objects can interact. The objects simply call each other's methods and pass the required data to them."

"It's a little fuzzy, but I think I almost get it."

"How do we know which methods to call and which data to pass?"

"Each class has a declaration, which indicates its intended use. Similarly, every method has a declaration that indicates what it can do and what data we need to pass to it. To use a class, you need to have a general understanding of what it does. You need to know exactly what each method does, but not exactly how it does it. It's like a magic wand."

"Huh! Sounds nice."

"Here. Have a look at the code of a class that copies files:"

Copy c:\data.txt to c:\result.txt
package com.codegym.lesson2;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileCopy
    public static void main(String[] args) throws IOException
        FileInputStream fileInputStream = new FileInputStream("c:\data.txt");
        FileOutputStream fileOutputStream = new FileOutputStream("c:\result.txt");

        while (fileInputStream.available() > 0)
            int data = fileInputStream.read();


"I can't say I get it all, but I think I got the essence of it."

"Great. See you next time then."

"I almost forgot. Here's your task from Diego."

Java Syntax, level 2, lesson 1
Implement the print method
Writing your own methods is the way to achieve unlimited freedom, but it is also a great responsibility. We implement methods carefully and thoughtfully. This task requires you to write a print method that will display a certain string. Not once, but four times. The string is a method argument, meaning that it will be supplied as an input.
Comments (240)
Lisa Marie Level 2 Stockholm Sweden
1 May 2021
Very strange description of this method call.
Josh Werner Level 3 Granite City United States
18 February 2021
Okay, fresh noob here. I was able to figure out the solution based on the comments, but I'd like someone to clarify what I just did. public static void print(String s) >This is a class that we have named "print", right? and (String s) is calling the previous class and its data, and declaring it as a variable (s)? >Then the command line that follows is telling the compiler what to do with the data from the main class? Thanks!
Barbara Fu Level 2 Seoul
26 January 2021
Gave up and looked at the solution. How does it know which line is "String s"?
AFRASIABI AIDIN Level 2 Rochester United States
20 January 2021
I'm all out of dark energy and there is no other lessons for me to do. I can't spend money please help.
envy Level 6 Detroit United States
4 January 2021
I got the solution. But calling sys out to print multiple times doesnt seem to be optimized. Also, I cant find hints to understand how to get this optimized ?
Roberto Gallardo Level 2 Zurich Switzerland
29 November 2020
I finally completed this, but still can´t understand some things. Where was this taught (String[] args)? What is "args"? I´m moving on. hopefully I get it later on.
OFPQuestor Level 2 Little Cowarne United Kingdom
14 October 2020
Following the Light side of the Force, ran out of Dark Matter. States that I must complete tasks in order to gain additional Dark Matter. All available tasks are from the Dark Side of the Force. Is this where the Light Side of the Force ends and we must “Pay the Price” to convert to the Dark Side?
Dinesh Level 6 Delhi India
14 October 2020
I tried to execute FileCopy But the file exists why this error than
Java_Mooood Level 3 San Antonio United States
7 October 2020
Very confusing , but its simple No need for loop , just println
John Raets Level 3 Not in list Canada
16 September 2020
How do you display text on screen without using print or println?