金子邦彦研究室プログラミングJava プログラミングの基本(JavaTutor, Paiza.IO, Online GDB を使用)(スライド資料とプログラム例)(全17回)

Java プログラミングの基本(JavaTutor, Paiza.IO, Online GDB を使用)(スライド資料とプログラム例)(全17回)

大学で使用した自作の資料等を,手直しの上公開している. クリエイティブ・コモンズ BY NC SA.

JavaTutor, Paiza.IO, OnlineGDB を用いた演習を交えて,Java の基本を学ぶ.

目次

  1. pi-1. プログラミング入門 [PDF], [パワーポイント]

    トピックス:プログラミング, Java Tutor での Java プログラム実行, GDB online での Java プログラム実行, 計算誤差, さまざまなプログラミング言語

  2. pi-2. Java プログラミングの基本 [PDF], [パワーポイント]

    トピックス:オブジェクトとメソッド, 引数, 代入, データの種類, 制御

  3. pi-3. 式の抽象化とメソッド [PDF], [パワーポイント]

    トピックス:式, 変数, 式の抽象化とメソッド, メソッド呼び出し

  4. pi-4. 条件分岐, ステップ実行 [PDF], [パワーポイント]

    トピックス:条件分岐, if, else, ステップ実行

  5. pi-5. コレクション, リスト, マップ [PDF], [パワーポイント]

    トピックス:コレクション, リスト, ArrayList、マップ, HashMap

  6. pi-6. 繰り返し(ループ) [PDF], [パワーポイント]

    トピックス:繰り返し(ループ), for, ステップ実行, 拡張 for 文, リスト

  7. pi-7. クラス, メソッド, オブジェクト生成(コンストラクタ) [PDF], [パワーポイント]

    トピックス:クラス, class, メソッド, コンストラクタ, new, this

  8. pi-8. クラス設計 [PDF], [パワーポイント]

    トピックス:クラス設計, オブジェクトの状態と状態変化, メソッド内でのみ使用する変数, 抽象化の組み合わせ

  9. pi-9. スーパークラス, サブクラス, 継承 [PDF], [パワーポイント]

    トピックス:スーパークラス, サブクラス, extends, super, 継承

  10. pi-10. コレクション, ジェネリクス [PDF], [パワーポイント]

    トピックス:コレクション, 基本データ型, ジェネリクス

  11. pi-11. 多相性, インターフェイス, デザインパターン [PDF], [パワーポイント]

    トピックス:クラス階層, 多相性, インターフェイス, デザインパターン

  12. pi-12. 時間, スリープ, 疑似乱数, タイマー [PDF], [パワーポイント]

    トピックス:Java の標準ライブラリ, 時間, スリープ, 疑似乱数, マルチスレッド, タイマー

  13. pi-13. 今までの総まとめ [PDF], [パワーポイント]

    トピックス:メソッド, クラス, スーパークラス, サブクラス, 継承, クラスの抽象化, Java プログラム例

  14. pi-14. イベント, イベントハンドラ, ソケット通信 [PDF], [パワーポイント]

    トピックス:イベント, イベントハンドラ, タイマーイベント, ソケット通信

  15. pi-15. カプセル化, MVCモデル, オブジェクトのマッピング [PDF], [パワーポイント]

    トピックス:カプセル化, MVC モデル, MVC モデルの応用, オブジェクトのマッピング

  16. pi-16. プログラムのテスト, アサーション, 例外処理 [PDF], [パワーポイント]

    トピックス:プログラムの設計レシピ, 種々のエラー, プログラムのテスト, アサーション, 例外処理

  17. pi-17. プログラム設計 [PDF], [パワーポイント]

    トピックス:クラス定義, クラス階層, 継承, UML のクラス図

YouTube 再生リスト「Java の基本」:

https://youtube.com/playlist?list=PLwoDcGBEg9WH6D0fsLu3M53VahHRr-cRx

YouTube のチャンネル「金子邦彦」
https://youtube.com/user/kunihikokaneko

関連するオンラインサービス

サイト内のJava 関連ページ

1. プログラミング入門

https://www.slideshare.net/kunihikokaneko1/pi1-255592621

資料:pi-1. プログラミング入門 [PDF], [パワーポイント]

トピックス:プログラミング, Java Tutor での Java プログラム実行, GDB online での Java プログラム実行, 計算誤差, さまざまなプログラミング言語

ソースコード

1-5

public class YourClassNameHere {
    public static void main(String[] args) {
        System.out.println(1.0/3.0);
    }
}

1-5

public class YourClassNameHere {
    public static void main(String[] args) {
        System.out.println(3 * 1.1);
    }
}

2. Java プログラミングの基本

SlideShare: https://www.slideshare.net/kunihikokaneko1/pi2-java

資料:pi-2. Java プログラミングの基本 [PDF], [パワーポイント]

トピックス:オブジェクトとメソッド, 引数, 代入, データの種類, 制御

全体まとめ

ソースコード

2-2

public class Main {
    public static void main(String[] args)  {
        int x = 100;
    }
}
public class Main {
    public static void main(String[] args)  {
        int x = 100;
        String s = "abc";
    }
}

3. 式の抽象化とメソッド

SlideShare: https://www.slideshare.net/kunihikokaneko1/pi3-255592644

資料:pi-3. 式の抽象化とメソッド [PDF], [パワーポイント]

トピックス:式, 変数, 式の抽象化とメソッド, メソッド呼び出し

全体まとめ

ソースコード

3-1

public class YourClassNameHere {
    public static void main(String[] args) {
        int x = 100;
        int y = 200;
        System.out.println(x + y);
    }
}
public class YourClassNameHere {
    public static void main(String[] args) {
        int x = 100;
        int y = 200;
        System.out.println(x * y);
    }
}
public class YourClassNameHere {
    public static void main(String[] args) {
        int x = 100;
        int y = 200;
        System.out.println((x + 10) * y);
    }
}
public class YourClassNameHere {
    public static void main(String[] args) {
        double teihen = 2.5;
        double takasa = 5;
        System.out.println(teihen * takasa / 2);
    }
}

3-2

public class YourClassNameHere {
    public static double foo(double a) {
        return a * 1.1;
    }
    public static void main(String[] args) {
        System.out.println(foo(100));
        System.out.println(foo(150));
        System.out.println(foo(400));
    }
}

4. 条件分岐, ステップ実行

SlideShare: https://www.slideshare.net/kunihikokaneko1/pi4-255592657

資料:pi-4. 条件分岐, ステップ実行 [PDF], [パワーポイント]

トピックス:条件分岐, if, else, ステップ実行

全体まとめ

ソースコード

4-1

public class YourClassNameHere {
    public static void main(String[] args) {
        int age = 10;
        if (age <= 12) {
            System.out.println("500 yen");
        } else {
            System.out.println("1200 yen");
        }
    }
}

5. コレクション,リスト,マップ

SlideShare: https://www.slideshare.net/kunihikokaneko1/pi5-255592811

資料:pi-5. コレクション,リスト,マップ [PDF], [パワーポイント]

トピックス:コレクション, リスト, ArrayList、マップ, HashMap

全体まとめ

資料中のソースコード

5-1

import java.util.ArrayList;

public class YourClassNameHere {
    public static void main(String[] args) {
        ArrayList m = new ArrayList();
        m.add("15");
        m.add("8");
        m.add("6");
        m.add("32");
        m.add("23");
        System.out.println(m.size());
    }
}

5-3

import java.util.HashMap;

public class YourClassNameHere {
    public static void main(String[] args) {
        HashMap x = new HashMap();
        x.put(1, "Red");
        x.put(2, "Yellow");
        x.put(3, "Blue");
        System.out.println(x.size());
    }
}

6. 繰り返し(ループ)

SlideShare: https://www.slideshare.net/kunihikokaneko1/pi6

資料:pi-6. 繰り返し(ループ) [PDF], [パワーポイント]

トピックス:繰り返し(ループ), for, ステップ実行, 拡張 for 文, リスト

資料中のソースコード

6-1

public class YourClassNameHere {
    public static void main(String[] args) {
        int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        System.out.println(days[9]);
    }
}

6-1

public class YourClassNameHere {
    public static void main(String[] args) {
        double x[] = {10, 20, 30, 40};
        int i;
        for(i = 0; i <= 3; i++) {
            System.out.println(x[i]);
        }
    }
}

6-1

