Skip to content

Instantly share code, notes, and snippets.

@comuttun
Created August 10, 2010 01:25
Show Gist options
  • Save comuttun/516456 to your computer and use it in GitHub Desktop.
Save comuttun/516456 to your computer and use it in GitHub Desktop.
70
60
80
90
100
95
75
85
/*
* 例題1-1 次の平均点を求めるプログラムの、実行結果を書いてください。
*
* ※ 実際に実行しないこと。
*/
public class PrintAverage1 {
public static void main(String[] args) {
double average = (1 + 2 + 3 + 4 + 5) / 5;
System.out.println("平均 = " + average);
}
}
/*
* 例題1-2 次の平均点を求めるプログラムの、実行結果を書いてください。
*
* ※ 実際に実行しないこと。
*/
public class PrintAverage2 {
public static void main(String[] args) {
int[] points = {3, 5, 7, 10, 13};
int sum = 0;
for (int i = 0; i < points.length; i++) {
System.out.println(i + 1 + "番目の要素 = " + points[i]);
sum += points[i];
}
double average = sum / points.length;
System.out.println("平均点 = " + average);
}
}
/*
* 例題1-3 次の平均点を求めるプログラムの、実行結果を書いてください。
*
* ※ 実際に実行しないこと。
*/
import java.util.List;
import java.util.ArrayList;
public class PrintAverage3 {
public static void main(String[] args) {
List<Integer> pointList = new ArrayList<Integer>();
pointList.add(10);
pointList.add(20);
pointList.add(30);
pointList.add(40);
pointList.add(50);
pointList.add(60);
System.out.println("pointList の要素数 = " + pointList.size());
int sum = 0;
for (int i = 0; i < pointList.size(); i++) {
sum += pointList.get(i);
}
System.out.println("合計値 = " + sum);
double average = sum / pointList.size();
System.out.println("平均値 = " + average);
}
}
/*
* 例題1-4 次の平均点を求めるプログラムの、実行結果を書いてください。
*
* ※ 実際に実行しないこと。
*/
import java.util.List;
import java.util.ArrayList;
public class PrintAverage4 {
public static void main(String[] args) {
List<Integer> pointList1 = new ArrayList<Integer>();
pointList1.add(100);
pointList1.add(70);
pointList1.add(50);
int sum1 = getSum(pointList1);
double average1 = getAverage(pointList1);
System.out.println("合計点1 = " + sum1 + ", 平均点1 = " + average1);
List<Integer> pointList2 = new ArrayList<Integer>();
pointList2.add(20);
pointList2.add(40);
pointList2.add(60);
pointList2.add(80);
int sum2 = getSum(pointList2);
double average2 = getAverage(pointList2);
System.out.println("合計点2 = " + sum2 + ", 平均点2 = " + average2);
List<Integer> pointList3 = new ArrayList<Integer>();
pointList3.add(70);
pointList3.add(0);
pointList3.add(50);
pointList3.add(0);
pointList3.add(30);
int sum3 = getSum(pointList3);
double average3 = getAverage(pointList3);
System.out.println("合計点3 = " + sum3 + ", 平均点3 = " + average3);
}
public static int getSum(List<Integer> pointList) {
System.out.println("===== 合計値算出開始 =====");
int sum = 0;
for (Integer point : pointList) {
sum += point;
System.out.println("...点数 = " + point + ", 合計点 = " + sum);
}
System.out.println("===== 合計値算出終了 =====");
return sum;
}
public static double getAverage(List<Integer> pointList) {
System.out.println("===== 平均値算出開始 =====");
int sum = getSum(pointList);
double average = sum / pointList.size();
System.out.println("===== 平均値算出終了 =====");
return average;
}
}
/* 例題1-5 次の平均点を求めるプログラムの、実行結果を書いてください。
*
* ※ 実際に実行しないこと。
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.List;
import java.util.ArrayList;
public class PrintAverage5 {
public static void main(String[] args) throws Exception {
List<Integer> pointList = readPoints();
System.out.println("読み込んだ点数を表示します。");
for (int i = 0; i < pointList.size(); i++) {
System.out.println(i + 1 + "個目 = " + pointList.get(i));
}
int average = getAverage(pointList);
System.out.println("平均は " + average + " 点でした。");
}
/** ファイルから点数を読み込みます。
*/
public static List<Integer> readPoints() throws Exception {
// 読み込んだ点数を保持するリスト
List<Integer> pointList = new ArrayList<Integer>();
// ファイルを読み込むためのクラス
BufferedReader reader = new BufferedReader(new FileReader("points.txt"));
int lineCount = 0;
while (true) {
System.out.println(lineCount + 1 + "行目を読み込みます。");
String line = reader.readLine();
if (line == null) {
// 読み込む行が無いのでループを抜ける。
break;
}
int point = Integer.parseInt(line);
pointList.add(point);
System.out.println("点数リストに " + point + " を追加しました。");
lineCount++;
}
return pointList;
}
public static int getAverage(List<Integer> pointList) {
System.out.println("平均値を計算します。");
int sum = getSum(pointList);
int average = sum / pointList.size();
System.out.println("平均値を計算しました。");
return average;
}
public static int getSum(List<Integer> pointList) {
System.out.println("合計値を計算します。");
int sum = 0;
for (Integer point : pointList) {
sum += point;
}
System.out.println("合計値を計算しました。");
return sum;
}
}
例題2-1 フローチャートを参考にし、20までの整数値のうち、
素数のみ出力するプログラムを作成してください。
ただし、素数を判定するメソッドを作成すること。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment