├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── example │ │ │ └── java2kotlinpractice │ │ │ ├── MainActivity.java │ │ │ ├── RecyclerAdapter.java │ │ │ ├── RecyclerListener.java │ │ │ ├── lesson1 │ │ │ └── Lesson1Activity.java │ │ │ ├── lesson2 │ │ │ ├── Lesson2Activity.java │ │ │ └── PokemonData.java │ │ │ ├── lesson3 │ │ │ └── Lesson3Activity.java │ │ │ ├── lesson4 │ │ │ ├── Item.kt │ │ │ ├── Lesson4.java │ │ │ ├── Lesson4Activity.java │ │ │ └── Pokemon.kt │ │ │ ├── lesson5 │ │ │ ├── Lesson5.java │ │ │ └── Lesson5Activity.java │ │ │ └── lesson6 │ │ │ ├── Lesson6.java │ │ │ └── Lesson6Activity.java │ └── res │ │ ├── layout │ │ ├── activity_lesson1.xml │ │ ├── activity_lesson2.xml │ │ ├── activity_lesson3.xml │ │ ├── activity_lesson4.xml │ │ ├── activity_lesson5.xml │ │ ├── activity_lesson6.xml │ │ ├── activity_main.xml │ │ └── recycler_row.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── example │ └── java2kotlinpractice │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | Thumbs.db 3 | 4 | .gradle 5 | /local.properties 6 | 7 | .idea 8 | !.idea/codeStyleSettings.xml 9 | *.iml 10 | 11 | /build 12 | /captures 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Agenda 2 | 3 | * Lesson1 : val, var 4 | * Lesson2 : Data class 5 | * Lesson3 : if, when, for, while 6 | * Lesson4 : Null 7 | * Lesson5 : Extension 8 | * Lesson6 : Lambda 9 | 10 | # Points 11 | 12 | * Lesson毎にKotlinに変換していきましょう 13 | * Branch 14 | - master branch -> Java 15 | - kotlin branch -> Kotlinで解答を書いています 16 | * Kotlinのコード規約は以下に従っています 17 | - https://github.com/openfresh/android-kotlin-style-guide 18 | 19 | ## Lesson1 20 | 21 | * final -> val 22 | * 型推論 23 | * 三項演算子 -> if-else 24 | 25 | ## Lesson2 26 | 27 | * data class
 28 | - PokemonDataクラスは1行になる
 29 | - Default argumentsを使う 30 | * Property access 31 | - `foo.setBar(bar)` , `foo.getBar()` -> `foo.bar = bar`, `foo.bar` 32 | * `${}` で変数にアクセス可能 33 | 34 | ## Lesson3 35 | 36 | * Javaのswitch, if-else文はwhenに置き換え可能 37 | * fibonacciは普通に変換しても良いが、