public class YourClassNameHere {
    public static void main(String[] args) {
        double x[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int i;
        for(i = 0; i <= 10; i++) {
            System.out.println((9.8 / 2) * x[i] * x[i]);
        }
    }
}

6-1

public class YourClassNameHere {
    public static void main(String[] args) {
        double x[] = {8, 6, 4, 2, 3};
        double y[] = {0, 0, 0, 0, 0};
        int i;
        for(i = 0; i <= 4; i++) {
            y[i] = x[i] * 1.1;
        }
    }
}

6-2

import java.util.ArrayList;

public class YourClassNameHere {
    public static void main(String[] args) {
        ArrayList m = new ArrayList();
        m.add("15");
        m.add("8");
        m.add("6");
        m.add("32");
        m.add("23");
        for(String s: m) {
            System.out.println(s);
        }
    }
}

7. クラス,メソッド,オブジェクト生成(コンストラクタ)

SlideShare: https://www.slideshare.net/kunihikokaneko1/pi7

資料:pi-7. クラス,メソッド,オブジェクト生成(コンストラクタ) [PDF], [パワーポイント]

トピックス:クラス, class, メソッド, コンストラクタ, new, this

ソースコード

7-2

class Ball {
    double x;
    double y;
    double r;
    String color;
    public Ball(double x, double y, double r, String color) {
        this.x = x;
        this.y = y;
        this.r = r;
        this.color = color;
    }
    public void printout() {
        System.out.println(this.x);
        System.out.println(this.y);
        System.out.println(this.r);
        System.out.println(this.color);
    }
}

public class YourClassNameHere {
    public static void main(String[] args) {
        Ball a = new Ball(8, 10, 1, "blue");
        Ball b = new Ball(2, 4, 3, "green");
        a.printout();
        b.printout();
    }
}

7-3

class Ball {
    double x;
    double y;
    double r;
    String color;
    public Ball(double x, double y, double r, String color) {
        this.x = x;
        this.y = y;
        this.r = r;
        this.color = color;
    }
    public void printout() {
        System.out.println(this.x);
        System.out.println(this.y);
        System.out.println(this.r);
        System.out.println(this.color);
    }
    public double dist() {
        return this.x + this.y;
    }
}

public class YourClassNameHere {
    public static void main(String[] args) {
        Ball a = new Ball(8, 10, 1, "blue");
        Ball b = new Ball(2, 4, 3, "green");
        a.printout();
        b.printout();
        System.out.println(a.dist());
    }
}

7-4

class Ball {
    double x;
    double y;
    double r;
    String color;
    public Ball(double x, double y, double r, String color) {
        this.x = x;
        this.y = y;
        this.r = r;
        this.color = color;
    }
    public void printout() {
        System.out.println(this.x);
        System.out.println(this.y);
        System.out.println(this.r);
        System.out.println(this.color);
    }
    public double dist() {
        return this.x + this.y;
    }
    public void move(double xx, double yy) {
        this.x = this.x + xx;
        this.y = this.y + yy;
    }
}

public class YourClassNameHere {
    public static void main(String[] args) {
        Ball a = new Ball(8, 10, 1, "blue");
        Ball b = new Ball(2, 4, 3, "green");
        a.move(5, 5);
        b.move(10, 10);
        a.printout();
        b.printout();
        System.out.println(a.dist());
    }
}

7-4

class Ball {
    double x;
    double y;
    double r;
    String color;
    public Ball(double x, double y, double r, String color) {
        this.x = x;
        this.y = y;
        this.r = r;
        this.color = color;
    }
    public void printout() {
        System.out.println(this.x);
        System.out.println(this.y);
        System.out.println(this.r);
        System.out.println(this.color);
    }
    public double dist() {
        return this.x + this.y;
    }
    public void move(double xx, double yy) {
        this.x = this.x + xx;
        this.y = this.y + yy;
    }
    public void right(double xx) {
        this.move(xx, 0);
    }
    public void left(double xx) {
        this.move(-xx, 0);
    }
}

public class YourClassNameHere {
    public static void main(String[] args) {
        Ball a = new Ball(8, 10, 1, "blue");
        Ball b = new Ball(2, 4, 3, "green");
        a.move(5, 5);
        b.move(10, 10);
        a.left(5);
        b.right(10);
        a.printout();
        b.printout();
        System.out.println(a.dist());
    }
}

8. クラス設計

SlideShare: https://www.slideshare.net/kunihikokaneko1/pi8

資料:pi-8. クラス設計 [PDF], [パワーポイント]

トピックス:クラス設計, オブジェクトの状態と状態変化, メソッド内でのみ使用する変数, 抽象化の組み合わせ

class Signal {
    String color;
    public Signal(String color) {
        this.color = color;
    }
}
public class YourClassNameHere {
    public static void main(String [] args) {
        Signal s = new Signal("red");
    }
}

8-1 2つめ

class Signal {
    String color;
    public Signal() {};
    public void red() { this.color = "red"; };
    public void yellow() { this.color = "yellow"; };
    public void blue() { this.color = "blue"; };
}
public class YourClassNameHere {
    public static void main(String [] args) {
        Signal s = new Signal();
        s.red();
    }
}

8-2

class Signal {
    String color;
    public Signal() {};
    public void red() { this.color = "red"; };
    public void yellow() { this.color = "yellow"; };
    public void blue() { this.color = "blue"; };
    public void go() {
        if (this.color.equals("blue")) { this.yellow(); }
        else if (this.color.equals("yellow")) { this.red(); }
        else if (this.color.equals("red")) { this.blue(); }
    }
}
public class YourClassNameHere {
    public static void main(String [] args) {
        Signal s = new Signal();
        s.red();
        s.go();
    }
}

8-3

class Product {
    String s;
    public Product() {};
    public void active() { this.s = "active"; };
    public void deactive() { this.s = "deactive"; };
    public void on() {
        if (this.s.equals("deactive")) { this.active(); }
    }
    public void off() {
        if (this.s.equals("active")) { this.deactive(); }
    }
}
public class YourClassNameHere {
    public static void main(String [] args) {
        Product p = new Product();
        p.deactive();
        p.on();
    }
}

8-4

public class YourClassNameHere {
    public static double foo(double a) {
      return a * 1.1;
    }
    public static void main(String [] args) {
      double p;
      p = 120;
      System.out.printf("%f\n", foo(p));
      p = 200;
      System.out.printf("%f\n", foo(p));
    }
}

8-5

public class YourClassNameHere {
    public static double x(double a) {
      return a * 1.1;
    }
    public static double y(double b) {
        return x(b * 100);
    }
    public static double z(double c) {
        return x(c * 200);
    }
    public static void main(String [] args) {
        System.out.printf("%f\n", y(5));
        System.out.printf("%f\n", y(12));
        System.out.printf("%f\n", z(8));
        System.out.printf("%f\n", z(16));                        
    }
}

9. スーパークラス,サブクラス,継承

SlideShare: https://www.slideshare.net/kunihikokaneko1/pi9

資料:pi-9. スーパークラス,サブクラス,継承 [PDF], [パワーポイント]

トピックス:スーパークラス, サブクラス, extends, super, 継承

10. コレクション,ジェネリクス

\

SlideShare: https://www.slideshare.net/kunihikokaneko1/pi10

資料:pi-10. コレクション,ジェネリクス [PDF], [パワーポイント]

トピックス:コレクション, 基本データ型, ジェネリクス

11. 多相性,インターフェイス,デザインパターン

SlideShare: https://www.slideshare.net/kunihikokaneko1/pi11

資料:pi-11. 多相性,インターフェイス,デザインパターン [PDF], [パワーポイント]

トピックス:クラス階層, 多相性, インターフェイス, デザインパターン

12. 時間,スリープ,疑似乱数,タイマー

SlideShare: https://www.slideshare.net/kunihikokaneko1/pi12

資料:pi-12. 時間,スリープ,疑似乱数,タイマー [PDF], [パワーポイント]

トピックス:Java の標準ライブラリ, 時間, スリープ, 疑似乱数, マルチスレッド, タイマー

13. 今までの総まとめ

SlideShare: https://www.slideshare.net/kunihikokaneko1/pi13

資料:pi-13. 今までの総まとめ [PDF], [パワーポイント]

トピックス:メソッド, クラス, スーパークラス, サブクラス, 継承, クラスの抽象化, Java プログラム例

14. イベント,イベントハンドラ,ソケット通信

SlideShare: https://www.slideshare.net/kunihikokaneko1/pi14-255593014/a>

資料:pi-14. イベント,イベントハンドラ,ソケット通信 [PDF], [パワーポイント]

トピックス:イベント, イベントハンドラ, タイマーイベント, ソケット通信

15. カプセル化,MVCモデル,オブジェクトのマッピング

SlideShare: https://www.slideshare.net/kunihikokaneko1/pi15-mvc

資料:pi-15. カプセル化,MVCモデル,オブジェクトのマッピング [PDF], [パワーポイント]

トピックス:カプセル化, MVC モデル, MVC モデルの応用, オブジェクトのマッピング

16. プログラムのテスト,アサーション,例外処理

SlideShare: https://www.slideshare.net/kunihikokaneko1/pi16

資料:pi-16. プログラムのテスト,アサーション,例外処理 [PDF], [パワーポイント]

トピックス:プログラムの設計レシピ, 種々のエラー, プログラムのテスト, アサーション, 例外処理

17. プログラム設計

SlideShare: https://www.slideshare.net/kunihikokaneko1/pi17

資料:pi-17. プログラム設計 [PDF], [パワーポイント]

トピックス:クラス定義, クラス階層, 継承, UML のクラス図