このブログを検索

2019年9月15日日曜日

アホ男子のための 学習プログラム (作成中) ④

「アホ男子のための 学習プログラム」

 1つのボタンを「回答」と「次の問題」に変更をして
 それぞれの機能を持たせてみた。


⭐️ ボタンの機能の変更
//◯問題を表示させるためのclassを作成
//◯問題を表示させる
//◯ボタンを反応させる
//◯合っているかどうかの確認
//回答後ボタンを変える
import java.awt.*;
import java.awt.event.*;
class ButtonNew2{
    public static void main(String[] args){
        MyWindow mw = new MyWindow();
    }
}

class Prob{ //このクラスで問題(String)と答え(int)を代入する。
    static int count; //いくつかの問題を出す為(確認用 count)
    String problem;
    int ans;
    Prob(String problem, int ans){
        this.problem = problem;
        this.ans = ans;
        count++;
    }
}

class MyWindow extends Frame implements ActionListener{
    Prob pro1;
    TextField tf;
    Button btn;
    int bottoncount = 1; //1→回答 2→次の問題 3→最後
    MyWindow(){
        pro1 = new Prob("鎌倉幕府の成立は", 1185);
        setTitle("問題集");
        setSize(640, 400);
        setLayout(null); //レイアウトの解除
        tf = new TextField(20); //テキストフィールドのインスタンスの作成
        tf.setBounds(80, 300, 370, 30); //x,t 大きさx,y
        add(tf); //テキストフィールドの配置
        btn = new Button(); //ボタンのインスタンスの作成
        btn.addActionListener(this);
        btn.setLabel("回答");
        btn.setBounds(480, 300, 100, 35); //x,t 大きさx,y
        add(btn);//ボタンの配置
        setVisible(true);
        addWindowListener( new WinListener() );
    }
    
    public void paint(Graphics g){
        Font font1 = new Font("MS Pゴシック",Font.PLAIN,35);
        g.setFont(font1);
        g.setColor(Color.DARK_GRAY);
        g.drawString(pro1.problem, 80, 130);
    }
    
    public void actionPerformed(ActionEvent e){
        if(e.getSource() == btn) judgment();
    }
    
    void judgment(){
        if(bottoncount == 1){
            String str = tf.getText();
            int num = 0;
            try{
                num = Integer.parseInt(str);
            }
            catch(Exception e){
                System.out.println(e); //後に消去
            }
            System.out.println(num); //試験用 入力した数値を 表示
            if (pro1.ans == num){
                stop(2000);
                tf.setText(num + "  正解だよ!!");
            }
            else if(num == 0){ //numに数値が入力されなかった場合。
                stop(2000);
                tf.setText(str + " 半角数値で入力してね。");
            }
            else{
                stop(2000);
                tf.setText(num + " はまちがい 正解は " + pro1.ans + " だよ。");
            }
            btn.setLabel("次の問題");
            bottoncount = 2;
        }
        else if(bottoncount == 2){
            tf.setText("");
            btn.setLabel("回答");
            bottoncount = 1;
        }
    }
   
    public void stop(int times){ //時間を止めるメソッド
        try{ Thread.sleep(times); } catch(Exception e) {}
    }
}

class WinListener extends WindowAdapter{
    public void windowClosing(WindowEvent e){System.exit(0);}

}


次にやる事は、問題をランダムに出題する事なんだけど、
構造を結構変えないといけないかもしれないな。

今日の教訓
 こういうプログラムを作る際には、
 事前にもっと考えないといけないな。

2019年9月14日土曜日

アホ男子のための 学習プログラム (作成中) ③

「アホ男子のための 学習プログラム」

 正誤をテキストフィールドに表示。
 スレッドを少しだけ止めるメソッドを使うことにより、
 タイムラグを作りました。



 
⭐️ テキストフィールドに 正誤を表示。

//◯問題を表示させるためのclassを作成
//◯問題を表示させる
//◯ボタンを反応させる
//◯合っているかどうかの確認
import java.awt.*;
import java.awt.event.*;
class Awt8{
    public static void main(String[] args){
        MyWindow mw = new MyWindow();
    }
}