解答ではgenerateSequenceを用いて書いた(難易度高いのでやらなくて良い) 38 | 39 | ## Lesson4 40 | 41 | * 複雑なのでPokemon, Itemは予めKotlin化済み 42 | * Primitive型の引数にもnullを渡せる 43 | * smart castを使ってnon-null型にする 44 | 45 | ## Lesson5 46 | 47 | * run, let, apply(scope関数)を使って書いてみる 48 | * 他にもalso, withがある(今回は使わなくても良い) 49 | 50 | ## Lesson6 51 | 52 | * lambda式の変数はデフォルトだと `it` 53 | 
 - 名前を付けることも出来る
 `hoge { foo -> print(foo) }` 54 | * 今まで使ったscope関数, when 等を使う -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | apply plugin: 'kotlin-android' 3 | 4 | android { 5 | compileSdkVersion 26 6 | buildToolsVersion "26.0.1" 7 | defaultConfig { 8 | applicationId "com.example.java2kotlinpractice" 9 | minSdkVersion 16 10 | targetSdkVersion 26 11 | versionCode 1 12 | versionName "1.0" 13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 14 | } 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 19 | } 20 | } 21 | } 22 | 23 | dependencies { 24 | compile 'com.android.support:appcompat-v7:26.0.1' 25 | compile 'com.android.support:recyclerview-v7:26.0.1' 26 | 27 | compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" 28 | } 29 | repositories { 30 | mavenCentral() 31 | } 32 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Applications/android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/java2kotlinpractice/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.example.java2kotlinpractice; 2 | 3 | import com.example.java2kotlinpractice.lesson1.Lesson1Activity; 4 | import com.example.java2kotlinpractice.lesson2.Lesson2Activity; 5 | import com.example.java2kotlinpractice.lesson3.Lesson3Activity; 6 | import com.example.java2kotlinpractice.lesson4.Lesson4Activity; 7 | import com.example.java2kotlinpractice.lesson5.Lesson5Activity; 8 | import com.example.java2kotlinpractice.lesson6.Lesson6Activity; 9 | 10 | import android.content.Intent; 11 | import android.os.Bundle; 12 | import android.support.v7.app.AppCompatActivity; 13 | import android.support.v7.widget.DividerItemDecoration; 14 | import android.support.v7.widget.LinearLayoutManager; 15 | import android.support.v7.widget.RecyclerView; 16 | import android.view.View; 17 | 18 | import java.util.ArrayList; 19 | 20 | public class MainActivity extends AppCompatActivity { 21 | 22 | @Override 23 | protected void onCreate(Bundle savedInstanceState) { 24 | super.onCreate(savedInstanceState); 25 | setContentView(R.layout.activity_main); 26 | 27 | final ArrayList list = new ArrayList<>(); 28 | list.add("Lesson1 : val, var"); 29 | list.add("Lesson2 : data class"); 30 | list.add("Lesson3 : for, when"); 31 | list.add("Lesson4 : null"); 32 | list.add("Lesson5 : extension"); 33 | list.add("Lesson6 : lambda"); 34 | 35 | final RecyclerView recyclerView = findViewById(R.id.recyclerView); 36 | recyclerView.setLayoutManager(new LinearLayoutManager(this)); 37 | recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL)); 38 | recyclerView.setAdapter(new RecyclerAdapter(this, list, new RecyclerListener() { 39 | @Override 40 | public void onRecyclerClicked(View view, int position) { 41 | 42 | switch (position) { 43 | case 0: 44 | startActivity(new Intent(MainActivity.this, Lesson1Activity.class)); 45 | break; 46 | case 1: 47 | startActivity(new Intent(MainActivity.this, Lesson2Activity.class)); 48 | break; 49 | case 2: 50 | startActivity(new Intent(MainActivity.this, Lesson3Activity.class)); 51 | break; 52 | case 3: 53 | startActivity(new Intent(MainActivity.this, Lesson4Activity.class)); 54 | break; 55 | case 4: 56 | startActivity(new Intent(MainActivity.this, Lesson5Activity.class)); 57 | break; 58 | case 5: 59 | startActivity(new Intent(MainActivity.this, Lesson6Activity.class)); 60 | break; 61 | } 62 | } 63 | })); 64 | } 65 | } -------------------------------------------------------------------------------- /app/src/main/java/com/example/java2kotlinpractice/RecyclerAdapter.java: -------------------------------------------------------------------------------- 1 | package com.example.java2kotlinpractice; 2 | 3 | import android.content.Context; 4 | import android.support.v7.widget.RecyclerView; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.TextView; 9 | 10 | import java.util.List; 11 | 12 | public class RecyclerAdapter extends RecyclerView.Adapter { 13 | 14 | private final Context context; 15 | private List list; 16 | private RecyclerListener listener; 17 | 18 | public RecyclerAdapter(final Context context, final List list, final RecyclerListener listener) { 19 | this.context = context; 20 | this.list = list; 21 | this.listener = listener; 22 | } 23 | 24 | @Override 25 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 26 | final View view = LayoutInflater.from(context).inflate(R.layout.recycler_row, parent, false); 27 | return new ViewHolder(view); 28 | } 29 | 30 | @Override 31 | public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { 32 | ViewHolder viewHolder = (ViewHolder) holder; 33 | viewHolder.textView.setText(list.get(position)); 34 | viewHolder.itemView.setOnClickListener(new View.OnClickListener() { 35 | @Override 36 | public void onClick(View view) { 37 | listener.onRecyclerClicked(view, position); 38 | } 39 | }); 40 | } 41 | 42 | @Override 43 | public int getItemCount() { 44 | return list.size(); 45 | } 46 | 47 | class ViewHolder extends RecyclerView.ViewHolder { 48 | 49 | TextView textView; 50 | 51 | private ViewHolder(View v) { 52 | super(v); 53 | textView = (TextView) v.findViewById(R.id.text); 54 | } 55 | } 56 | } -------------------------------------------------------------------------------- /app/src/main/java/com/example/java2kotlinpractice/RecyclerListener.java: -------------------------------------------------------------------------------- 1 | package com.example.java2kotlinpractice; 2 | 3 | import android.view.View; 4 | 5 | public interface RecyclerListener { 6 | 7 | void onRecyclerClicked(View v, int position); 8 | } -------------------------------------------------------------------------------- /app/src/main/java/com/example/java2kotlinpractice/lesson1/Lesson1Activity.java: -------------------------------------------------------------------------------- 1 | package com.example.java2kotlinpractice.lesson1; 2 | 3 | import com.example.java2kotlinpractice.R; 4 | 5 | import android.os.Bundle; 6 | import android.support.annotation.Nullable; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.widget.TextView; 9 | 10 | import java.util.Random; 11 | 12 | /** 13 | * var, val 14 | * 型推論 15 | */ 16 | public class Lesson1Activity extends AppCompatActivity { 17 | 18 | @Override 19 | protected void onCreate(@Nullable Bundle savedInstanceState) { 20 | super.onCreate(savedInstanceState); 21 | setContentView(R.layout.activity_lesson1); 22 | 23 | final TextView textView = findViewById(R.id.textView); 24 | final TextView textView2 = findViewById(R.id.textView2); 25 | 26 | final Random random = new Random(); 27 | 28 | int randomValue = random.nextInt(1); 29 | final String text = randomValue == 0 ? "Java" : "Kotlin"; 30 | textView.setText(text); 31 | 32 | randomValue = random.nextInt(1); 33 | final String text2 = randomValue == 0 ? "var" : "val"; 34 | textView2.setText(text2); 35 | } 36 | } -------------------------------------------------------------------------------- /app/src/main/java/com/example/java2kotlinpractice/lesson2/Lesson2Activity.java: -------------------------------------------------------------------------------- 1 | package com.example.java2kotlinpractice.lesson2; 2 | 3 | import com.example.java2kotlinpractice.R; 4 | 5 | import android.os.Bundle; 6 | import android.support.annotation.Nullable; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.widget.TextView; 9 | 10 | /** 11 | * data class 12 | */ 13 | public class Lesson2Activity extends AppCompatActivity { 14 | 15 | @Override 16 | protected void onCreate(@Nullable Bundle savedInstanceState) { 17 | super.onCreate(savedInstanceState); 18 | setContentView(R.layout.activity_lesson2); 19 | 20 | TextView textView = findViewById(R.id.textView); 21 | TextView textView2 = findViewById(R.id.textView2); 22 | 23 | final PokemonData pikachu = new PokemonData("Pikachu", 35); 24 | final PokemonData lugia = new PokemonData("Lugia", 50, 106); 25 | 26 | textView.setText(lugia.getName() + " attacks " + pikachu.getName()); 27 | 28 | pikachu.setHp(0); 29 | textView2.setText("Pikachu HP: " + pikachu.getHp()); 30 | } 31 | } -------------------------------------------------------------------------------- /app/src/main/java/com/example/java2kotlinpractice/lesson2/PokemonData.java: -------------------------------------------------------------------------------- 1 | package com.example.java2kotlinpractice.lesson2; 2 | 3 | /** 4 | * このclassをdata classに変更してみましょう 5 | * https://kotlinlang.org/docs/reference/data-classes.html 6 | * 7 | * Hint : 物凄く短くなります 8 | */ 9 | public class PokemonData { 10 | 11 | final private String name; 12 | private int level; 13 | private int hp; 14 | 15 | public PokemonData(String name, int hp) { 16 | this(name, 5, hp); 17 | } 18 | 19 | public PokemonData(String name, int level, int hp) { 20 | this.name = name; 21 | this.level = level; 22 | this.hp = hp; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public int getLevel() { 30 | return level; 31 | } 32 | 33 | public void setLevel(int level) { 34 | this.level = level; 35 | } 36 | 37 | public int getHp() { 38 | return hp; 39 | } 40 | 41 | public void setHp(int hp) { 42 | this.hp = hp; 43 | } 44 | 45 | @Override 46 | public boolean equals(Object o) { 47 | if (this == o) { 48 | return true; 49 | } 50 | if (!(o instanceof PokemonData)) { 51 | return false; 52 | } 53 | 54 | PokemonData that = (PokemonData) o; 55 | 56 | if (level != that.level) { 57 | return false; 58 | } 59 | if (hp != that.hp) { 60 | return false; 61 | } 62 | return name != null ? name.equals(that.name) : that.name == null; 63 | 64 | } 65 | 66 | @Override 67 | public int hashCode() { 68 | int result = name != null ? name.hashCode() : 0; 69 | result = 31 * result + level; 70 | result = 31 * result + hp; 71 | return result; 72 | } 73 | } -------------------------------------------------------------------------------- /app/src/main/java/com/example/java2kotlinpractice/lesson3/Lesson3Activity.java: -------------------------------------------------------------------------------- 1 | package com.example.java2kotlinpractice.lesson3; 2 | 3 | import com.example.java2kotlinpractice.R; 4 | 5 | import android.os.Bundle; 6 | import android.support.annotation.Nullable; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.widget.TextView; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | import java.util.Random; 13 | 14 | /** 15 | * if / when / for / while 16 | */ 17 | public class Lesson3Activity extends AppCompatActivity { 18 | 19 | @Override 20 | protected void onCreate(@Nullable Bundle savedInstanceState) { 21 | super.onCreate(savedInstanceState); 22 | setContentView(R.layout.activity_lesson3); 23 | 24 | TextView textView = findViewById(R.id.textView); 25 | TextView textView2 = findViewById(R.id.textView2); 26 | 27 | /////////////////////////////////// 28 | final List fibonacciList = new ArrayList<>(); 29 | 30 | for (int i = 1; i <= 10; i++) { 31 | fibonacciList.add(fibonacci(i)); 32 | } 33 | 34 | // forEach文等が用意されている 35 | String result = ""; 36 | for (Long item : fibonacciList) { 37 | result += item + ","; 38 | } 39 | textView.setText(result); 40 | 41 | /////////////////////////////////// 42 | final Random random = new Random(); 43 | int randomValue = random.nextInt(8); 44 | 45 | // switchはwhenを使って置き換えてみましょう 46 | switch (randomValue) { 47 | case 0: 48 | result = "Vaporeon"; // シャワーズ 49 | break; 50 | case 1: 51 | result = "Jolteon"; // サンダース 52 | break; 53 | case 2: 54 | result = "Flareon"; // ブースター 55 | break; 56 | case 3: 57 | result = "Espeon"; // エーフィ 58 | break; 59 | case 4: 60 | result = "Umbreon"; // ブラッキー 61 | break; 62 | case 5: 63 | result = "Leafeon"; // リーフィア 64 | break; 65 | case 6: 66 | result = "Glaceon"; // グレイシア 67 | break; 68 | default: 69 | result = "Sylveon"; // ニンフィア 70 | break; 71 | } 72 | textView2.setText("Eevee evolves into " + result + "!"); 73 | } 74 | 75 | /** 76 | * Extra 77 | * 余裕がある人はgenerateSequenceを使って書いてみるとKotlinっぽく書ける (難易度高い) 78 | */ 79 | private static long fibonacci(int n) { 80 | if (n <= 1) { 81 | return n; 82 | } else { 83 | return fibonacci(n - 1) + fibonacci(n - 2); 84 | } 85 | } 86 | } -------------------------------------------------------------------------------- /app/src/main/java/com/example/java2kotlinpractice/lesson4/Item.kt: -------------------------------------------------------------------------------- 1 | package com.example.java2kotlinpractice.lesson4 2 | 3 | /** 4 | * ポケモンにおけるitem class 5 | * 予めKotlin化済み 6 | */ 7 | data class Item(val name: String, val description: String) -------------------------------------------------------------------------------- /app/src/main/java/com/example/java2kotlinpractice/lesson4/Lesson4.java: -------------------------------------------------------------------------------- 1 | package com.example.java2kotlinpractice.lesson4; 2 | 3 | import android.support.annotation.Nullable; 4 | 5 | /** 6 | * Lesson5でもgotchaメソッドが使われている 7 | * 自動変換するとobject classになるが、それだと他のclassから参照出来ない 8 | * 9 | * => kotlinはTop levelに関数をおける 10 | */ 11 | public class Lesson4 { 12 | 13 | /** 14 | * @param partnerPokemon null if you don't have your partner pokemon. 15 | * @return pokemon nullable. 16 | */ 17 | public static Pokemon gotcha(@Nullable Pokemon partnerPokemon) { 18 | if (partnerPokemon == null) { 19 | return new Pokemon("Pikachu", 35, 55, 40, 90); 20 | } 21 | 22 | if (partnerPokemon.getDefence() > partnerPokemon.getAttack()) { 23 | return new Pokemon("Bulbasaur", 45, 49, 49, 45); //フシギダネ 24 | } else if (partnerPokemon.getAttack() > partnerPokemon.getLevel() * 10) { 25 | return new Pokemon("Charmander", 39, 52, 43, 65); //ヒトカゲ 26 | } else if (partnerPokemon.getSpeed() > partnerPokemon.getLevel() * 3) { 27 | return new Pokemon("Squirtle", 44, 48, 65, 43); //ゼニガメ 28 | } else { 29 | return null; 30 | } 31 | } 32 | 33 | /** 34 | * Kotlin変換では引数"number"をnullableにして下さい 35 | * 36 | * @param number マイナスの場合は"Eevee"を返す 37 | * Kotlin変換後は "number"マイナスではなくnullの場合に"Eevee"にを返す 38 | * @return 引数が0~7以外の場合はnullを返す 39 | */ 40 | public static String evolveEevee(int number) { 41 | // kotlin変換では、numberがnullで"Eevee"返す 42 | if (number < 0) { 43 | return "Eevee"; 44 | } 45 | 46 | switch (number) { 47 | case 0: 48 | return "Vaporeon"; 49 | case 1: 50 | return "Jolteon"; 51 | case 2: 52 | return "Flareon"; 53 | case 3: 54 | return "Espeon"; 55 | case 4: 56 | return "Umbreon"; 57 | case 5: 58 | return "Leafeon"; 59 | case 6: 60 | return "Glaceon"; 61 | case 7: 62 | return "Sylveon"; 63 | default: 64 | return null; 65 | } 66 | } 67 | } -------------------------------------------------------------------------------- /app/src/main/java/com/example/java2kotlinpractice/lesson4/Lesson4Activity.java: -------------------------------------------------------------------------------- 1 | package com.example.java2kotlinpractice.lesson4; 2 | 3 | import com.example.java2kotlinpractice.R; 4 | 5 | import android.os.Bundle; 6 | import android.support.annotation.Nullable; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.widget.TextView; 9 | 10 | import java.util.Random; 11 | 12 | import static com.example.java2kotlinpractice.lesson4.Lesson4.evolveEevee; 13 | import static com.example.java2kotlinpractice.lesson4.Lesson4.gotcha; 14 | 15 | /** 16 | * null 17 | */ 18 | public class Lesson4Activity extends AppCompatActivity { 19 | 20 | // Lesson3までとは異なり、プロパティに定義しているのでnullableにする 21 | private TextView textView; 22 | private TextView textView2; 23 | 24 | @Override 25 | protected void onCreate(@Nullable Bundle savedInstanceState) { 26 | super.onCreate(savedInstanceState); 27 | setContentView(R.layout.activity_lesson4); 28 | 29 | textView = findViewById(R.id.textView); 30 | textView2 = findViewById(R.id.textView2); 31 | lesson4_1(); 32 | lesson4_2(); 33 | } 34 | 35 | private void lesson4_1() { 36 | final Random random = new Random(); 37 | int randomValue = random.nextInt(8); 38 | // kotlin変換後はint型にnullも渡せる 39 | String pokemon = evolveEevee(randomValue); 40 | 41 | if (pokemon != null) { 42 | textView.setText(pokemon); 43 | } 44 | } 45 | 46 | private void lesson4_2() { 47 | Pokemon newPokemon = gotcha(null); 48 | if (newPokemon != null) { 49 | textView2.setText("Gotcha! " + newPokemon.getName()); 50 | } else { 51 | textView2.setText("Darn! The pokemon broke free!"); 52 | } 53 | } 54 | } -------------------------------------------------------------------------------- /app/src/main/java/com/example/java2kotlinpractice/lesson4/Pokemon.kt: -------------------------------------------------------------------------------- 1 | package com.example.java2kotlinpractice.lesson4 2 | 3 | import android.support.annotation.IntRange 4 | import java.util.Random 5 | 6 | /** 7 | * このclassは予めKotlin化済 8 | * 一応下に変換前のJava codeもあり 9 | * 10 | * base* ポケモンにおける種族値 11 | */ 12 | 13 | class Pokemon(val name: String, baseHp: Int, baseAttack: Int, baseDefence: Int, baseSpeed: Int, val level: Int = 5) { 14 | 15 | companion object { 16 | // ポケモンにおける個体値の最大値 17 | private val INDIVIDUAL_MAX_VALUE = 31 18 | // ポケモンにおける性格補正は1倍固定 19 | private val NATURE_VALUE = 1.0f 20 | } 21 | 22 | // Java用に作成した Kotlinだとdefault引数があるので不要 23 | constructor(name: String, baseHp: Int, baseAttack: Int, baseDefence: Int, baseSpeed: Int) : 24 | this(name, baseHp, baseAttack, baseDefence, baseSpeed, 5) 25 | 26 | var hp: Int = 0 27 | set(value) { 28 | field = if (value < 0) 0 else value 29 | } 30 | val attack: Int 31 | val defence: Int 32 | val speed: Int 33 | 34 | // ポケモンにおける努力値 (各パラメータに255まで割り当てられる. 全部の努力値の合計は510までだが今回は割愛) 35 | var effortHp: Int = 0 36 | set(@IntRange(from = 0, to = 255) value) { // 自動生成されるsetter, getterも書き換えられる 37 | field = value 38 | } 39 | var effortAttack: Int = 0 40 | set(@IntRange(from = 0, to = 255) value) { 41 | field = value 42 | } 43 | var effortDefence: Int = 0 44 | set(@IntRange(from = 0, to = 255) value) { 45 | field = value 46 | } 47 | var effortSpeed: Int = 0 48 | set(@IntRange(from = 0, to = 255) value) { 49 | field = value 50 | } 51 | 52 | // ポケモンにおける持ち物 (ポケモンは持ち物を1つ持てる) 53 | var heldItem: Item? = null 54 | 55 | // ポケモンにおける個体値 (本来はパラメータ毎に異なるが今回は割愛) 56 | private val individualValue: Int 57 | 58 | // 初期化の処理をinitに書ける ≒ Javaのコンストラクタ 59 | init { 60 | individualValue = Random().nextInt(INDIVIDUAL_MAX_VALUE + 1) 61 | this.hp = ((baseHp * 2f + individualValue.toFloat() + effortHp / 4f) * (level / 100f) + (10 + level)).toInt() 62 | this.attack = calcStatus(baseAttack, individualValue, effortAttack) 63 | this.defence = calcStatus(baseDefence, individualValue, effortDefence) 64 | this.speed = calcStatus(baseSpeed, individualValue, effortSpeed) 65 | } 66 | 67 | private fun calcStatus(baseValue: Int, individualValue: Int, effortValue: Int) = 68 | (((baseValue * 2f + individualValue.toFloat() + effortValue / 4f) * (level / 100f) + 5) * NATURE_VALUE).toInt() 69 | } 70 | 71 | //public class Pokemon { 72 | // 73 | // // ポケモンにおける個体値の最大値 74 | // private static final int INDIVIDUAL_MAX_VALUE = 31; 75 | // // ポケモンにおける性格補正は1倍固定 76 | // private static final float NATURE_VALUE = 1.0f; 77 | // 78 | // final private String name; 79 | // private int level; 80 | // private int hp; 81 | // private int attack; 82 | // private int defence; 83 | // private int speed; 84 | // 85 | // // ポケモンにおける努力値 (各パラメータに255まで割り当てられる. 全部の努力値の合計は510までだが今回は割愛) 86 | // @IntRange(from = 0, to = 255) 87 | // private int effortHp; 88 | // @IntRange(from = 0, to = 255) 89 | // private int effortAttack; 90 | // @IntRange(from = 0, to = 255) 91 | // private int effortDefence; 92 | // @IntRange(from = 0, to = 255) 93 | // private int effortSpeed; 94 | // 95 | // public Pokemon(String name, int hp, int attack, int defence, int speed) { 96 | // this(name, 5, hp, attack, defence, speed); 97 | // } 98 | // 99 | // public Pokemon(String name, int level, int hp, int attack, int defence, int speed) { 100 | // this.name = name; 101 | // this.level = level; 102 | // final int individualValue = new Random().nextInt(INDIVIDUAL_MAX_VALUE + 1); 103 | // this.hp = (int) ((hp * 2f + individualValue + effortHp / 4f) * (level / 100f) + (10 + level)); 104 | // this.attack = (int) (((attack * 2f + individualValue + effortAttack / 4f) * (level / 100f) + 5) * NATURE_VALUE); 105 | // this.defence = (int) (((defence * 2f + individualValue + effortDefence / 4f) * (level / 100f) + 5) * NATURE_VALUE); 106 | // this.speed = (int) (((speed * 2f + individualValue + effortSpeed / 4f) * (level / 100f) + 5) * NATURE_VALUE); 107 | // } 108 | // 109 | // public String getName() { 110 | // return name; 111 | // } 112 | // 113 | // public int getLevel() { 114 | // return level; 115 | // } 116 | // 117 | // public int getHp() { 118 | // return hp; 119 | // } 120 | // 121 | // public void setHp(int hp) { 122 | // if (hp < 0) { 123 | // hp = 0; 124 | // } 125 | // this.hp = hp; 126 | // } 127 | // 128 | // public int getAttack() { 129 | // return attack; 130 | // } 131 | // 132 | // public int getDefence() { 133 | // return defence; 134 | // } 135 | // 136 | // public int getSpeed() { 137 | // return speed; 138 | // } 139 | // 140 | // public void setEffortHp(int effortHp) { 141 | // this.effortHp = effortHp; 142 | // } 143 | // 144 | // public void setEffortAttack(int effortAttack) { 145 | // this.effortAttack = effortAttack; 146 | // } 147 | // 148 | // public void setEffortDefence(int effortDefence) { 149 | // this.effortDefence = effortDefence; 150 | // } 151 | // 152 | // public void setEffortSpeed(int effortSpeed) { 153 | // this.effortSpeed = effortSpeed; 154 | // } 155 | //} -------------------------------------------------------------------------------- /app/src/main/java/com/example/java2kotlinpractice/lesson5/Lesson5.java: -------------------------------------------------------------------------------- 1 | package com.example.java2kotlinpractice.lesson5; 2 | 3 | import com.example.java2kotlinpractice.lesson4.Item; 4 | import com.example.java2kotlinpractice.lesson4.Pokemon; 5 | 6 | import java.util.Random; 7 | 8 | public class Lesson5 { 9 | 10 | /** 11 | * 伝説のポケモンを捕まえる 12 | * 持ち物を持っている場合がある 13 | * 14 | * @return nullableでpokemonを返す 15 | */ 16 | public static Pokemon gotchaLegend() { 17 | final Random random = new Random(); 18 | int randomValue = random.nextInt(4); 19 | 20 | Pokemon pokemon; 21 | switch (randomValue) { 22 | case 0: 23 | pokemon = new Pokemon("Moltres", 90, 100, 90, 90, 50); //ファイヤー 24 | pokemon.setHeldItem(new Item("Fire Stone", 25 | "A peculiar stone that can make certain species of Pokémon evolve. The stone has a fiery orange heart.")); //ほのおのいし 26 | break; 27 | case 1: 28 | pokemon = new Pokemon("Zapdos", 90, 90, 85, 100, 50); //サンダー 29 | pokemon.setHeldItem(new Item("Thunder Stone", 30 | "A peculiar stone that can make certain species of Pokémon evolve. It has a distinct thunderbolt pattern.")); //かみなりのいし 31 | break; 32 | case 2: 33 | pokemon = new Pokemon("Articuno", 90, 85, 100, 85, 50); //フリーザ 34 | pokemon.setHeldItem(new Item("Ice Stone", 35 | "A peculiar stone that can make certain species of Pokémon evolve. It has an unmistakable snowflake pattern.")); //こおりのいし 36 | break; 37 | default: 38 | pokemon = null; 39 | break; 40 | } 41 | return pokemon; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/java2kotlinpractice/lesson5/Lesson5Activity.java: -------------------------------------------------------------------------------- 1 | package com.example.java2kotlinpractice.lesson5; 2 | 3 | import com.example.java2kotlinpractice.R; 4 | import com.example.java2kotlinpractice.lesson4.Pokemon; 5 | 6 | import android.os.Bundle; 7 | import android.support.annotation.Nullable; 8 | import android.support.v7.app.AppCompatActivity; 9 | import android.widget.TextView; 10 | 11 | import static com.example.java2kotlinpractice.lesson4.Lesson4.gotcha; 12 | import static com.example.java2kotlinpractice.lesson5.Lesson5.gotchaLegend; 13 | 14 | /** 15 | * Extension 16 | */ 17 | public class Lesson5Activity extends AppCompatActivity { 18 | 19 | /** 20 | * (拡張関数とは関係ない) 21 | * 必ず値が代入されるならnon-nullにも出来る 22 | * lateinitでnon-nullに遅延評価可能 ref) Delegates.notNull(), lazy 23 | */ 24 | private TextView textView; 25 | private TextView textView2; 26 | 27 | @Override 28 | protected void onCreate(@Nullable Bundle savedInstanceState) { 29 | super.onCreate(savedInstanceState); 30 | setContentView(R.layout.activity_lesson5); 31 | 32 | textView = findViewById(R.id.textView); 33 | textView2 = findViewById(R.id.textView2); 34 | lesson5_1(); 35 | lesson5_2(); 36 | } 37 | 38 | private void lesson5_1() { 39 | final Pokemon hoOh = new Pokemon("Ho-oh", 106, 130, 90, 90); 40 | final Pokemon newPokemon = gotcha(hoOh); 41 | 42 | // let, runとelvis演算子を組み合わせてみましょう 43 | if (newPokemon != null) { 44 | textView.setText(newPokemon.getName()); 45 | } else { 46 | textView.setText(hoOh.getName()); 47 | } 48 | } 49 | 50 | private void lesson5_2() { 51 | final Pokemon pokemon = gotchaLegend(); 52 | 53 | // ポケモンの変数定義は必要無くなる 54 | if (pokemon != null) { 55 | // 努力値をセット 56 | pokemon.setEffortHp(252); 57 | pokemon.setEffortAttack(252); 58 | pokemon.setEffortDefence(4); 59 | pokemon.setEffortSpeed(0); 60 | textView2.setText(pokemon.getName()); 61 | } else { 62 | textView2.setText("Mistake!"); 63 | } 64 | } 65 | } -------------------------------------------------------------------------------- /app/src/main/java/com/example/java2kotlinpractice/lesson6/Lesson6.java: -------------------------------------------------------------------------------- 1 | package com.example.java2kotlinpractice.lesson6; 2 | 3 | import com.example.java2kotlinpractice.lesson4.Pokemon; 4 | 5 | import android.support.annotation.Nullable; 6 | 7 | import java.util.Random; 8 | 9 | public class Lesson6 { 10 | 11 | // battleターン間隔 12 | private static final int INTERVAL_DURATION = 2000; 13 | 14 | // 技のダメージは固定 15 | private static final int DAMAGE = 50; 16 | 17 | /** 18 | * Use Thread.sleep() in this method. 19 | */ 20 | public static void battle(Pokemon partnerPokemon, Pokemon enemy, @Nullable EventListener listener) { 21 | 22 | final Pokemon first; 23 | final Pokemon last; 24 | 25 | // 先行, 後攻決め 本来勝負中に能力変化があるが、今回は変化しないため1回のみ 26 | if (partnerPokemon.getSpeed() > enemy.getSpeed()) { 27 | first = partnerPokemon; 28 | last = enemy; 29 | } else { 30 | first = enemy; 31 | last = partnerPokemon; 32 | } 33 | 34 | // battle 35 | while (true) { 36 | // 先行攻撃 37 | int damage = calcAttackDamage(first, last); 38 | last.setHp(last.getHp() - damage); 39 | if (listener != null) { 40 | listener.onAttacked(first, last, damage); 41 | } 42 | 43 | // hp確認 44 | if (last.getHp() <= 0) { 45 | if (listener != null) { 46 | listener.onBattleEnd(first, last); 47 | } 48 | break; 49 | } 50 | 51 | try { 52 | Thread.sleep(INTERVAL_DURATION); 53 | } catch (InterruptedException e) { 54 | e.printStackTrace(); 55 | } 56 | 57 | // 後攻攻撃 58 | damage = calcAttackDamage(last, first); 59 | first.setHp(first.getHp() - damage); 60 | if (listener != null) { 61 | listener.onAttacked(last, first, damage); 62 | } 63 | 64 | // hp確認 65 | if (first.getHp() <= 0) { 66 | if (listener != null) { 67 | listener.onBattleEnd(last, first); 68 | } 69 | break; 70 | } 71 | 72 | try { 73 | Thread.sleep(INTERVAL_DURATION); 74 | } catch (InterruptedException e) { 75 | e.printStackTrace(); 76 | } 77 | } 78 | } 79 | 80 | /** 81 | * スコープ関数(run, let...)を上手く使うとreturnを消せる 82 | */ 83 | private static int calcAttackDamage(Pokemon from, Pokemon to) { 84 | // 0.85 ~ 1.0 85 | final float rate = (new Random().nextInt(16) + 85) / 100f; 86 | return (int) (((from.getLevel() * 2 / 5f + 2) * DAMAGE * from.getAttack() / to.getDefence() / 50f + 2) * rate); 87 | } 88 | 89 | /** 90 | * interfaceの書き方はJavaとあまり変わらない 91 | */ 92 | public interface EventListener { 93 | 94 | void onAttacked(Pokemon attacker, Pokemon defender, int damage); 95 | 96 | void onBattleEnd(Pokemon winner, Pokemon loser); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/java2kotlinpractice/lesson6/Lesson6Activity.java: -------------------------------------------------------------------------------- 1 | package com.example.java2kotlinpractice.lesson6; 2 | 3 | import com.example.java2kotlinpractice.R; 4 | import com.example.java2kotlinpractice.lesson4.Pokemon; 5 | 6 | import android.os.Bundle; 7 | import android.os.Handler; 8 | import android.support.annotation.Nullable; 9 | import android.support.v7.app.AppCompatActivity; 10 | import android.view.View; 11 | import android.widget.Button; 12 | import android.widget.TextView; 13 | 14 | import static com.example.java2kotlinpractice.lesson5.Lesson5.gotchaLegend; 15 | 16 | /** 17 | * Lambda 18 | */ 19 | public class Lesson6Activity extends AppCompatActivity { 20 | 21 | private Button button; 22 | private TextView textView; 23 | private Button button2; 24 | private TextView textView2; 25 | 26 | @Override 27 | protected void onCreate(@Nullable Bundle savedInstanceState) { 28 | super.onCreate(savedInstanceState); 29 | setContentView(R.layout.activity_lesson6); 30 | 31 | button = findViewById(R.id.button); 32 | textView = findViewById(R.id.textView); 33 | button2 = findViewById(R.id.button2); 34 | textView2 = findViewById(R.id.textView2); 35 | lesson6_1(); 36 | lesson6_2(); 37 | } 38 | 39 | private void lesson6_1() { 40 | // lambda式で書ける 41 | // itに名前を付けないと中のlambda式と名前が衝突するので注意 42 | button.setOnClickListener(new View.OnClickListener() { 43 | @Override 44 | public void onClick(View view) { 45 | final Pokemon pokemon = gotchaLegend(); 46 | if (pokemon != null) { 47 | if (pokemon.getHeldItem() != null) { 48 | textView.setText(pokemon.getName() + " has " + pokemon.getHeldItem().getName()); 49 | } else { 50 | textView.setText(pokemon.getName() + " doesn't have items."); 51 | } 52 | } else { 53 | textView.setText("Mistake!"); 54 | } 55 | } 56 | }); 57 | } 58 | 59 | private void lesson6_2() { 60 | 61 | // UI threadを止めてるのでhandlerを使ってtext viewを更新 62 | // handlerはandroidのthread間通信を実現するもの 63 | // https://developer.android.com/reference/android/os/Handler.html 64 | final Handler handler = new Handler(); 65 | 66 | button2.setOnClickListener(new View.OnClickListener() { 67 | @Override 68 | public void onClick(View view) { 69 | 70 | button2.setClickable(false); 71 | 72 | final Pokemon mewTwo = new Pokemon("Mewtwo", 50, 106, 110, 90, 139); 73 | final Pokemon mew = new Pokemon("Mew", 50, 100, 100, 100, 100); 74 | 75 | new Thread(new Runnable() { 76 | @Override 77 | public void run() { 78 | Lesson6.battle(mewTwo, mew, new Lesson6.EventListener() { 79 | @Override 80 | public void onAttacked(final Pokemon attacker, final Pokemon defender, final int damage) { 81 | handler.post(new Runnable() { 82 | public void run() { 83 | textView2.setText(defender.getName() + " is attacked " + damage + " damage from " 84 | + attacker.getName()); 85 | } 86 | }); 87 | } 88 | 89 | @Override 90 | public void onBattleEnd(final Pokemon winner, final Pokemon loser) { 91 | handler.post(new Runnable() { 92 | public void run() { 93 | textView2.setText(winner.getName() + " is win."); 94 | button2.setClickable(true); 95 | } 96 | }); 97 | } 98 | }); 99 | } 100 | }).start(); 101 | } 102 | }); 103 | } 104 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_lesson1.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 15 | 16 | 25 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_lesson2.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 15 | 16 | 25 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_lesson3.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 15 | 16 | 25 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_lesson4.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 15 | 16 | 25 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_lesson5.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 15 | 16 | 25 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_lesson6.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 15 | 16 |