Programming with Java, part 1

記事
IT・テクノロジー
A happy new year! I look forward to working with you
again this year. I hope I can do the following things this year.

■Plans
・Programming with Java
・I will try to make a Big or Small game with Java.
・Measures for the Fundamental Information Technology
 Engineer Examination (around March)

In summary, I will introduce programming with Java
little by little from today. First, take a look at the
sample program.

----------------------------- Sample.java ----------------------------------------
public class Sample {
    public static void main(String[] args) throws Exception {
        int x = 6;
        int y = 4;
        int z = x * y / 2;
        System.out.println(z);
    }
}
----------------------------------------------------------------------------------------

The first line says "public class Sample". In Java,
write the class name like this. Since the class name and
the file name are the same, the file name is "Sample.java".
A class is described the process for executing a program.
We will write the processes, such as "This class will do
this kind of processing".

In Sample.java, variables x, y, z are declared as int type.
Variables are like boxes for storing values such as letters
and numbers. This box is declared as an int type.
Variables declared as int type can be treated as numbers.

"System.out.println (z);" is a process to output the value
of the variable z to the screen. You can see what is registered
in the variable z. The variable z is the calculated value of
"x ✕ y ÷ 2", so 12 is output.

Then, how about improving the Sample class as follows?

----------------------------- Triangle.java ----------------------------------------
public class Triangle {
    public static void main(String[] args) throws Exception {
        int iBottom = 6;
        int iHeight = 4;
        // Calculate the area of the triangle.
        int iArea = iBottom * iHeight / 2;
        System.out.println(iArea);
    }
}
----------------------------------------------------------------------------------------

I changed the Sample class to the Triangle class.
Since it is difficult to understand what a program does with
the Sample class, I think that the class name should be a name
that grasps the main points of the program.
Variables can also be named freely. It was x, y, z earlier, but
if you use iBottom or iHeight, you can see that it is a variable
that represents the base and height of the triangle, so the
program will be easier to read. The "i" was added to the
beginning of the variable name so that the name can tell that
this variable is an int type. This is not a rule. If the customer
requests software development, you may make program
according to the naming rules.

Next, about the part of "// Calculate the area of the triangle."
This is a comment to leave in the program. You can freely
comment on what this process means, what role this variable
has, and so on. If you add "//" at the beginning, the computer
treats the rest of the line as a comment and does not affect
the execution of the program.


Next time, I'll improve this Triangle class even more.


[Japanese]
明けましておめでとうございます。今年もよろしくお願いします。
今年は、以下のようなことをやっていきたいと思っています。

■予定
・Javaのプログラミング
・Big or Small ゲームをJavaで作ってみる
・基本情報技術者試験の午後対策(3月頃)

このように、今日からJavaのプログラミングについて少しずつ触れていきます。まずは下記のサンプルプログラムを見てください。

----------------------------- Sample.java ----------------------------------------
public class Sample {
    public static void main(String[] args) throws Exception {
        int x = 6;
        int y = 4;
        int z = x * y / 2;
        System.out.println(z);
    }
}
----------------------------------------------------------------------------------------

1行目に「public class Sample」とあるように、Javaではクラス名を記述します。ファイル名は「Sample.java」のように、クラス名とファイル名は同じにしましょう。クラスとは、プログラムを実行するための処理をまとめたものです。「このクラスでは、こんな処理をするよ」というように、処理内容を私達が書いていく(= プログラミング)ことになります。

Sample.javaでは、int型で変数x、y、zを宣言しています。変数は、文字や数値などの値を入れておくための箱のようなものです。この箱がint型で宣言されています。int型で宣言した変数は、数値として扱うことができます。

「System.out.println(z);」は、変数zの値を画面に出力する処理です。変数zに何が登録されているのかを知ることができます。変数zは、x ✕ y ÷ 2を計算した値ですので、12が出力されます。

それではSampleクラスを下記のように改良してみるとどうでしょうか?

----------------------------- Triangle.java ----------------------------------------
public class Triangle {
    public static void main(String[] args) throws Exception {
        int iBottom = 6;
        int iHeight = 4;
        // Calculate the area of the triangle.
        int iArea = iBottom * iHeight / 2;
        System.out.println(iArea);
    }
}
----------------------------------------------------------------------------------------

SampleクラスをTriangleクラスに変更しました。Sampleクラスだと何をするプログラムなのか分かりにくいので、クラス名はそのプログラムの要点をつかむ名称が良いと思います。
また、変数も自由に名前がつけられます。先程はx、y、zでしたが、iBottom
やiHeightにすれば三角形の底辺や高さを表す変数だと分かるので、プログラムが読みやすくなります。変数名の先頭に「i」とつけましたが、この変数はint型だと名前から分かるようにつけてみました。これがルールというわけではないです。もしお客様からソフトウェア開発を依頼された場合、命名ルールを提示されたときはそれに従ってプログラミングすることもあります。

続いて、「// Calculate the area of the triangle.」の部分についてです。これは、プログラムに残しておくコメントです。どんな処理をしているのか、この変数にはどんな役割があるのかなどを、自由にコメントできます。先頭に「//」を付けると、コンピュータはその行のそれ以降の部分をコメントとして扱いますので、プログラムの実行に影響を及ぼしません。


次回はこのTriangleクラスをさらに改良していきます。
サービス数40万件のスキルマーケット、あなたにぴったりのサービスを探す