class Prob{ //このクラスで問題(String)と答え(int)を代入する。
    static int count; //いくつかの問題を出す為(確認用 count)
    String problem;
    int ans;
    Prob(String problem, int ans){
        this.problem = problem;
        this.ans = ans;
        count++;
    }
}

class MyWindow extends Frame implements ActionListener{
    Prob pro1;
    TextField tf;
    Button btn;
    MyWindow(){
        pro1 = new Prob("鎌倉幕府の成立は", 1185);
        setTitle("問題集");
        setSize(640, 400);
        setLayout(null); //レイアウトの解除
        tf = new TextField(20); //テキストフィールドのインスタンスの作成
        tf.setBounds(80, 300, 370, 30); //x,t 大きさx,y
        add(tf); //テキストフィールドの配置
        btn = new Button(); //ボタンのインスタンスの作成
        btn.addActionListener(this);
        btn.setLabel("回答");
        btn.setBounds(480, 300, 100, 35); //x,t 大きさx,y
        add(btn);//ボタンの配置
        setVisible(true);
        addWindowListener( new WinListener() );
    }
    
    public void paint(Graphics g){
        Font font1 = new Font("MS Pゴシック",Font.PLAIN,35);
        g.setFont(font1);
        g.setColor(Color.DARK_GRAY);
        g.drawString(pro1.problem, 80, 130);
    }
    
    public void actionPerformed(ActionEvent e){
        if(e.getSource() == btn) judgment();
    }
    
    void judgment(){
        String str = tf.getText();
        int num = 0;
        try{
            num = Integer.parseInt(str);
        }
        catch(Exception e){
            System.out.println(e); //後に消去
        }
        System.out.println(num); //試験用 入力した数値を 表示
        if (pro1.ans == num){
            stop(2000);
            tf.setText(num + "  正解だよ!!");
        }
        else if(num == 0){ //numに数値が入力されなかった場合。
            stop(2000);
            tf.setText(str + " 半角数値で入力してね。");
        }
        else{
            stop(2000);
            tf.setText(num + " はまちがい 正解は " + pro1.ans + " だよ。");
        }
    }
   
    public void stop(int times){ //時間を止めるメソッド
        try{ Thread.sleep(times); } catch(Exception e) {}
    }
}

class WinListener extends WindowAdapter{
    public void windowClosing(WindowEvent e){System.exit(0);}

}


次の問題を表示する際、
テキストフィールドを消去、次の問題を表示するために、
ボタンを「次の問題」に変えてみよう。

今日の教訓
 ボタンのラベルを変えて、色々な機能を持たせるためには。。。
 (考え中)

2019年9月13日金曜日

アホ男子のための 学習プログラム (作成中) ②

「アホ男子のための 学習プログラム」を作ってみた。

 とりあえず、半角数値を入力し
 合っているかどうかを確認。
 
 とりあえず ターミナルに表示。



 
⭐️ 合否の判定

//◯問題を表示させるためのclassを作成
//◯問題を表示させる
//◯ボタンを反応させる
//合っているかどうかの確認
import java.awt.*;
import java.awt.event.*;
class Awt6{
    public static void main(String[] args){
        MyWindow mw = new MyWindow();
    }
}

class Prob{ //このクラスで問題(String)と答え(int)を代入する。
    static int count; //いくつかの問題を出す為(確認用 count)
    String problem;
    int ans;
    Prob(String problem, int ans){
        this.problem = problem;
        this.ans = ans;
        count++;
    }
}

class MyWindow extends Frame implements ActionListener{
    Prob pro1;
    TextField tf;
    Button btn;
    MyWindow(){
        pro1 = new Prob("鎌倉幕府の成立は", 1185);
        setTitle("問題集");
        setSize(640, 400);
        setLayout(null); //レイアウトの解除
        tf = new TextField(20); //テキストフィールドのインスタンスの作成
        tf.setBounds(80, 300, 370, 30); //x,t 大きさx,y
        add(tf); //テキストフィールドの配置
        btn = new Button(); //ボタンのインスタンスの作成
        btn.addActionListener(this);
        btn.setLabel("回答");
        btn.setBounds(480, 300, 100, 35); //x,t 大きさx,y
        add(btn);//ボタンの配置
        setVisible(true);
        addWindowListener( new WinListener() );
    }
    
    public void paint(Graphics g){
        Font font1 = new Font("MS Pゴシック",Font.PLAIN,35);
        g.setFont(font1);
        g.setColor(Color.DARK_GRAY);
        g.drawString(pro1.problem, 80, 130);
    }
    
    public void actionPerformed(ActionEvent e){
        if(e.getSource() == btn) judgment();
    }
    
    void judgment(){
        String str = tf.getText();
        int num = 0;
        try{
            num = Integer.parseInt(str);
        }
        catch(Exception e){
            System.out.println(e); //後に消去
            tf.setText("半角数値で入力してね。");
        }
        System.out.println(num); //試験用 入力した数値を 表示
        if (pro1.ans == num){
            System.out.println("正解だよ");
        }
        else{
            System.out.println("まちがいだよ");
        }
    }
    
    
}

class WinListener extends WindowAdapter{
    public void windowClosing(WindowEvent e){System.exit(0);}

}


とりあえず String str = tf.getText();  で
テキストフィールドから 文字列を受け取ることが出来た。

今日の教訓
 前回 作った プログラムが 本当に役に立っている。

2019年9月12日木曜日

アホ男子のための 学習プログラム (作成中) ①

また、新しく 「アホ男子のための 学習プログラム」を作ってみた。
 
 今回は

 画面に表示
  ↓
 テキストフィールドに答えを入力
  ↓
 ポチっ
  ↓
 「正解!」

 というもの

 今回は位置決め のみ。





 
⭐️ 位置決め

//◯問題を表示させるためのclassを作成
//◯問題を表示させる

import java.awt.*;
import java.awt.event.*;
class Awt3{
    public static void main(String[] args){
        MyWindow mw = new MyWindow();
    }
}

class Prob{ //このクラスで問題(String)と答え(int)を代入する。
    static int count; //いくつかの問題を出す為(確認用 count)
    String problem;
    int ans;
    Prob(String problem, int ans){
        this.problem = problem;
        this.ans = ans;
        count++;
    }
}

class MyWindow extends Frame{
    Prob pro1;
    MyWindow(){
        pro1 = new Prob("鎌倉幕府の成立は", 1185);
        setTitle("問題集");
        setSize(640, 400);
        setLayout(null); //レイアウトの解除
        TextField tf = new TextField(20); //テキストフィールドのインスタンスの作成
        tf.setBounds(80, 300, 370, 30); //x,t 大きさx,y
        add(tf); //テキストフィールドの配置
        Button btn = new Button(); //ボタンのインスタンスの作成
        btn.setLabel("回答");
        btn.setBounds(480, 300, 100, 35); //x,t 大きさx,y
        add(btn);//ボタンの配置
        setVisible(true);
        addWindowListener( new WinListener() );
    }
    
    public void paint(Graphics g){
        Font font1 = new Font("MS Pゴシック",Font.PLAIN,35);
        g.setFont(font1);
        g.setColor(Color.DARK_GRAY);
        g.drawString(pro1.problem, 80, 130);
    }
}

class WinListener extends WindowAdapter{
    public void windowClosing(WindowEvent e){System.exit(0);}

}

次は ボタンの反応を確認しよう。

今日の教訓
 やっぱり 作りたいものを作る→必要な箇所を勉強する
 の流れが大切。

2019年9月11日水曜日

やさしいjava Lesson11 新しいクラス。

やさしいjava 6版 新しいクラス。
 内容は 継承、スーパークラス、サブクラス、super()、protected、
 オーバーライド、final など。

 今まで 他のテキスト「Java入門教室」、やProgateでも
 たくさん勉強した所。

 ただ、意味は 以前よりも グッと理解はできるけど
 具体的にどの様に サブクラスが使われているのか、
 まだ解らない。

 やっぱり、「Java入門教室」の最後のプログラムを
 もっと学習せねば。。。

