LoginSignup
1
0

More than 1 year has passed since last update.

【Java入門】提出課題や基礎研修資料にありがちなコード一覧

Posted at

開発環境

paiza.io

課題サンプル

1.繰り返し構文と条件分岐構文

繰り返し構文と条件分岐構文を用いて、下記出力を実現せよ。なお、繰り返し構文と条件分岐構文の利用は1回とは限らない。
<出力結果>
00 01 02 03 04 05 06 07 08 09
10 00 12 13 14 15 16 17 18 19
20 21 00 23 24 25 26 27 28 29
30 31 32 00 34 35 36 37 38 39
40 41 42 43 00 45 46 47 48 49
50 51 52 53 54 00 56 57 58 59
60 61 62 63 64 65 00 67 68 69
70 71 72 73 74 75 76 00 78 79
80 81 82 83 84 85 86 87 00 89
90 91 92 93 94 95 96 97 98 00

2.総合課題

作成したインターフェースが設定されているクラス内にある集計メソッドに、複数のPersonクラスインスタンスを格納したリストを引数で受け取る。受け取ったPersonクラスインスタンスリスト内の変数値(例えば年齢)を集計する処理を作成し、集計値を戻り値とする。
戻した値を出力する。
集計した値を用い、グラフ出力メソッド内でグラフを表す文字列を出力する。

・出力の例
<集計結果>
0~9歳:21人
10歳台:14人
20歳台:15人

<グラフ>
0~9歳:■■■■■■■■■■■■■■■■■■■■■
10歳台:■■■■■■■■■■■■■■
20歳台:■■■■■■■■■■■■■■■

サンプルコード

1.繰り返し構文と条件分岐構文

Main.java
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        for(int i = 0; i < 10; i++){
            for(int j = 0; j < 10; j++){
                //条件分岐
                if(i == j){
                    System.out.print("00");
                    System.out.print(" ");
                }else{
                    System.out.print(i);
                    System.out.print(j);
                    System.out.print(" ");
                }
            }
            //改行
            System.out.println();
        }
    }
}

出力結果には、実務的でないパズル的な要素が含まれているが、それぞれのfor文がどのように繰り返されるかを理解しやすい課題。

2.総合課題

Main.java
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        
        //戻り値を受け取る
        ArrayList<Person> persons = addArray();
        //インスタンス
        Bar b = new Bar();
        //totalメソッドにpersonsを渡す
        b.total(persons);
    }
    
    private static ArrayList<Person> addArray(){
        
        //インスタンス
        ArrayList<Person> personList = new ArrayList<Person>();
        Random ran = new java.util.Random();
        //乱数を格納する
        for(int i = 0; i < 10; i++){
            Person person = new Person();
            int num = ran.nextInt(101);
            person.setOld(num);
            personList.add(person);
        }
        //戻り値
        return personList;
    }
}
Person.java
import java.util.*;
//人情報クラス
public class Person {
    
    private int old = 0;
    //setter,getter
    public void setOld(int age){
        this.old = age;
    }
    public int getOld(){
        return this.old;
    }
}
Chart.java
import java.util.*;
//インターフェース
interface Chart {
    //集計メソッド
    public void total(ArrayList<Person> list);
    //出力メソッド
    public void graph(int x, int y, int z);
}
Bar.java
import java.util.*;
//インターフェース実装
public class Bar implements Chart {
    //集計メソッド
    public void total(ArrayList<Person> list){
        
        int count1 = 0, count2 = 0, count3 = 0;
        //for文でクラス型リストからオブジェクトを取得
        for(int i = 0;i < list.size(); i++){
            Person p = list.get(i);
            int value = p.getOld();
            if(value < 10){
                count1++;
            }else if(value >= 10 && value < 20){
                count2++;
            }else{
                count3++;
            }
        } 
        //出力
        System.out.println("<集計結果>");
        System.out.println("0~9歳:" + count1 + "人");
        System.out.println("10~19歳:" + count2 + "人");
        System.out.println("それ以上:" + count3 + "人");
        //出力メソッド
        graph(count1, count2, count3);
    }
    //出力メソッド
    public void graph(int x, int y, int z){
        System.out.println("<グラフ>");
        String[] res = {"0~9歳:","10~19歳:", "それ以上:"};
        for(int i = 0; i < res.length; i++){
            System.out.print(res[i]);
            if(i == 0){
                printGraph(x);
            }else if(i == 1){
                printGraph(y);    
            }else{
                printGraph(z);
            }
        }
    }
    
    //■を出力する共通処理
    private void printGraph(int cnt){
        String str = "■ ";
        //グラフの出力
        for(int i = 0; i < cnt; i++){
            System.out.print(str);
        }
        System.out.println();
    }
}

Javaの基礎的な内容が凝縮された課題。初学者が躓きがちな、戻り値やインターフェースの必要性を理解するのに適した内容。
他に何かあれば追記予定。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0