5 『合成・リスト・ライブラリ』
5.1 成績管理アプリケーション(オブジェクト指向)
5.1.1 成績管理アプリケーション(設計)
5.1.2 成績管理アプリケーション(1)
リスト 5.1.2.1 ScoreManagerOO1.java
1:
14: public class ScoreManagerOO1 {
15:
16: public static void main(String[] args) {
17: ScoreManagerOO1 scoreManager = new ScoreManagerOO1();
18: scoreManager.main();
19: }
20:
21:
22: final int MANAGEMENT_SCORE_MAX_SIZE = 5;
23: final int NULL_INDEX = -1;
24:
25: final String ADD = "add";
26: final String DELETE = "del";
27: final String SEARCH = "search";
28: final String SHOW = "show";
29: final String QUIT = "quit";
30:
31:
32: private Score[] scores;
33: private int scoreSize;
34:
35:
36: private void main() {
37:
38: System.out.println("★★★★★★成績管理アプリケーション★★★★★★");
39:
40:
41: initializeData();
42: manageScores();
43:
44:
45: System.out.println("アプリケーションを終了します。");
46: }
47:
48:
49: private void initializeData() {
50:
51: scores = new Score[MANAGEMENT_SCORE_MAX_SIZE];
52:
53:
54: scoreSize = 0;
55: }
56:
57:
58: private void manageScores() {
59:
60: while (true) {
61:
62: String command = getCommand();
63:
64:
65: if (command.equals(ADD)) {
66: addScore();
67: } else if (command.equals(DELETE)) {
68: deleteScore();
69: } else if (command.equals(SEARCH)) {
70: searchScore();
71: } else if (command.equals(SHOW)) {
72: showScoreList();
73: } else if (command.equals(QUIT)) {
74: break;
75: } else {
76: System.out.println("そのようなコマンドはありません");
77: }
78: }
79: }
80:
81:
82: private String getCommand() {
83:
84: System.out.print("コマンドを入力してください");
85: System.out.print("(" + ADD + ":成績の追加");
86: System.out.print(", " + DELETE + ":成績の削除");
87: System.out.print(", " + SEARCH + ":成績の検索");
88: System.out.print(", " + SHOW + ":成績一覧と平均点の表示");
89: System.out.print(", " + QUIT + ":終了" + ")");
90: System.out.println();
91:
92:
93: String command = Input.getString();
94: return command;
95: }
96:
97:
98: private void addScore() {
99:
100: System.out.println("名前を入力してください");
101: String name = Input.getString();
102: System.out.println("成績を入力してください");
103: int score = Input.getInt();
104:
105:
106: int addScoreIndex = getScoreSize();
107: scores[addScoreIndex] = new Score(score, name);
108: scoreSize++;
109:
110:
111: System.out.println("成績を登録しました");
112: }
113:
114: private void deleteScore() {
115:
116: System.out.println("削除する成績の名前を入力してください");
117: String name = Input.getString();
118:
119:
120: int deleteIndex = searchScore(name);
121: deleteScore(deleteIndex);
122:
123:
124: System.out.println("成績を削除しました");
125: }
126:
127:
128: private void searchScore() {
129:
130: System.out.println("検索する成績の名前を入力してください");
131: String name = Input.getString();
132:
133:
134: int searchIndex = searchScore(name);
135:
136:
137: System.out.println(scores[searchIndex].getName() + "さんの点数は"
138: + scores[searchIndex].getScore() + "点です");
139: }
140:
141:
142: private void showScoreList() {
143:
144: System.out.println("★★★★★★成績一覧表★★★★★★");
145: for (int i = 0; i < getScoreSize(); i++) {
146: System.out.println(scores[i].getName() + "さん"
147: + scores[i].getScore() + "点");
148: }
149:
150:
151: System.out.println("★★★★★★平均点★★★★★★");
152: double average = getAverageScore();
153: System.out.println("平均点:" + average + "点");
154: }
155:
156:
157: private int searchScore(String name) {
158: for (int i = 0; i < getScoreSize(); i++) {
159: if (scores[i].getName().equals(name)) {
160: return i;
161: }
162: }
163: return NULL_INDEX;
164: }
165:
166:
167: private int getScoreSize() {
168: return scoreSize;
169: }
170:
171:
172: private void deleteScore(int deleteIndex) {
173:
174: scoreSize--;
175:
176:
177: for (int i = deleteIndex; i < getScoreSize(); i++) {
178: scores[i] = scores[i + 1];
179: }
180: }
181:
182:
183: private double getAverageScore() {
184:
185: double total = 0d;
186: for (int i = 0; i < getScoreSize(); i++) {
187: total = total + scores[i].getScore();
188: }
189:
190:
191: double average = total / getScoreSize();
192:
193: return average;
194: }
195:
196: }
リスト 5.1.2.2 Score.java
1:
7: public class Score {
8:
9: private int score;
10: private String name;
11:
12:
15: public Score(int score, String name) {
16: this.score = score;
17: this.name = name;
18: }
19:
20:
23: public String getName() {
24: return this.name;
25: }
26:
27:
30: public int getScore() {
31: return this.score;
32: }
33: }
5.1.3 成績管理アプリケーション(2)(配列隠蔽・リスト導入)
リスト 5.1.3.1 ScoreManagerOO2.java
1:
15: public class ScoreManagerOO2 {
16:
17: public static void main(String[] args) {
18: ScoreManagerOO2 scoreManager = new ScoreManagerOO2();
19: scoreManager.main();
20: }
21:
22:
23: final int NULL_INDEX = -1;
24:
25: final String ADD = "add";
26: final String DELETE = "del";
27: final String SEARCH = "search";
28: final String SHOW = "show";
29: final String QUIT = "quit";
30:
31:
32: private ScoreList scoreList;
33:
34:
35: private void main() {
36:
37: System.out.println("★★★★★★成績管理アプリケーション★★★★★★");
38:
39:
40: initializeData();
41: manageScores();
42:
43:
44: System.out.println("アプリケーションを終了します。");
45: }
46:
47:
48: private void initializeData() {
49: scoreList = new ScoreList();
50: }
51:
52:
53: private void manageScores() {
54:
55: while (true) {
56:
57: String command = getCommand();
58:
59:
60: if (command.equals(ADD)) {
61: addScore();
62: } else if (command.equals(DELETE)) {
63: deleteScore();
64: } else if (command.equals(SEARCH)) {
65: searchScore();
66: } else if (command.equals(SHOW)) {
67: showScoreList();
68: } else if (command.equals(QUIT)) {
69: break;
70: } else {
71: System.out.println("そのようなコマンドはありません");
72: }
73: }
74: }
75:
76:
77: private String getCommand() {
78:
79: System.out.print("コマンドを入力してください");
80: System.out.print("(" + ADD + ":成績の追加");
81: System.out.print(", " + DELETE + ":成績の削除");
82: System.out.print(", " + SEARCH + ":成績の検索");
83: System.out.print(", " + SHOW + ":成績一覧と平均点の表示");
84: System.out.print(", " + QUIT + ":終了" + ")");
85: System.out.println();
86:
87:
88: String command = Input.getString();
89: return command;
90: }
91:
92:
93: private void addScore() {
94:
95: System.out.println("名前を入力してください");
96: String name = Input.getString();
97: System.out.println("成績を入力してください");
98: int score = Input.getInt();
99:
100:
101: scoreList.add(new Score(score, name));
102:
103:
104: System.out.println("成績を登録しました");
105: }
106:
107: private void deleteScore() {
108:
109: System.out.println("削除する成績の名前を入力してください");
110: String name = Input.getString();
111:
112:
113: int deleteIndex = searchScoreIndexByName(name);
114: scoreList.remove(deleteIndex);
115:
116:
117: System.out.println("成績を削除しました");
118: }
119:
120:
121: private void searchScore() {
122:
123: System.out.println("検索する成績の名前を入力してください");
124: String name = Input.getString();
125:
126:
127: int searchIndex = searchScoreIndexByName(name);
128:
129:
130: Score score = scoreList.getScore(searchIndex);
131: System.out.println(score.getName() + "さんの点数は" + score.getScore()
132: + "点です");
133: }
134:
135:
136: private int searchScoreIndexByName(String name) {
137: for (int i = 0; i < scoreList.getSize(); i++) {
138: if (scoreList.getScore(i).getName().equals(name)) {
139: return i;
140: }
141: }
142: return NULL_INDEX;
143: }
144:
145:
146: private void showScoreList() {
147:
148: System.out.println("★★★★★★成績一覧表★★★★★★");
149: for (int i = 0; i < scoreList.getSize(); i++) {
150: Score score = scoreList.getScore(i);
151: System.out.println(score.getName() + "さん" + score.getScore() + "点");
152: }
153:
154:
155: System.out.println("★★★★★★平均点★★★★★★");
156: double average = getAverageScore();
157: System.out.println("平均点:" + average + "点");
158: }
159:
160:
161: private double getAverageScore() {
162:
163: double total = 0d;
164: for (int i = 0; i < scoreList.getSize(); i++) {
165: total = total + scoreList.getScore(i).getScore();
166: }
167:
168:
169: double average = total / scoreList.getSize();
170:
171: return average;
172: }
173:
174: }
5.1.3.1 成績リストを表現するクラス
リスト 5.1.3.1.1 ScoreList.java
1:
7: public class ScoreList {
8:
9:
10: final int NULL_INDEX = -1;
11:
12:
13: private Score[] scores;
14: private int scoreSize;
15:
16:
19: public ScoreList() {
20: scores = new Score[2];
21: scoreSize = 0;
22: }
23:
24:
27: public void add(Score score) {
28: int addScoreIndex = getSize();
29: scores[addScoreIndex] = score;
30: scoreSize++;
31: }
32:
33:
36: public void remove(Score score) {
37: int deleteIndex = getIndex(score);
38: remove(deleteIndex);
39: }
40:
41:
44: public void remove(int deleteIndex) {
45:
46: scoreSize--;
47:
48:
49: for (int i = deleteIndex; i < getSize(); i++) {
50: scores[i] = scores[i + 1];
51: }
52: }
53:
54:
57: public int getIndex(Score score) {
58: for (int i = 0; i < getSize(); i++) {
59: if (scores[i] == score) {
60: return i;
61: }
62: }
63: return NULL_INDEX;
64: }
65:
66:
69: public Score getScore(int index) {
70: return scores[index];
71: }
72:
73:
76: public int getSize() {
77: return scoreSize;
78: }
79:
80:
83: private void ensureCapacity() {
84: Score[] newScores = new Score[scoreSize + 1];
85: arrayCopy(scores, newScores);
86: scores = newScores;
87: }
88:
89:
92: private void arrayCopy(Score[] source, Score[] target) {
93: for (int i = 0; i < source.length && i < target.length; i++) {
94: target[i] = source[i];
95: }
96: }
97:
98: }
5.1.3.2 成績リストを表現するクラス(可変長)
リスト 5.1.3.2.1 UnlimitudeScoreList.java
1:
8: public class UnlimitedScoreList {
9:
10:
11: final int NULL_INDEX = -1;
12:
13:
14: private Score[] scores;
15: private int scoreSize;
16:
17:
20: public UnlimitedScoreList() {
21: scores = new Score[2];
22: scoreSize = 0;
23: }
24:
25:
28: public void add(Score score) {
29: int addScoreIndex = getSize();
30: scores[addScoreIndex] = score;
31: scoreSize++;
32:
33: ensureCapacity();
34: }
35:
36:
39: public void remove(Score score) {
40: int deleteIndex = getIndex(score);
41: remove(deleteIndex);
42: }
43:
44:
47: public void remove(int deleteIndex) {
48:
49: scoreSize--;
50:
51:
52: for (int i = deleteIndex; i < getSize(); i++) {
53: scores[i] = scores[i + 1];
54: }
55:
56: ensureCapacity();
57: }
58:
59:
62: public int getIndex(Score score) {
63: for (int i = 0; i < getSize(); i++) {
64: if (scores[i] == score) {
65: return i;
66: }
67: }
68: return NULL_INDEX;
69: }
70:
71:
74: public Score getScore(int index) {
75: return scores[index];
76: }
77:
78:
81: public int getSize() {
82: return scoreSize;
83: }
84:
85:
88: private void ensureCapacity() {
89: Score[] newScores = new Score[scoreSize + 1];
90: arrayCopy(scores, newScores);
91: scores = newScores;
92: }
93:
94:
97: private void arrayCopy(Score[] source, Score[] target) {
98: for (int i = 0; i < source.length && i < target.length; i++) {
99: target[i] = source[i];
100: }
101: }
102:
103: }
5.1.4 成績管理アプリケーション(3)(汎用リスト)
リスト 5.1.4.1 ScoreManagerOO3.java
1:
15: public class ScoreManagerOO3 {
16:
17: public static void main(String[] args) {
18: ScoreManagerOO3 scoreManager = new ScoreManagerOO3();
19: scoreManager.main();
20: }
21:
22:
23: final int NULL_INDEX = -1;
24:
25: final String ADD = "add";
26: final String DELETE = "del";
27: final String SEARCH = "search";
28: final String SHOW = "show";
29: final String QUIT = "quit";
30:
31:
32: private ObjectList scoreList;
33:
34:
35:
36: private void main() {
37:
38: System.out.println("★★★★★★成績管理アプリケーション★★★★★★");
39:
40:
41: initializeData();
42: manageScores();
43:
44:
45: System.out.println("アプリケーションを終了します。");
46: }
47:
48:
49: private void initializeData() {
50: scoreList = new ObjectList();
51:
52: }
53:
54:
55: private void manageScores() {
56:
57: while (true) {
58:
59: String command = getCommand();
60:
61:
62: if (command.equals(ADD)) {
63: addScore();
64: } else if (command.equals(DELETE)) {
65: deleteScore();
66: } else if (command.equals(SEARCH)) {
67: searchScore();
68: } else if (command.equals(SHOW)) {
69: showScoreList();
70: } else if (command.equals(QUIT)) {
71: break;
72: } else {
73: System.out.println("そのようなコマンドはありません");
74: }
75: }
76: }
77:
78:
79: private String getCommand() {
80:
81: System.out.print("コマンドを入力してください");
82: System.out.print("(" + ADD + ":成績の追加");
83: System.out.print(", " + DELETE + ":成績の削除");
84: System.out.print(", " + SEARCH + ":成績の検索");
85: System.out.print(", " + SHOW + ":成績一覧と平均点の表示");
86: System.out.print(", " + QUIT + ":終了" + ")");
87: System.out.println();
88:
89:
90: String command = Input.getString();
91: return command;
92: }
93:
94:
95: private void addScore() {
96:
97: System.out.println("名前を入力してください");
98: String name = Input.getString();
99: System.out.println("成績を入力してください");
100: int score = Input.getInt();
101:
102:
103: scoreList.add(new Score(score, name));
104:
105:
106: System.out.println("成績を登録しました");
107: }
108:
109: private void deleteScore() {
110:
111: System.out.println("削除する成績の名前を入力してください");
112: String name = Input.getString();
113:
114:
115: int deleteIndex = searchScoreIndexByName(name);
116: scoreList.remove(deleteIndex);
117:
118:
119: System.out.println("成績を削除しました");
120: }
121:
122:
123: private void searchScore() {
124:
125: System.out.println("検索する成績の名前を入力してください");
126: String name = Input.getString();
127:
128:
129: int searchIndex = searchScoreIndexByName(name);
130:
131:
132: Score score = (Score) scoreList.get(searchIndex);
133: System.out.println(score.getName() + "さんの点数は" + score.getScore()
134: + "点です");
135: }
136:
137:
138: private int searchScoreIndexByName(String name) {
139: for (int i = 0; i < scoreList.getSize(); i++) {
140: if (((Score) scoreList.get(i)).getName().equals(name)) {
141: return i;
142: }
143: }
144: return NULL_INDEX;
145: }
146:
147:
148: private void showScoreList() {
149:
150: System.out.println("★★★★★★成績一覧表★★★★★★");
151: for (int i = 0; i < scoreList.getSize(); i++) {
152: Score score = (Score) scoreList.get(i);
153: System.out.println(score.getName() + "さん" + score.getScore() + "点");
154: }
155:
156:
157: System.out.println("★★★★★★平均点★★★★★★");
158: double average = getAverageScore();
159: System.out.println("平均点:" + average + "点");
160: }
161:
162:
163: private double getAverageScore() {
164:
165: double total = 0d;
166: for (int i = 0; i < scoreList.getSize(); i++) {
167: total = total + ((Score) scoreList.get(i)).getScore();
168: }
169:
170:
171: double average = total / scoreList.getSize();
172:
173: return average;
174: }
175:
176: }
5.1.4.1 汎用リストを表現するクラス
リスト 5.1.4.1.1 ObjectList.java
1:
7: public class ObjectList {
8:
9:
10: final int NULL_INDEX = -1;
11:
12:
13: private Object[] objects;
14: private int size;
15:
16:
19: public ObjectList() {
20: objects = new Object[2];
21: size = 0;
22: }
23:
24:
27: public void add(Object Object) {
28: int addObjectIndex = getSize();
29: objects[addObjectIndex] = Object;
30: size++;
31:
32: ensureCapacity();
33: }
34:
35:
38: public void remove(Object Object) {
39: int deleteIndex = getIndex(Object);
40: remove(deleteIndex);
41: }
42:
43:
46: public void remove(int deleteIndex) {
47:
48: size--;
49:
50:
51: for (int i = deleteIndex; i < getSize(); i++) {
52: objects[i] = objects[i + 1];
53: }
54:
55: ensureCapacity();
56: }
57:
58:
61: public int getIndex(Object object) {
62: for (int i = 0; i < getSize(); i++) {
63: if (objects[i] == object) {
64: return i;
65: }
66: }
67: return NULL_INDEX;
68: }
69:
70:
73: public Object get(int index) {
74: return objects[index];
75: }
76:
77:
80: public int getSize() {
81: return size;
82: }
83:
84:
87: private void ensureCapacity() {
88: Object[] newObjects = new Object[size + 1];
89: arrayCopy(objects, newObjects);
90: objects = newObjects;
91: }
92:
93:
96: private void arrayCopy(Object[] source, Object[] target) {
97: for (int i = 0; i < source.length && i < target.length; i++) {
98: target[i] = source[i];
99: }
100: }
101:
102: }
5.1.4.2 汎用リストを表現するクラス(連結リストで実装)
リスト 5.1.4.2.1 ObjectLinkedList.java
1:
7: public class ObjectLinkedList {
8:
9:
10: final int NULL_INDEX = -1;
11:
12:
13: private Link firstLink = null;
14:
15:
18: public ObjectLinkedList() {
19: }
20:
21:
24: public void add(Object object) {
25:
26: Link newLink = new Link();
27: newLink.data = object;
28:
29:
30: if (firstLink == null) {
31:
32:
33: firstLink = newLink;
34:
35: } else {
36: Link currentLink = firstLink;
37:
38:
39: while (currentLink.next != null) {
40: currentLink = currentLink.next;
41: }
42:
43:
44: currentLink.next = newLink;
45: }
46: }
47:
48:
51: public void remove(Object object) {
52:
53: Link currentLink = firstLink;
54: Link previousLink = null;
55:
56:
57: while (currentLink != null) {
58:
59:
60: if (currentLink.data == object) {
61:
62:
63: if (currentLink == firstLink) {
64: firstLink = currentLink.next;
65: break;
66: } else if (currentLink.next == null) {
67: previousLink.next = null;
68: break;
69: } else {
70: previousLink.next = currentLink.next;
71: break;
72: }
73: }
74:
75:
76: previousLink = currentLink;
77: currentLink = currentLink.next;
78: }
79: }
80:
81:
84: public void remove(int index) {
85: Object removeTarget = get(index);
86: remove(removeTarget);
87: }
88:
89:
92: public int getIndex(Object object) {
93: Link currentLink = firstLink;
94: int currentIndex = 0;
95:
96:
97: while (currentLink != null) {
98:
99:
100: if (currentLink.data == object) {
101: return currentIndex;
102: }
103:
104:
105: currentLink = currentLink.next;
106: currentIndex++;
107: }
108:
109: return NULL_INDEX;
110:
111: }
112:
113:
116: public Object get(int index) {
117: Link currentLink = firstLink;
118: int currentIndex = 0;
119:
120:
121: while (currentLink != null) {
122:
123:
124: if (currentIndex == index) {
125: return currentLink.data;
126: }
127:
128:
129: currentLink = currentLink.next;
130: currentIndex++;
131: }
132:
133: return null;
134: }
135:
136:
139: public int getSize() {
140: Link currentLink = firstLink;
141: int count = 0;
142:
143:
144: while (currentLink != null) {
145:
146:
147: count++;
148:
149:
150: if (currentLink.next == null) {
151: return count;
152: }
153:
154:
155: currentLink = currentLink.next;
156: }
157:
158:
159: return count;
160: }
161:
162:
167: class Link {
168: Object data;
169: Link next;
170: }
171:
172: }
173:
5.1.5 成績管理アプリケーション(4)(javaのライブラリを使う)
リスト 5.1.5.1 ScoreManagerOO4.java
1: import java.util.ArrayList;
2: import java.util.List;
3:
4:
18: public class ScoreManagerOO4 {
19:
20: public static void main(String[] args) {
21: ScoreManagerOO4 scoreManager = new ScoreManagerOO4();
22: scoreManager.main();
23: }
24:
25:
26: final int NULL_INDEX = -1;
27:
28: final String ADD = "add";
29: final String DELETE = "del";
30: final String SEARCH = "search";
31: final String SHOW = "show";
32: final String QUIT = "quit";
33:
34:
35: private List scoreList;
36:
37:
38: private void main() {
39:
40: System.out.println("★★★★★★成績管理アプリケーション★★★★★★");
41:
42:
43: initializeData();
44: manageScores();
45:
46:
47: System.out.println("アプリケーションを終了します。");
48: }
49:
50:
51: private void initializeData() {
52: scoreList = new ArrayList();
53: }
54:
55:
56: private void manageScores() {
57:
58: while (true) {
59:
60: String command = getCommand();
61:
62:
63: if (command.equals(ADD)) {
64: addScore();
65: } else if (command.equals(DELETE)) {
66: deleteScore();
67: } else if (command.equals(SEARCH)) {
68: searchScore();
69: } else if (command.equals(SHOW)) {
70: showScoreList();
71: } else if (command.equals(QUIT)) {
72: break;
73: } else {
74: System.out.println("そのようなコマンドはありません");
75: }
76: }
77: }
78:
79:
80: private String getCommand() {
81:
82: System.out.print("コマンドを入力してください");
83: System.out.print("(" + ADD + ":成績の追加");
84: System.out.print(", " + DELETE + ":成績の削除");
85: System.out.print(", " + SEARCH + ":成績の検索");
86: System.out.print(", " + SHOW + ":成績一覧と平均点の表示");
87: System.out.print(", " + QUIT + ":終了" + ")");
88: System.out.println();
89:
90:
91: String command = Input.getString();
92: return command;
93: }
94:
95:
96: private void addScore() {
97:
98: System.out.println("名前を入力してください");
99: String name = Input.getString();
100: System.out.println("成績を入力してください");
101: int score = Input.getInt();
102:
103:
104: scoreList.add(new Score(score, name));
105:
106:
107: System.out.println("成績を登録しました");
108: }
109:
110: private void deleteScore() {
111:
112: System.out.println("削除する成績の名前を入力してください");
113: String name = Input.getString();
114:
115:
116: int deleteIndex = searchScoreIndexByName(name);
117: scoreList.remove(deleteIndex);
118:
119:
120: System.out.println("成績を削除しました");
121: }
122:
123:
124: private void searchScore() {
125:
126: System.out.println("検索する成績の名前を入力してください");
127: String name = Input.getString();
128:
129:
130: int searchIndex = searchScoreIndexByName(name);
131:
132:
133: Score score = (Score) scoreList.get(searchIndex);
134: System.out.println(score.getName() + "さんの点数は" + score.getScore()
135: + "点です");
136: }
137:
138:
139: private int searchScoreIndexByName(String name) {
140: for (int i = 0; i < scoreList.size(); i++) {
141: if (((Score) scoreList.get(i)).getName().equals(name)) {
142: return i;
143: }
144: }
145: return NULL_INDEX;
146: }
147:
148:
149: private void showScoreList() {
150:
151: System.out.println("★★★★★★成績一覧表★★★★★★");
152: for (int i = 0; i < scoreList.size(); i++) {
153: Score score = (Score) scoreList.get(i);
154: System.out.println(score.getName() + "さん" + score.getScore() + "点");
155: }
156:
157:
158: System.out.println("★★★★★★平均点★★★★★★");
159: double average = getAverageScore();
160: System.out.println("平均点:" + average + "点");
161: }
162:
163:
164: private double getAverageScore() {
165:
166: double total = 0d;
167: for (int i = 0; i < scoreList.size(); i++) {
168: total = total + ((Score) scoreList.get(i)).getScore();
169: }
170:
171:
172: double average = total / scoreList.size();
173:
174: return average;
175: }
176:
177: }
5.2 シューティングゲーム
5.2.1 シューティングゲーム(5)(リスト導入・オプション導入)
リスト 5.2.1.1 ShootingGame5.java
1: import java.util.ArrayList;
2: import java.util.List;
3:
4: import obpro.gui.BCanvas;
5: import obpro.gui.BWindow;
6: import obpro.sound.BSound;
7:
8:
15: public class ShootingGame5 {
16:
17: public static void main(String[] args) {
18: ShootingGame5 shootingGame = new ShootingGame5();
19: shootingGame.main();
20: }
21:
22:
23: private BWindow window;
24:
25:
26: private List elements = new ArrayList();
27:
28:
29: private Background background;
30: private PlayerAircraft player;
31: private List enemies = new ArrayList();
32: private List bullets = new ArrayList();
33:
34:
35: private BSound bgm;
36:
37: private void main() {
38: openWindow();
39: doAnimation();
40: }
41:
42:
43: private void openWindow() {
44: window = new BWindow();
45: window.setLocation(100, 100);
46: window.setSize(640, 480);
47: window.show();
48: }
49:
50:
51: private void doAnimation() {
52:
53: BCanvas canvas = window.getCanvas();
54: initializeElements();
55: initializeSounds();
56:
57:
58: bgm.loop();
59: while (true) {
60: {
61:
62: processOneStepForGame(canvas);
63:
64:
65: for (int i = 0; i < elements.size(); i++) {
66: AnimationElement element = (AnimationElement) elements
67: .get(i);
68: element.processOneStep(canvas);
69: }
70: }
71:
72:
73: canvas.clear();
74: for (int i = 0; i < elements.size(); i++) {
75: AnimationElement element = (AnimationElement) elements.get(i);
76: element.draw(canvas);
77: }
78: canvas.update();
79:
80:
81: canvas.sleep(0.01);
82: }
83: }
84:
85:
88: private void initializeElements() {
89:
90: background = new Background();
91: elements.add(background);
92:
93:
94: player = new PlayerAircraft(50, 50, this);
95: elements.add(player);
96: }
97:
98:
101: private void initializeSounds() {
102:
103: bgm = new BSound("sound/bgm.mp3");
104:
105:
106: BSound.load("sound/explode_enemy.mp3");
107: BSound.load("sound/explode_player.mp3");
108: BSound.load("sound/fire.mp3");
109: BSound.load("sound/optionvoice.mp3");
110: BSound.load("sound/powerup.mp3");
111: }
112:
113:
116: private void processOneStepForGame(BCanvas canvas) {
117:
118: int randomNumber = (int) (Math.random() * 30);
119: if (randomNumber == 0) {
120: int y = (int) (Math.random() * canvas.getCanvasHeight());
121: EnemyAircraft enemy = new EnemyAircraft(canvas.getCanvasWidth(), y,
122: this);
123: addEnemy(enemy);
124: }
125:
126:
127: for (int i = 0; i < enemies.size(); i++) {
128: EnemyAircraft enemy = (EnemyAircraft) enemies.get(i);
129: if (enemy.isAlive() && player.intersects(enemy)) {
130: player.explode();
131: }
132: }
133:
134:
135: for (int i = 0; i < enemies.size(); i++) {
136: EnemyAircraft enemy = (EnemyAircraft) enemies.get(i);
137: for (int j = 0; j < bullets.size(); j++) {
138: Bullet bullet = (Bullet) bullets.get(j);
139: if (bullet.isAlive() && enemy.isAlive()
140: && enemy.intersects(bullet)) {
141: enemy.explode();
142: bullet.destroy();
143: }
144: }
145: }
146: }
147:
148:
151: public void addEnemy(EnemyAircraft enemy) {
152: enemies.add(enemy);
153: elements.add(enemy);
154: }
155:
156:
159: public void removeEnemy(EnemyAircraft enemy) {
160: enemies.remove(enemy);
161: elements.remove(enemy);
162: }
163:
164:
167: public void addBullet(Bullet bullet) {
168: bullets.add(bullet);
169: elements.add(bullet);
170: }
171:
172:
175: public void removeBullet(Bullet bullet) {
176: bullets.remove(bullet);
177: elements.remove(bullet);
178: }
179:
180: }
5.2.2 アニメーションするオブジェクトすべてのスーパークラス
リスト 5.2.2.1 AnimationElement.java
1: import obpro.gui.BCanvas;
2:
3:
6: public class AnimationElement {
7:
8:
11: public void processOneStep(BCanvas canvas) {
12: }
13:
14:
17: public void draw(BCanvas canvas) {
18: }
19:
20: }
5.2.3 背景を表現するクラス
リスト 5.2.3.1 Background.java
1: import obpro.gui.BCanvas;
2:
3:
6: public class Background extends AnimationElement {
7:
8: private int x1 = 0;
9: private int x2 = 1000;
10:
11:
14: public void processOneStep(BCanvas canvas) {
15: x1--;
16: if (x1 < -1000) {
17: x1 = 1000;
18: }
19: x2--;
20: if (x2 < -1000) {
21: x2 = 1000;
22: }
23: }
24:
25:
28: public void draw(BCanvas canvas) {
29: canvas.drawImage("img/background.jpg", x1, 0);
30: canvas.drawImage("img/background.jpg", x2, 0);
31: }
32:
33: }
5.2.4 シューティングゲームのキャラクターを表現するクラス
リスト 5.2.4.1 ShootingCharacter.java
1:
4: public class ShootingCharacter extends AnimationElement {
5:
6:
7: private int x = 0;
8: private int y = 0;
9: private int width = 0;
10: private int height = 0;
11:
12:
15: public ShootingCharacter(int x, int y, int width, int height) {
16: this.x = x;
17: this.y = y;
18: this.width = width;
19: this.height = height;
20: }
21:
22:
25: public int getHeight() {
26: return this.height;
27: }
28:
29:
32: public int getWidth() {
33: return this.width;
34: }
35:
36:
39: public int getX() {
40: return this.x;
41: }
42:
43:
46: public int getY() {
47: return this.y;
48: }
49:
50:
53: public void setLocation(int newX, int newY) {
54: x = newX;
55: y = newY;
56: }
57:
58:
61: public void move(int moveX, int moveY) {
62: x = x + moveX;
63: y = y + moveY;
64: }
65:
66:
69: public boolean intersects(ShootingCharacter another) {
70: int self_leftX = this.getX();
71: int self_rightX = this.getX() + this.getWidth();
72: int another_leftX = another.getX();
73: int another_rightX = another.getX() + another.getWidth();
74: int self_topY = this.getY();
75: int self_bottomY = this.getY() + this.getHeight();
76: int another_topY = another.getY();
77: int another_bottomY = another.getY() + another.getHeight();
78:
79: return (another_leftX < self_rightX && another_rightX > self_leftX
80: && another_topY < self_bottomY && another_bottomY > self_topY);
81: }
82: }
5.2.5 自機を表現するクラス
リスト 5.2.5.1 PlayerAircraft.java
1: import java.util.ArrayList;
2: import java.util.List;
3:
4: import obpro.gui.BCanvas;
5: import obpro.sound.BSound;
6:
7:
10: public class PlayerAircraft extends ShootingCharacter {
11:
12:
13: private final int ALIVE = 1;
14: private final int EXPLODING = 2;
15: private final int DEAD = 3;
16:
17: private final int EXPLODING_ANIMATION_SIZE = 11;
18:
19:
20: private int liveState = ALIVE;
21: private int explodingCount = 0;
22:
23:
24: private ShootingGame5 game;
25: private List options = new ArrayList();
26:
27:
30: public PlayerAircraft(int x, int y, ShootingGame5 game) {
31: super(x, y, 100, 50);
32: this.game = game;
33: }
34:
35:
38: public void move(int moveX, int moveY) {
39:
40: super.move(moveX, moveY);
41:
42:
43: for (int i = 0; i < options.size(); i++) {
44: Option option = (Option) options.get(i);
45: option.move(moveX, moveY);
46: }
47: }
48:
49:
52: public void processOneStep(BCanvas canvas) {
53: if (liveState == ALIVE) {
54:
55: if (canvas.isKeyPressing(37)) {
56: move(-5, 0);
57: }
58: if (canvas.isKeyPressing(38)) {
59: move(0, -5);
60: }
61: if (canvas.isKeyPressing(39)) {
62: move(5, 0);
63: }
64: if (canvas.isKeyPressing(40)) {
65: move(0, 5);
66: }
67:
68:
69: if (canvas.isKeyDown()) {
70: int keyCode = canvas.getKeyCode();
71: if (keyCode == 70) {
72: fire();
73: }
74: }
75:
76:
77: if (canvas.isKeyDown()) {
78: int keyCode = canvas.getKeyCode();
79: if (keyCode == 71) {
80: BSound.play("sound/optionvoice.mp3", 100);
81: BSound.play("sound/powerup.mp3");
82: options.add(new Option(getX(), getY(), getWidth(),
83: getHeight(), options.size() + 1));
84: }
85: }
86:
87:
88: for (int i = 0; i < options.size(); i++) {
89: Option option = (Option) options.get(i);
90: option.processOneStep(canvas);
91: }
92: } else if (liveState == EXPLODING) {
93: processExplode();
94: }
95: }
96:
97:
100: public boolean isAlive() {
101: return liveState == ALIVE;
102: }
103:
104:
107: private void processExplode() {
108: explodingCount++;
109: if (explodingCount >= EXPLODING_ANIMATION_SIZE) {
110: liveState = DEAD;
111: }
112: }
113:
114:
117: public void explode() {
118: if (liveState == ALIVE) {
119: liveState = EXPLODING;
120: explodingCount = 0;
121: BSound.play("sound/explode_player.mp3");
122: }
123: }
124:
125:
128: public void draw(BCanvas canvas) {
129: if (liveState == ALIVE) {
130:
131: drawAircraft(canvas);
132:
133:
134: for (int i = 0; i < options.size(); i++) {
135: Option option = (Option) options.get(i);
136: option.draw(canvas);
137: }
138: } else if (liveState == EXPLODING) {
139: drawExplosion(canvas);
140: }
141: }
142:
143:
146: private void drawAircraft(BCanvas canvas) {
147: canvas.drawImage("img/player.png", getX(), getY(), getWidth(),
148: getHeight());
149: }
150:
151:
154: private void drawExplosion(BCanvas canvas) {
155: canvas.drawImage("img/explode" + (explodingCount + 1) + ".gif",
156: getX(), getY(), getWidth(), getHeight());
157: }
158:
159:
162: private void fire() {
163: if (liveState == ALIVE) {
164: BSound.play("sound/fire.mp3");
165:
166:
167: fire(this);
168:
169:
170: for (int i = 0; i < options.size(); i++) {
171: Option option = (Option) options.get(i);
172: fire(option);
173: }
174: }
175: }
176:
177:
180: private void fire(ShootingCharacter shooter) {
181: Bullet bullet = new Bullet(0, 0, 20, 10, game);
182: int x = shooter.getX() + shooter.getWidth() / 2 - bullet.getWidth() / 2;
183: int y = shooter.getY() + shooter.getHeight() / 2 - bullet.getHeight()
184: / 2;
185: bullet.setLocation(x, y);
186: game.addBullet(bullet);
187: }
188: }
5.2.6 敵機を表現するクラス
リスト 5.2.6.1 EnemyAircraft.java
1: import obpro.gui.BCanvas;
2: import obpro.sound.BSound;
3:
4:
7: public class EnemyAircraft extends ShootingCharacter {
8:
9:
10: private final int ALIVE = 1;
11: private final int EXPLODING = 2;
12: private final int DEAD = 3;
13:
14: private final int EXPLODING_ANIMATION_SIZE = 11;
15:
16:
17: private int liveState = ALIVE;
18: private int explodingCount = 0;
19:
20:
21: private ShootingGame5 game;
22:
23:
26: public EnemyAircraft(int x, int y, ShootingGame5 game) {
27: super(x, y, 100, 50);
28: this.game = game;
29: }
30:
31:
34: public void processOneStep(BCanvas canvas) {
35: if (liveState == ALIVE) {
36:
37: move(-5, 0);
38:
39:
40: if (getX() < -100) {
41: destroy();
42: }
43: } else if (liveState == EXPLODING) {
44: processExplode();
45: }
46: }
47:
48:
51: public boolean isAlive() {
52: return liveState == ALIVE;
53: }
54:
55:
58: public void destroy() {
59: game.removeEnemy(this);
60: liveState = DEAD;
61: }
62:
63:
66: private void processExplode() {
67: explodingCount++;
68: if (explodingCount >= EXPLODING_ANIMATION_SIZE) {
69: destroy();
70: }
71: }
72:
73:
76: public void explode() {
77: if (liveState == ALIVE) {
78: liveState = EXPLODING;
79: explodingCount = 0;
80: BSound.play("sound/explode_enemy.mp3");
81: }
82: }
83:
84:
87: public void draw(BCanvas canvas) {
88: if (liveState == ALIVE) {
89: drawAircraft(canvas);
90: } else if (liveState == EXPLODING) {
91: drawExplosion(canvas);
92: }
93: }
94:
95:
98: private void drawAircraft(BCanvas canvas) {
99: canvas.drawImage("img/enemy.png", getX(), getY(), getWidth(),
100: getHeight());
101: }
102:
103:
106: private void drawExplosion(BCanvas canvas) {
107: canvas.drawImage("img/explode" + (explodingCount + 1) + ".gif",
108: getX(), getY(), getWidth(), getHeight());
109: }
110:
111: }
5.2.7 弾を表現するクラス
リスト 5.2.7.1 Bullet.java
1: import java.awt.Color;
2:
3: import obpro.gui.BCanvas;
4:
5:
8: public class Bullet extends ShootingCharacter {
9:
10:
11: private final int ALIVE = 1;
12: private final int DEAD = 2;
13:
14:
15: private int liveState = ALIVE;
16:
17:
18: private ShootingGame5 game;
19:
20:
21: private Color color = new Color(0, 0, 255);
22:
23:
26: public Bullet(int x, int y, int width, int height, ShootingGame5 game) {
27: super(x, y, width, height);
28: this.game = game;
29: }
30:
31:
34: public void processOneStep(BCanvas canvas) {
35: if (liveState == ALIVE) {
36:
37: move(15, 0);
38:
39:
40: if (getX() > canvas.getCanvasWidth()) {
41: destroy();
42: }
43: }
44: }
45:
46:
49: public void destroy() {
50: if (liveState == ALIVE) {
51: liveState = DEAD;
52: game.removeBullet(this);
53: }
54: }
55:
56:
59: public boolean isAlive() {
60: return liveState == ALIVE;
61: }
62:
63:
66: public void draw(BCanvas canvas) {
67: if (liveState == ALIVE) {
68: canvas.drawFillTriangle(color, getX(), getY(), getX() + getWidth(),
69: getY(), getX(), getY() + getHeight());
70: canvas.drawFillTriangle(color, getX(), getY() + getHeight(), getX()
71: + getWidth(), getY() + getHeight(), getX() + getWidth(),
72: getY());
73: }
74: }
75:
76: }
5.2.8 オプションを表現するクラス
リスト 5.2.8.1 Sample1.dama
1: import java.util.LinkedList;
2:
3: import obpro.gui.BCanvas;
4:
5:
8: public class Option extends ShootingCharacter {
9:
10:
11: private final int SCALE_UP = 1;
12: private final int SCALE_DOWN = 2;
13:
14:
15: private int animationState = SCALE_UP;
16: private int animationCount = 0;
17:
18:
19: private LinkedList playerMoveHistories = new LinkedList();
20:
21:
24: public Option(int x, int y, int width, int height, int index) {
25: super(x, y, width, height);
26: initializeHistories(index);
27: }
28:
29:
32: private void initializeHistories(int index) {
33: for (int i = 0; i < 15 * index; i++) {
34: playerMoveHistories.add(new MoveData());
35: }
36: }
37:
38:
41: public void move(int moveX, int moveY) {
42:
43: MoveData moveData = (MoveData) playerMoveHistories.removeFirst();
44: super.move(moveData.dx, moveData.dy);
45:
46:
47: moveData.dx = moveX;
48: moveData.dy = moveY;
49: playerMoveHistories.addLast(moveData);
50: }
51:
52:
55: public void processOneStep(BCanvas canvas) {
56:
57: if (animationState == SCALE_UP) {
58: animationCount++;
59: } else if (animationState == SCALE_DOWN) {
60: animationCount--;
61: }
62:
63:
64: if (animationState == SCALE_UP && animationCount >= 10) {
65: animationState = SCALE_DOWN;
66: } else if (animationState == SCALE_DOWN && animationCount <= 0) {
67: animationState = SCALE_UP;
68: }
69: }
70:
71:
74: public void draw(BCanvas canvas) {
75: int w = getWidth() / 3 + animationCount;
76: int h = getHeight() / 3 + animationCount;
77: int x = getX() + (getWidth() - w) / 2;
78: int y = getY() + (getHeight() - h) / 2;
79: canvas.drawImage("img/option.png", x, y, w, h);
80: }
81:
82:
86: class MoveData {
87: int dx = 0;
88: int dy = 0;
89: }
90: }