今日の教訓
 勉強箇所が わかってきたのは すごく良いこと。

2019年9月10日火曜日

やさしいjava P299のサンプルプログラム

やさしいjava 6版
 P299の 5番のサンプルプログラムを、
 今、自分だったらこう書いてみるか〜
 という 気分で お気楽に書いてみた。

🌟サンプルプログラム

import java.io.*;
class SamplePP4{
    public static void main(String[] args){
        MyPoint[] mp = new MyPoint[3];
        for(int i=0; i<mp.length; i++){
            mp[i] = new MyPoint( rnd(150)-25, rnd(150)-25 );
        }
        for(int i=0; i<mp.length; i++){
            int ppx = input("point No," + (i+1) + "のx point を入力してください。");
            int ppy = input("point No," + (i+1) + "のy point を入力してください。");
            
            mp[i].setX(ppy, ppy);
            
        }
        for(int i=0; i<mp.length; i++){
            System.out.println((i+1) + "番目のMyPoint " + mp[i].show());
        }
    }
    
    public static int rnd(int max){ return(int)(Math.random()*max); }
    
    public static int input(String txt){
        System.out.print(txt);
        String str = null;
        BufferedReader br =
         new BufferedReader(new InputStreamReader(System.in));
        try{
            str = br.readLine();
        }
        catch(IOException e){
            System.out.println(e);
        }
        try{
            return Integer.parseInt(str);
        }
        catch(Exception e){
            System.out.println("数値以外が入力されました。50を入力します。");
            return 50;
        }
    }
}

class MyPoint{
    static int count = 0;
    private int x;
    private int y;
    public MyPoint(){
        x = 0;
        y = 0;
        count++;
        System.out.println("Point" + count + "の 初期設定を行いました x=" + this.x + " y=" + this.y);
    }
    public MyPoint(int px, int py){
        this();
        this.x = confi(px);
        this.y = confi(py);
        System.out.println("Point" + count + "の 再設定を行いました x=" + this.x + " y=" + this.y);
    }
    
    public void setX(int px){
        this.x = confi(px);
    }
    
    public void setY(int py){
        this.y = confi(py);
    }
    
    public void setX(int px, int py){ //メソッドのオーバーロード
        this.x = confi(px);
        this.y = confi(py);
    }
    
    public int getX(){
        return this.x;
    }
    
    public int getY(){
        return this.y;
    }
    
    public int confi(int num){
        if(num>=0 && num<=100){
            return num;
        }
        else{
            System.out.println("範囲外の数値が入力されました。50を入力します。");
            return 50;
        }
    }
    
    public String show(){
        return "x=" + this.x + " y=" + this.y;
    }

}


今日の教訓
 すごくゴチャゴチャしたものが完成。

2019年9月9日月曜日

アホ男子のための 割り算 問題 学習プログラム(修正)

アホ男子 の為の 割り算学習プログラム。
 
 整数の乱数を返すメソッドを使って、
 割り算プログラムを修正してみた。
 
⭐️ 割り算プログラム(乱数メソッドを使用)
//◯新しくLabelを作る。
//◯Buttonの反応部を // にする。
//◯Labelを反応させる。
//◯Lavelに正解数を表示させる。
//◯整数の乱数を返すメソッドを使用
//問題作成部と 判断部分を 他のクラスにする(他の教科でも使いやすくする)
//正解数によってコメントを変えてみる。
// 「正解数は◯問です」ではなく、⭐︎にて表示を行う。
//間違いの出題数の調整。
//コマンドライン引数で レベルの入力を行う。
//スレッドで タイマーをかける。
import java.awt.*;
import java.awt.event.*;
class Count3{
    public static void main(String[] args){
        MyWindow mw = new MyWindow();
    }
}

class MyWindow extends Frame implements ActionListener{
    Label la1, la2;
    Button btn1, btn2;
    int moto; //割られる数値です。
    int waru; //割る数値です。
    int ans; //答え
    int amari; //答え(あまり)
    static int count = 1; //出題数
    static int Correct_answer; //正解数
    MyWindow(){
        setTitle("問題集");
        setSize(640, 400);
        setLayout(null); //レイアウトの解除
        btn1 = new Button("◯");
        btn2 = new Button("×");
        btn1.setBounds(100, 300, 150, 80);
        btn2.setBounds(400, 300, 150, 80);
        btn1.addActionListener(this);
        btn2.addActionListener(this);
        add(btn1);
        add(btn2);
        la1 = new Label( questions() );
        la1.setFont(new Font("Serif", Font.PLAIN, 34));
        la1.setAlignment(Label.CENTER);
        la1.setBackground(Color.LIGHT_GRAY); //Label 1 背景色
        la1.setForeground(Color.BLACK);  //Label 文字色
        la1.setBounds(100, 100, 450, 50);
        add(la1);
        la2 = new Label("割 り 算 の テ ス ト");
        //la2 = setFont(new Font("Serif", Font.BOLD, 24));
        la2.setAlignment(Label.CENTER);
        la2.setBackground(Color.LIGHT_GRAY); //Label 2 背景色
        la2.setForeground(Color.darkGray);  //Label 2 文字色
        la2.setBounds(150, 180, 350, 50);
        add(la2);
        setVisible(true);
        addWindowListener( new WinListener() );
    }
    
    public String questions(){
        waru = rnd(29) +2; //割る数値(2〜30)
        ans = rnd(9) +1; //答え(1〜9)
        amari = rnd(waru-1) +1; //あまりの数値(1〜割る数値−1)
        moto = waru * ans + amari; //割られる数値
        System.out.println(moto + " ÷ " + waru + " = " + ans + " ... " + amari);//験算用
        btn1.setLabel("◯");
        btn2.setLabel("×");
        int ran1 = rnd(2); //間違いの発生 確率50% ただし 変数に0を加算する事もある。
        if(ran1 == 0){
            int ran2 = rnd(3);
            if(ran2 == 0){
                int falseAmari = rnd(3);
                falseAmari -= 1;
                amari = amari + falseAmari; // あまりに -1,0,1 を加算
            }
            else{
                int falsewaru = rnd(2);
                ans = ans + falsewaru; // 商に 0,1 を加算
            }
        }
            
        return moto + " ÷ " + waru + " = " + ans + " ... " + amari;
    }
    
    public boolean judgment(){
        return moto/waru==ans && moto%waru==amari; //答えと 余りの判定。booleanにて返す。
    }
    
    
    public void actionPerformed(ActionEvent e){
        if(e.getSource() == btn1) {
            if(judgment() == true){
                //btn1.setLabel("正解 !");
                la2.setText("正解だよ !");
                Correct_answer++;
                countCalculation();
            }
            else{
                //btn1.setLabel("違っているよ !");
                la2.setText("違っているよ !");
                countCalculation();
            }
        }
        if(e.getSource() == btn2) {
            if(judgment() == false){
                //btn2.setLabel("正解 !");
                la2.setText("正解だよ !");
                Correct_answer++;
                countCalculation();
            }
            else{
                //btn2.setLabel("違っているよ !");
                la2.setText("違っているよ !");
                countCalculation();
            }
        }
    }
    
    public void countCalculation(){
        try { Thread.sleep(2000); } catch (InterruptedException e) {} // 2秒間だけ処理を止める
        System.out.println(count + "回目"); //試験用
        count++;
        if(count <= 5){  //5回で終了。
            la1.setText( questions() ); //ラベルの表示の書き換え questions() メソッドへ
        }
        else{
            la1.setText("よく 頑張ったね !!");
            la2.setText("正解数は" + Correct_answer + "問です。");
            //System.out.println("お疲れ様でした。");
            //System.out.println("正解数は" + Correct_answer + "問です。");
        }
    }
    
    public static int rnd(int max) { return(int)(Math.random()*max);} //整数の乱数を返すメソッド
}

class WinListener extends WindowAdapter{
    public void windowClosing(WindowEvent e){System.exit(0);}

}

 ランダム作成する部分を一箇所にまとめたので、
 スッキリ。 

今日の教訓
 まだまだ 改良するのだ。