-
-
Save fukayatsu/2909968 to your computer and use it in GitHub Desktop.
2科目の成績の合計、平均点を出す。(クラスメソッド、インスタンス、コンストラクタありVer)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainStudents { | |
public static void main(String[] args) { | |
Students students = new Students(); | |
students.add(new Student("小貫", 82, 70)); | |
students.add(new Student("鈴木", 85, 74)); | |
students.add(new Student("山田", 74, 88)); | |
students.add(new Student("後藤", 90, 74)); | |
students.add(new Student("池田", 70, 82)); | |
students.printTotalScores(); | |
students.printCount(); | |
students.printAverages(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.HashMap; | |
import java.util.Map; | |
public class Student { | |
private String name; | |
private Map<Subject, Double> scores; | |
public Student(String name, double japaneseScore, double englishScore) { | |
this.name = name; | |
scores = new HashMap<>(); | |
scores.put(Subject.JAPANESE, japaneseScore); | |
scores.put(Subject.ENGLISH, englishScore); | |
} | |
public double getTotalScore() { | |
double total = 0; | |
for (Double score : scores.values()) { | |
total += score; | |
} | |
return total; | |
} | |
public double getScore(Subject subject) { | |
return scores.get(subject); | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public Map<Subject, Double> getScores() { | |
return scores; | |
} | |
public void setScores(Map<Subject, Double> scores) { | |
this.scores = scores; | |
} | |
public void printTotalScore() { | |
System.out.println(name + "の合計点は" + getTotalScore() + "です。"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
public class Students { | |
private ArrayList<Student> students; | |
public Students() { | |
this.students = new ArrayList<>(); | |
} | |
public void add(Student student) { | |
students.add(student); | |
} | |
public int count() { | |
return students.size(); | |
} | |
public double getAverage(Subject subject) { | |
double sum = 0; | |
for (Student student : students) { | |
sum += student.getScore(subject); | |
} | |
return sum / (double) count(); | |
} | |
public void printCount() { | |
System.out.println("生徒の数は" + count() + "人です。"); | |
} | |
public void printTotalScores() { | |
for (Student student : students) { | |
student.printTotalScore(); | |
} | |
} | |
public void printAverages() { | |
for (Subject subject : Subject.values()) { | |
double average = getAverage(subject); | |
System.out.println(subject.getName() + "の平均点は" + average + "です。"); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public enum Subject { | |
JAPANESE("国語"), ENGLISH("英語"); | |
private String name; | |
private Subject(String name) { | |
this.name = name; | |
} | |
public String getName() { | |
return name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment