1 / 27

オブジェクト・プログラミング

オブジェクト・プログラミング. 第8回. 今日のメニュー. 前回の課題解説 8章,9章の解説と実験 本日はプログラミングしません. 前回の課題解説. 1.名前付け 2.メソッドの復習 3.クラスの復習 4.手続き指向の復習 5.データ構造とアルゴリズムの結合 6.さいごに. 1.名前付け. 1.1 ローレル指数を求めるメソッド 規則が違う getrohrer rohrersum GetRohrer 何をするか分からない calculate calc cal rohrer 妥当 getRohrer calcRohrer その他

Download Presentation

オブジェクト・プログラミング

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. オブジェクト・プログラミング 第8回

  2. 今日のメニュー • 前回の課題解説 • 8章,9章の解説と実験 • 本日はプログラミングしません

  3. 前回の課題解説 • 1.名前付け • 2.メソッドの復習 • 3.クラスの復習 • 4.手続き指向の復習 • 5.データ構造とアルゴリズムの結合 • 6.さいごに

  4. 1.名前付け • 1.1 ローレル指数を求めるメソッド • 規則が違う • getrohrer • rohrersum • GetRohrer • 何をするか分からない • calculate • calc • cal • rohrer • 妥当 • getRohrer • calcRohrer • その他 • rohrerData • newRohrer • wantRohrer

  5. 1.名前付け • コメントから書く • 適切な日本語を考えよ • メソッドはたいてい動詞形になる(~する) /** * ローレル指数を求める */ public int getRohrer(int weight,int height){ }

  6. 1.名前付け • 1.2 生徒のデータを表現するクラス • 代表例 • Student • StudentData • StudentInfo • StudentStatus • ChildData • Type • ~Type • StudentType • ChildDataType • その他 • ItemType • Health • Data • Profile • Member

  7. 1.名前付け • コメントから書く • 日本語で考える • 名詞がクラス名になる • クラス名は複数形にならない • ~Data,~Infoはたいていの場合、いらない • ItemType(商品種類)は、後にItem(商品)というクラスが登場するためTypeがついている

  8. 2.メソッド • 2.1 変数の復習 • 3つのxの意味の違いを考えてみよう int x; int y; x = 100; y = x; //名前のx //箱(番地)のx //中身(この場合は100)のx

  9. 2.メソッド //呼び出し1 int w; w = 56; //呼び出し2 int w; w = weight; • 2.2 引数 //呼び出し1 getRohrer(56,187); //呼び出し2 int weight = 62; int height = 177; getRohrer(weight,height); //中身(この場合は62)のweight //宣言 public int getRohrer(int w,int h){ } //名前のw

  10. 2.メソッド • スコープが違うので、同じ変数名が可能 • 名前が同じでも違う変数です //呼び出し1 int weight; weight = 56; //呼び出し2 int weight; weight = weight; //呼び出し1 getRohrer(56,187); //呼び出し2 int weight = 62; int height = 177; getRohrer(weight,height); //宣言 public int getRohrer(int weight,int height){ }

  11. 2.メソッド • 2.3 戻り値(1) 何のx? //呼び出し int rohrer;//戻り値を受け取る変数 rohrer = getRohrer(); //宣言 public int getRohrer(){ return 100; } //宣言 public int getRohrer(){ int x = 100; return x; } //中身(この場合は100)のx

  12. 2.メソッド • 2.3 戻り値(2) 何故ここに戻り値の型を 書かなければならないのか //宣言 public int getRohrer(int weight,int height){ return 100; } //名前のgetRohrer //呼び出し int rohrer;//戻り値を受け取る変数 rohrer = getRohrer(56,187); //中身(この場合は100)の getRohrer

  13. 3.クラス • 3.1 クラスの宣言 /** * 生徒クラス */ public class Student{ String name;//名前 int weight;//体重 int height;//身長 }

  14. ② ① ③ ⑤ ④ 66 "山田" 8769番地 weight name x ① ② 8769番地 3.クラス 1行目がどのような意味か 正確に説明せよ Student x = new Student(); x.weight = 66; x.name = "山田"; ④ ⑤

  15. 3.クラス • 3.3 変数の復習(クラスの変数) • 基本的にはintの時と変わらず • 4つのxの意味の違いを説明せよ Student x; Student y; x = new Student(); y = x; x.weight = 100; //名前のx //箱(番地)のx //中身(インスタンス番地)のx //番地が指すインスタンスのx

  16. 3. クラス • 多かった例 • せっかくクラスを作ったのに引数が2つのまま //メイン public static void main(String args[]){ Student student = new Student(); getRohrer(student.weight,student.height); } //ローレル指数を求める public static int getRohrer(int weight,int height){ }

  17. 3. クラス • 生徒クラスを引数にして、生徒のローレル指数を求めるメソッドにする //メイン public static void main(String args[]){ Student student = new Student(); getRohrer(student); } //ローレル指数を求める public static int getRohrer(Student student){ }

  18. 3.クラス //呼び出し1 Student student; student = new Student(); //呼び出し2 Student student; student = student; • 3.4 引数(クラス) //呼び出し1 getRohrer(new Student()); //呼び出し2 Student student; student = new Student(); getRohrer(student); //中身のstudent //宣言 public int getRohrer(Student student){ } //名前のstudent

  19. 3.クラス • 3.5 戻り値(クラス) //呼び出し String rohrerMessage;//戻り値を受け取る変数 rohrerMessage = getRohrerMessage(120); //宣言 public String getRohrer(int rohrer){ if(rohrer < 100){ return "やせすぎです"; }else{ String message = "太りすぎです"; return message; } } //中身のmessage

  20. 4.手続き指向の復習 • 手続き指向 • 意味のある仕事ごとにメソッドにまとめ、次々と呼び出すことで一つの大きな仕事を行う

  21. 4.手続き指向の復習 /** * 生徒のローレル指数の平均を取得する */ public int getAverageOfRohrer(){ int amount = 0;//ローレル指数の合計 int studentNum = 0;//生徒の人数 //ローレル指数の合計と、人数を求める for(int i=0;i<10;i++){ if(studentArray[i] != null){//生徒が入っている場合 Student student = studentArray[i]; int rohrer = student.getWeight() * 10000000 / student.getHeight() / student.getHeight() / student.getHeight();//ローレル指数を求める amount = amount + rohrer;//ローレル指数の合計に加える studentNum++;//生徒の人数を1加える } } //合計を人数で割り、平均を求める int average = amount / studentNum; return average; } • 多かった例 • 「ローレル指数を求める」ってメソッド化しなかったっけ?

  22. 4.手続き指向の復習(#1) /** * 生徒のローレル指数の平均を取得する */ public int getAverageOfRohrer(){ int amount = 0;//ローレル指数の合計 int studentNum = 0;//生徒の人数 //ローレル指数の合計と、人数を求める for(int i=0;i<10;i++){ if(studentArray[i] != null){//生徒が入っている場合 Student student = studentArray[i]; int rohrer = getRohrer(studentArray[i]);//ローレル指数を取得する amount = amount + rohrer;//ローレル指数の合計に加える studentNum++;//生徒の人数を1加える } } //合計を人数で割り、平均を求める int average = amount / studentNum; return average; } • せっかくメソッドを作ったのだから、使おう • メソッドからメソッドを呼ぶ • 「ローレル指数の平均値を求める」メソッド」から • 「一人分のローレル指数を求めるメソッド」を呼ぶ

  23. 5. データ構造とアルゴリズムの結合 • StudentListクラスは良く出来ていた • データ構造(配列)とアルゴリズムを結合させたクラス • Studentクラスもデータ構造とアルゴリズムを結合できる(#2) • getRohrer()メソッド • 出来た人は希少

  24. 引数がなくなっている 5.データ構造とアルゴリズムの結合 /** * 生徒クラス */ public class Student{ String name;//名前 int weight;//体重 int height;//身長 // ローレル指数を取得する public int getRohrer(){ int rohrer; rohrer = weight * 10000000 / height / height / height; return rohrer; } } データ構造 アルゴリズム

  25. 5.データ構造とアルゴリズムの結合 • 「生徒のローレル指数を求める」という明確なプログラムが書ける public static void main(String args[]){ //生徒を一人生成する Student student = new Student(); student.weight = 89; student.height = 166; int rohrer = student.getRohrer();//ローレル指数を求める System.out.println(rohrer);//表示する }

  26. 6. さいごに(#2) • 生徒人数変数の導入(8章で詳しく説明) //生徒リストクラス public class StudentList{ private Student[] studentArray;//生徒を保存する配列 private int studentNum;//生徒の人数 //生徒のローレル指数の平均を取得する public int getAverageOfRohrer(){ int amount = 0;//ローレル指数の合計 //ローレル指数の合計を求める for(int i=0;i<studentNum;i++){ int rohrer = studentArray[i].getRohrer();//ローレル指数を取得する amount = amount + rohrer;//ローレル指数の合計に加える } //合計を人数で割り、平均を求める int average = amount / studentNum; return average; } }

  27. 6.さいごに • クラスの健康データ表示のメソッド化 • Answerクラス(#2)を参照 /** * クラスの健康データを表示する */ public static void displayClassHealthData(String className,StudentList studentList){ int avgHeight = studentList.getAverageOfHeight(); System.out.println(className+"の身長の平均は"+avgHeight+"です"); int avgWeight = studentList.getAverageOfWeight(); System.out.println(className+"の体重の平均は"+avgWeight+"です"); int avgRohrer = studentList.getAverageOfRohrer(); System.out.println(className+"のローレル指数の平均は"+avgRohrer+"です"); }

More Related