OOP课程题目集第三次总结
前言
本次总结针对于pta上第三阶段的6次题目集。
第七次题目集主要巩固图形继承、多态、接口及集合框架,题量不多,题目简单;
第八次题目集主要掌握类的继承、多态性使用方法以及接口的应用,题量不多,比较简单;
第九次作业主要训练对列表,集合等的使用,题量少,难度一般;
第十,十一,十二次作业 是课程成绩统计程序的一次类结构设计,改进和增加功能;
设计与分析
下面对几次题目集的部分题目进行设计与分析。
9-1 统计Java程序中关键词的出现次数
题面较长,列出主要任务,逐个任务完成。
- Java中共有53个关键字
- 从键盘输入一段源码,统计这段源码中出现的关键字的数量
- 注释中出现的关键字不用统计
- 字符串中出现的关键字不用统计
- 统计出的关键字及数量按照关键字升序进行排序输出
- 未输入源码则认为输入非法
我的主要做题思想为先对一行行数据接受处理,数据进行简单的格式化之后在addValue中一个个单词分析存入treeMap中
用map键值对储存单词和次数,用set遍历已储存的键
Main类如下
public static void main(String[] args){
TreeMap<String,Integer> treeMap = initMap();//初始化treemap
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
if(str.equals("exit")){//判断是否为空
System.out.println("Wrong Format");
exit(0);
}
HashSet<String> setkey=new HashSet<>(treeMap.keySet());//便于遍历key
while(!str.matches("exit")){
str.trim();//去除tab和空格
if(str.startsWith("//")){//以单行注释为开头
}else{//正常输入
str = teimQutation(str);//去除”“内的内容
addValue(treeMap, str, setkey);//添加相应的值
}
str = scan.nextLine();
}
mapOut(treeMap, setkey);//输出关键词
}
对于注释中出现的关键字采用了flag标记法,用isComment记录当前是否处于多行注释状态
而treeMap解决排序输出问题
完整代码代码如下
package pta.oop09;
import java.util.*;
import static java.lang.System.exit;
public class Main {
public static boolean isComment = false;//判断是否处于多行注释状态
public static void main(String[] args){
TreeMap<String,Integer> treeMap = initMap();//初始化treemap
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
if(str.equals("exit")){//判断是否为空
System.out.println("Wrong Format");
exit(0);
}
HashSet<String> setkey=new HashSet<>(treeMap.keySet());//便于遍历key
while(!str.matches("exit")){
str.trim();//去除tab和空格
if(str.startsWith("//")){//以单行注释为开头
}else{//正常输入
str = teimQutation(str);//去除”“内的内容
addValue(treeMap, str, setkey);//添加相应的值
}
str = scan.nextLine();
}
mapOut(treeMap, setkey);//输出关键词
}
private static void addValue(TreeMap<String, Integer> treeMap, String str, HashSet<String> setkey) {
if (str.contains("/*")) {
str = str.replace("/*"," /* ");//便于后续做分割
}
if (str.contains("*/")) {
str = str.replace("*/"," */ ");
}
String[] words = str.split("\\s+|\\(|\\)|\\.|\\[|]|\\{|,|;|\"");
for (int i = 0; i < words.length; i++) {
if (words[i].contains("//")){
return;
}
if (words[i].equals("/*")) {
isComment = true;
} else if (words[i].equals("*/")) {
isComment = false;
}
if (!isComment) {
iterate(treeMap, setkey, words, i);//遍历找键,加值
}
}
}
private static void iterate(TreeMap<String, Integer> treeMap, HashSet<String> setkey, String[] words, int i) {
Iterator iter = setkey.iterator(); //先获得所有key,再用迭代器遍历输出
while (iter.hasNext()) {
int time = 0;
String key = (String) iter.next();
if (words[i].equals(key)){
time++;
}
treeMap.put(key, treeMap.get(key) + time);//值加1操作
}
}
private static String teimQutation(String str) {
StringBuilder sb = new StringBuilder(str);
while(sb.indexOf("\"")>=0){
int indexFirst = sb.indexOf("\"");
int indexSecond = sb.indexOf("\"",indexFirst+1);
sb.replace(indexFirst,indexSecond+1," ");//将字符串中的数据清空
}
return sb.toString();
}
private static void mapOut(TreeMap<String, Integer> treeMap, HashSet<String> setkey) {
if(!treeMap.isEmpty()){//输出
Iterator<Map.Entry<String,Integer>> it = treeMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Integer> entry = it.next();
if (entry.getValue() > 0) {
System.out.println(entry.getValue() + "\t" + entry.getKey());
}
}
}
}
public static TreeMap<String,Integer> initMap(){
String[] keyWordString = {"abstract", "assert", "boolean", "break", "byte", "case", "catch",//记录53个关键词
"char", "class", "const", "continue", "default", "do", "double", "else", "enum",
"extends", "for", "final", "finally", "float", "goto", "if", "implements", "import",
"instanceof", "int", "interface", "long", "native", "new", "package", "private",
"protected", "public", "return", "short", "static", "strictfp", "super", "switch",
"synchronized", "this", "throw", "throws", "transient", "try", "void", "volatile",
"while", "true", "false", "null"};
TreeMap<String,Integer> treeMap = new TreeMap<>();//用treemap因为键后需排序
for (String words:keyWordString) {
treeMap.put(words,0);//初始化map
}
return treeMap;
}
}
相关复杂度解析反思

不足:写代码时整体把握不足,敲打随意,没有做好前期的准备,对代码无进行性能优化
类图展示:

10-1 课程成绩统计程序-1
先读题并简单分析,列出任务:
- 按情况分别存入课程信息和选课信息
- 对信息进行异常判断,存入正确的信息
- 学生,课程,班级的平均分的计算
- 将学生,课程,班级按平均分分别排序
- 将学生,课程,班级按平均分分别输出
我的大致思路,先考虑好一般情况,并实现,再将异常插入
学生,课程,班级数据用Arraylist储存
代码整体分为4个部分
在isValidFormat方法中,针对WrongFormat的进行判断
信息进行异常判断,存入正确的信息:dataInput
正确的选课信息导入学生,课程,班级中:dataStore
平均分的排序:dataSort
输出:dataOutput
完整代码如下:
import java.text.Collator;
import java.util.*;
class Student{
private int id;
private String name;
private ArrayList<Score> scores = new ArrayList<Score>();//记录所有成绩
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public ArrayList<Score> getScores() {
return scores;
}
public int getAverageScore(){//取平均值
int add = 0;
int count = 0;
for (int i = 0; i < scores.size(); i++) {
count++;
add+=scores.get(i).Scores;
}
if (count == 0) {
return -1;
} else {
return add / count;
}
}
}
class Class{
private String classId;
private ArrayList<Student> classments = new ArrayList<Student>();//存储一个班所有学生
public Class(String classId, Student student) {
this.classId = classId;
this.classments.add(student);
}
public String getClassId() {
return classId;
}
public ArrayList<Student> getClassments() {
return classments;
}
public int getAverageScore(){
int add = 0;
int count = 0;
for (int i = 0; i < classments.size(); i++) {
if (!classments.get(i).getScores().isEmpty()) {
count++;
add += classments.get(i).getAverageScore();
}
}
if (count == 0) {
return -1;
} else {
return add / count;
}
}
}
class Course{
private String name;
private String nature;//性质
private String method;//方法
private ArrayList<Score> scores = new ArrayList<Score>();
public Course(String name, String nature, String method) {
this.name = name;
this.nature = nature;
this.method = method;
}
public String getName() {
return name;
}
public String getMethod() {
return method;
}
public ArrayList<Score> getScores() {
return scores;
}
public int getAverageGRE(){
int add = 0;
for (int i = 0; i < scores.size(); i++) {
if (scores.get(i).returnMethod().equals("考试")){
TextScore temp = (TextScore)(scores.get(i));
add+=temp.getGRE();
}else{
InspectScore temp = (InspectScore)(scores.get(i));//向下转型为了调用子类特有方法
add+=temp.getGRE();
}
}
return add/scores.size();
}
public int getAverageGPA(){
int add = 0;
int count = 0;
for (int i = 0; i < scores.size(); i++) {
if (scores.get(i).returnMethod().equals("考试")){
TextScore temp = (TextScore)(scores.get(i));
count++;
add+=temp.getGPA();
}
}
if (count == 0) {
return -1;
} else {
return add / count;
}
}
public int getAverageScore() {
int add = 0;
int count = 0;
for (int i = 0; i < scores.size(); i++) {
if (scores.get(i).returnMethod().equals("考察") && method.equals("考试") || scores.get(i).returnMethod().equals("考试") && method.equals("考察")) {
continue;
} else {
count++;
add += scores.get(i).Scores;
}
}
if (count == 0) {
return -1;
} else {
return add / count;
}
}
public boolean isDataMisMatch(String[] data){
return data.length == 4 &&getMethod().equals("考试") || data.length == 5 && getMethod().equals("考察");
}
}
abstract class Score{
protected int Scores;//总成绩
public int getScores() {
return Scores;
}
abstract public String returnMethod();//获取成绩类型
}
class TextScore extends Score{
private int GPA;//平时成绩
private int GRE;//考试成绩
public TextScore(int GPA, int GRE) {
this.GPA = GPA;
this.GRE = GRE;
Scores = (int) (0.3*GPA+0.7*GRE);
}
public int getGPA() {
return GPA;
}
public int getGRE() {
return GRE;
}
@Override
public String returnMethod() {
return "考试";
}
}
class InspectScore extends Score{
private int GRE;//考试成绩
public InspectScore(int GRE) {
this.GRE = GRE;
Scores = GRE;
}
public int getGRE() {
return GRE;
}
@Override
public String returnMethod() {
return "考察";
}
}
class Elective{
private Course course;
private Student student;
private Score score;
public Elective(Course course, Student student, Score score) {
this.course = course;
this.student = student;
this.score = score;
}
public Course getCourse() {
return course;
}
public Student getStudent() {
return student;
}
public Score getScore() {
return score;
}
public boolean isVilidData(){
if(getCourse().getMethod().equals("考试")&&getScore().returnMethod().equals("考察")||getCourse().getMethod().equals("考察")&&getScore().returnMethod().equals("考试")||getScore().getScores()<0){
return false;
}
return true;
}
}
public class Main {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
String[] data = scan.nextLine().split(" ");//存输入信息
ArrayList<Course> allCourse = new ArrayList<Course>();//存储所有课程
ArrayList<Student> allStudent = new ArrayList<Student>();//存储所有学生
ArrayList<Class> allClass = new ArrayList<Class>();//存储所有班级
ArrayList<Elective> allElective = new ArrayList<Elective>();//存储所有选课信息
dataInput(scan, data, allCourse, allElective);
dataStore(allCourse, allStudent, allClass, allElective);
dataSort(allCourse, allStudent, allClass);
dataOutput(allCourse, allStudent, allClass);
}
private static void dataOutput(ArrayList<Course> allCourse, ArrayList<Student> allStudent, ArrayList<Class> allClass) {
//对学生,课程,班级分别处理
for (int i = 0; i < allStudent.size(); i++) {
if (allStudent.get(i).getScores().isEmpty()){
System.out.println(allStudent.get(i).getId()+" "+ allStudent.get(i).getName()+" did not take any exams");
}else{
System.out.println(allStudent.get(i).getId()+" "+ allStudent.get(i).getName()+" "+ allStudent.get(i).getAverageScore());
}
}
for (int i = 0; i < allCourse.size(); i++) {
if (allCourse.get(i).getScores().isEmpty()||allCourse.get(i).getAverageScore()==-1){
System.out.println(allCourse.get(i).getName()+" "+"has no grades yet");
}else {
if (allCourse.get(i).getMethod().equals("考试")) {
System.out.println(allCourse.get(i).getName() + " " + allCourse.get(i).getAverageGPA() + " " + allCourse.get(i).getAverageGRE() + " " + allCourse.get(i).getAverageScore());
} else {
System.out.println(allCourse.get(i).getName() + " " + allCourse.get(i).getAverageGRE() + " " + allCourse.get(i).getAverageScore());
}
}
}
for (int i = 0; i < allClass.size(); i++) {
if (allClass.get(i).getClassments().isEmpty()||allClass.get(i).getAverageScore()==-1) {
System.out.println(allClass.get(i).getClassId()+" has no grades yet");
} else {
System.out.println(allClass.get(i).getClassId() + " " + allClass.get(i).getAverageScore());
}
}
}
private static void dataSort(ArrayList<Course> allCourse, ArrayList<Student> allStudent, ArrayList<Class> allClass) {
//对学生,课程,班级分别处理
if (!allStudent.isEmpty()) {
studentSort(allStudent);
}
if(!allCourse.isEmpty()){
courseSort(allCourse);
}
if(!allClass.isEmpty()){
classSort(allClass);
}
}
private static void dataStore(ArrayList<Course> allCourse, ArrayList<Student> allStudent, ArrayList<Class> allClass, ArrayList<Elective> allElective) {
//对学生,课程,班级分别处理
for (int i = 0; i < allElective.size(); i++) {
for (int j = 0; j < allCourse.size(); j++) {
if (allElective.get(i).getCourse().getName().equals(allCourse.get(j).getName())){
allCourse.get(j).getScores().add(allElective.get(i).getScore());
}
}
}
for (int i = 0; i < allElective.size(); i++) {
boolean isExist = false;
for (int j = 0; j < allStudent.size(); j++) {
if (allElective.get(i).getStudent().getId()==allStudent.get(j).getId()){
if (allElective.get(i).isVilidData())
allStudent.get(j).getScores().add(allElective.get(i).getScore());
isExist = true;
}
}
if (!isExist) {
if (allElective.get(i).isVilidData()) {
allElective.get(i).getStudent().getScores().add(allElective.get(i).getScore());
}
allStudent.add(allElective.get(i).getStudent());
}
}
for (int i = 0; i < allStudent.size(); i++) {
boolean isExist = false;
for (int j = 0; j < allClass.size(); j++) {
if (allClass.get(j).getClassId().equals((""+ allStudent.get(i).getId()).substring(0,6))){//转为String比较
allClass.get(j).getClassments().add(allStudent.get(i));
isExist = true;
}
}
if (!isExist){
allClass.add(new Class((""+ allStudent.get(i).getId()).substring(0,6), allStudent.get(i)));
}
}
}
private static void dataInput(Scanner scan, String[] data, ArrayList<Course> allCourse, ArrayList<Elective> allElective) {
//对学生,课程,班级分别处理
while(!data[0].equals("end")) {
if(!isValidFormat(data)){//判断格式
System.out.println("wrong format");
data = scan.nextLine().split(" ");//存输入信息
continue;
}
if (data.length == 2&&data[1].equals("必修")){
allCourse.add(new Course(data[0], data[1], "考试"));
}
if (data.length == 3) {
//课程输入
if (data[1].equals("必修")&& data[2].equals("考察")) {//判断输入时课程性质和方式是否匹配
System.out.println(data[0] + " : course type & access mode mismatch");
}else if (isRepeatCrouse(allCourse, data)){
//空
}else{
allCourse.add(new Course(data[0], data[1], data[2]));
}
} else if (data.length == 4 || data.length == 5) {
//有关选课信息的输入
boolean IsCourseExist = false;
boolean IsMisMatch = false;
if (isRepeatData(allElective, data)){
data = scan.nextLine().split(" ");//是否重复
continue;
}
for (int i = 0; i < allCourse.size(); i++) {
if (data[2].equals(allCourse.get(i).getName())) {
if (allCourse.get(i).isDataMisMatch(data)) {//判断输入时课程性质.方式和所给成绩是否匹配
IsMisMatch = true;
wrongStore(data, allElective);
IsCourseExist = true;
break;
}
rightStore(data, allCourse, allElective, i);//储存该选课
IsCourseExist = true;
}
}
if (!IsCourseExist){
System.out.println(data[2]+ " does not exist");
wrongStore(data, allElective);
} else if (IsMisMatch) {
System.out.println(data[0] + " " + data[1] + " : access mode mismatch");
}
}
data = scan.nextLine().split(" ");//存输入信息
}
}
private static void wrongStore(String[] data, ArrayList<Elective> allElective) {
if (data.length==5) {
allElective.add(new Elective(new Course("null","null","null"), new Student(Integer.parseInt(data[0]), data[1]), new TextScore(-1, -1)));
} else if(data.length==4){
allElective.add(new Elective(new Course("null","null","null"), new Student(Integer.parseInt(data[0]), data[1]), new InspectScore(-1)));
}
}
private static boolean isValidFormat(String[] data) {//信息错误
if (data.length == 2&&data[1].equals("必修")){
return true;
} else if (data.length == 3){
if (data[0].length()>10) {
return false;
}
if (!data[1].equals("必修")&&!data[1].equals("选修")){
return false;
}
if (!data[2].equals("考试")&&!data[2].equals("考察")){
return false;
}
} else if (data.length==4) {
if (data[0].length()!=8) {
return false;
}
if (data[1].length()>10) {
return false;
}
if (Integer.parseInt(data[3])>100||Integer.parseInt(data[3])<0) {
return false;
}
} else if (data.length==5) {
if (data[0].length()!=8||data[1].length()>10) {
return false;
}
if (!data[3].matches("[0-9]+")||!data[4].matches("[0-9]+")){
return false;
}
if (Integer.parseInt(data[3])>100||Integer.parseInt(data[3])<0||Integer.parseInt(data[4])>100||Integer.parseInt(data[4])<0) {
return false;
}
}else{
return false;
}
return true;
}
private static void studentSort(ArrayList<Student> allStudent) {
for (int i = 0; i < allStudent.size()-1; i++) {
for (int j = 0; j < allStudent.size()-i-1; j++) {
if(allStudent.get(j).getId()>allStudent.get(j+1).getId()){
Collections.swap(allStudent, j, j+1);
}
}
}
}
private static void classSort(ArrayList<Class> allClass) {
for (int i = 0; i < allClass.size()-1; i++) {
for (int j = 0; j < allClass.size()-i-1; j++) {
if(allClass.get(j).getClassId().compareTo(allClass.get(j+1).getClassId())>0){
Collections.swap(allClass, j, j+1);
}
}
}
}
private static void courseSort(ArrayList<Course> allCourse) {
Collections.sort(allCourse, new Comparator<Course>() {
@Override
public int compare(Course o1, Course o2) {
Collator collator = Collator.getInstance(Locale.CHINA);
return collator.compare(o1.getName(), o2.getName());
}
});
}
private static void rightStore(String[] data, ArrayList<Course> allCourse, ArrayList<Elective> allElective, int i) {
if (data.length==5) {
allElective.add(new Elective(allCourse.get(i), new Student(Integer.parseInt(data[0]), data[1]), new TextScore(Integer.parseInt(data[3]), Integer.parseInt(data[4]))));
} else{
allElective.add(new Elective(allCourse.get(i), new Student(Integer.parseInt(data[0]), data[1]), new InspectScore(Integer.parseInt(data[3]))));
}
}
public static boolean isRepeatCrouse(ArrayList<Course> allCourse,String[] data){
for (int i = 0; i < allCourse.size(); i++) {
if (allCourse.get(i).getName().equals(data[0])){
return true;
}
}
return false;
}
public static boolean isRepeatData(ArrayList<Elective> allElectiveCourse,String[] data){
for (int i = 0; i < allElectiveCourse.size(); i++) {
if (allElectiveCourse.get(i).getStudent().getId()==Integer.parseInt(data[0])&&allElectiveCourse.get(i).getCourse().getName().equals(data[2])){
return true;
}
}
return false;
}
}
相关复杂度分析:

不足:对于主函数承担了过多的方法,一些功能未能放到更适合的位置,
通过将一些功能性的代码提取到其他类中,我们可以使Main类更加简洁和易于阅读如Error类和Contorl类中
相关类图:

11-1课程成绩统计程序-2
课程成绩统计程序-2在第一次的基础上增加了实验课
完整代码如下:
import java.text.Collator;
import java.util.*;
class Student {
private int id;
private String name;
private ArrayList<Score> scores = new ArrayList<>();//记录所有成绩
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public ArrayList<Score> getScores() {
return scores;
}
public int getAverageScore() {//取平均值
int add = 0;
int count = 0;
for (int i = 0; i < scores.size(); i++) {
count++;
add += scores.get(i).Scores;
}
if (count == 0) {
return -1;
} else {
return add / count;
}
}
}
class Class {
private String classId;
private ArrayList<Student> classments = new ArrayList<>();//存储一个班所有学生
public Class(String classId, Student student) {
this.classId = classId;
this.classments.add(student);
}
public String getClassId() {
return classId;
}
public ArrayList<Student> getClassments() {
return classments;
}
public int getAverageScore() {
int add = 0;
int count = 0;
for (int i = 0; i < classments.size(); i++) {
if (!classments.get(i).getScores().isEmpty()) {
count++;
add += classments.get(i).getAverageScore();
}
}
if (count == 0) {
return -1;
} else {
return add / count;
}
}
}
class Course {
private String name;
private String nature;//性质
private String method;//方法
private ArrayList<Score> scores = new ArrayList<>();
public Course(String name, String nature, String method) {
this.name = name;
this.nature = nature;
this.method = method;
}
public String getName() {
return name;
}
public String getMethod() {
return method;
}
public ArrayList<Score> getScores() {
return scores;
}
public int getAverageGRE() {
int add = 0;
for (int i = 0; i < scores.size(); i++) {
if (scores.get(i).returnMethod().equals("考试")) {
TextScore temp = (TextScore) (scores.get(i));
add += temp.getGRE();
} else if (scores.get(i).returnMethod().equals("考察")) {
InspectScore temp = (InspectScore) (scores.get(i));//向下转型为了调用子类特有方法
add += temp.getGRE();
}
}
return add / scores.size();
}
public int getAverageGPA() {
int add = 0;
int count = 0;
for (int i = 0; i < scores.size(); i++) {
if (scores.get(i).returnMethod().equals("考试")) {
TextScore temp = (TextScore) (scores.get(i));
count++;
add += temp.getGPA();
}
}
if (count == 0) {
return -1;
} else {
return add / count;
}
}
public int getAverageScore() {
int add = 0;
int count = 0;
for (int i = 0; i < scores.size(); i++) {
if (scores.get(i).returnMethod().equals("考察") && method.equals("考试") || scores.get(i).returnMethod().equals("考试") && method.equals("考察")) {
} else {
count++;
add += scores.get(i).Scores;
}
}
if (count == 0) {
return -1;
} else {
return add / count;
}
}
public boolean isDataMisMatch(String[] data) {
return data.length == 4 && getMethod().equals("考试") || data.length == 5 && getMethod().equals("考察") || data.length > 5 && Integer.parseInt(data[3]) != data.length - 4;
}
}
abstract class Score {
protected int Scores;//总成绩
public int getScores() {
return Scores;
}
abstract public String returnMethod();//获取成绩类型
}
class TextScore extends Score {
private int GPA;//平时成绩
private int GRE;//考试成绩
public TextScore(int GPA, int GRE) {
this.GPA = GPA;
this.GRE = GRE;
Scores = (int) (0.3 * GPA + 0.7 * GRE);
}
public int getGPA() {
return GPA;
}
public int getGRE() {
return GRE;
}
@Override
public String returnMethod() {
return "考试";
}
}
class InspectScore extends Score {
private int GRE;//考试成绩
public InspectScore(int GRE) {
this.GRE = GRE;
Scores = GRE;
}
public int getGRE() {
return GRE;
}
@Override
public String returnMethod() {
return "考察";
}
}
class ExperimentScore extends Score {
private ArrayList<Integer> experimentScore;//成绩
public ExperimentScore() {
Scores = -1;
}
public ExperimentScore(ArrayList<Integer> experimentScore) {
this.experimentScore = experimentScore;
int all = 0;
for (int i = 0; i < experimentScore.size(); i++) {
all += this.experimentScore.get(i);
}
Scores = all / experimentScore.size();
}
@Override
public String returnMethod() {
return "考察";
}
}
class Elective {
private Course course;
private Student student;
private Score score;
public Elective(Course course, Student student, Score score) {
this.course = course;
this.student = student;
this.score = score;
}
public Course getCourse() {
return course;
}
public Student getStudent() {
return student;
}
public Score getScore() {
return score;
}
public boolean isValidData() {
if (getCourse().getMethod().equals("考试") && getScore().returnMethod().equals("考察") || getCourse().getMethod().equals("考察") && getScore().returnMethod().equals("考试") || getScore().getScores() < 0) {
return false;
}
return true;
}
}
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String[] data = scan.nextLine().split(" ");//存输入信息
ArrayList<Course> allCourse = new ArrayList<>();//存储所有课程
ArrayList<Student> allStudent = new ArrayList<>();//存储所有学生
ArrayList<Class> allClass = new ArrayList<>();//存储所有班级
ArrayList<Elective> allElective = new ArrayList<>();//存储所有选课信息
dataInput(scan, data, allCourse, allElective);
dataStore(allCourse, allStudent, allClass, allElective);
dataSort(allCourse, allStudent, allClass);
dataOutput(allCourse, allStudent, allClass);
}
private static void dataOutput(ArrayList<Course> allCourse, ArrayList<Student> allStudent, ArrayList<Class> allClass) {
//对学生,课程,班级分别处理
for (int i = 0; i < allStudent.size(); i++) {
if (allStudent.get(i).getScores().isEmpty()) {
System.out.println(allStudent.get(i).getId() + " " + allStudent.get(i).getName() + " did not take any exams");
} else {
System.out.println(allStudent.get(i).getId() + " " + allStudent.get(i).getName() + " " + allStudent.get(i).getAverageScore());
}
}
for (int i = 0; i < allCourse.size(); i++) {
if (allCourse.get(i).getScores().isEmpty() || allCourse.get(i).getAverageScore() == -1) {
System.out.println(allCourse.get(i).getName() + " " + "has no grades yet");
} else {
if (allCourse.get(i).getMethod().equals("考试")) {
System.out.println(allCourse.get(i).getName() + " " + allCourse.get(i).getAverageGPA() + " " + allCourse.get(i).getAverageGRE() + " " + allCourse.get(i).getAverageScore());
} else if (allCourse.get(i).getMethod().equals("考察")) {
System.out.println(allCourse.get(i).getName() + " " + allCourse.get(i).getAverageGRE() + " " + allCourse.get(i).getAverageScore());
} else {
System.out.println(allCourse.get(i).getName() + " " + allCourse.get(i).getAverageScore());
}
}
}
for (int i = 0; i < allClass.size(); i++) {
if (allClass.get(i).getClassments().isEmpty() || allClass.get(i).getAverageScore() == -1) {
System.out.println(allClass.get(i).getClassId() + " has no grades yet");
} else {
System.out.println(allClass.get(i).getClassId() + " " + allClass.get(i).getAverageScore());
}
}
}
private static void dataSort(ArrayList<Course> allCourse, ArrayList<Student> allStudent, ArrayList<Class> allClass) {
//对学生,课程,班级分别处理
if (!allStudent.isEmpty()) {
studentSort(allStudent);
}
if (!allCourse.isEmpty()) {
courseSort(allCourse);
}
if (!allClass.isEmpty()) {
classSort(allClass);
}
}
private static void dataStore(ArrayList<Course> allCourse, ArrayList<Student> allStudent, ArrayList<Class> allClass, ArrayList<Elective> allElective) {
//对学生,课程,班级分别处理
for (int i = 0; i < allElective.size(); i++) {
for (int j = 0; j < allCourse.size(); j++) {
if (allElective.get(i).getCourse().getName().equals(allCourse.get(j).getName())) {
allCourse.get(j).getScores().add(allElective.get(i).getScore());
}
}
}
for (int i = 0; i < allElective.size(); i++) {
boolean isExist = false;
for (int j = 0; j < allStudent.size(); j++) {
if (allElective.get(i).getStudent().getId() == allStudent.get(j).getId()) {
if (allElective.get(i).isValidData())
allStudent.get(j).getScores().add(allElective.get(i).getScore());
isExist = true;
}
}
if (!isExist) {
if (allElective.get(i).isValidData()) {
allElective.get(i).getStudent().getScores().add(allElective.get(i).getScore());
}
allStudent.add(allElective.get(i).getStudent());
}
}
for (int i = 0; i < allStudent.size(); i++) {
boolean isExist = false;
for (int j = 0; j < allClass.size(); j++) {
if (allClass.get(j).getClassId().equals(("" + allStudent.get(i).getId()).substring(0, 6))) {//转为String比较
allClass.get(j).getClassments().add(allStudent.get(i));
isExist = true;
}
}
if (!isExist) {
allClass.add(new Class(("" + allStudent.get(i).getId()).substring(0, 6), allStudent.get(i)));
}
}
}
private static void dataInput(Scanner scan, String[] data, ArrayList<Course> allCourse, ArrayList<Elective> allElective) {
//对学生,课程,班级分别处理
while (!data[0].equals("end")) {
//判断格式
if (!isValidFormat(data)) {
System.out.println("wrong format");
data = scan.nextLine().split(" ");//存输入信息
continue;
}
//信息输入
if (data.length == 2 && data[1].equals("必修")) {
allCourse.add(new Course(data[0], data[1], "考试"));
}else if (data.length == 3) {
if (data[1].equals("必修") && data[2].equals("考察") || !data[1].equals("实验") && data[2].equals("实验") || data[1].equals("实验") && !data[2].equals("实验")) {//判断输入时课程性质和方式是否匹配
System.out.println(data[0] + " : course type & access mode mismatch");
} else if (isRepeatCourse(allCourse, data)) {
} else {//课程输入
allCourse.add(new Course(data[0], data[1], data[2]));
}
} else if (data.length >= 4) {
//是否重复
if (isRepeatData(allElective, data)) {
data = scan.nextLine().split(" ");
continue;
}
//有关选课信息的输入
dataChooseInput(data, allCourse, allElective);
}
data = scan.nextLine().split(" ");//存输入信息
}
}
private static void dataChooseInput(String[] data, ArrayList<Course> allCourse, ArrayList<Elective> allElective) {
boolean IsCourseExist = false;
boolean IsMisMatch = false;
for (int i = 0; i < allCourse.size(); i++) {
if (data[2].equals(allCourse.get(i).getName())) {
if (allCourse.get(i).isDataMisMatch(data)) {//判断输入时课程性质.方式和所给成绩是否匹配
IsMisMatch = true;
wrongStore(data, allElective);
IsCourseExist = true;
break;
}
rightStore(data, allCourse, allElective, i);//储存该选课
IsCourseExist = true;
}
}
wrongData(data, allElective, IsCourseExist, IsMisMatch);
}
private static void wrongData(String[] data, ArrayList<Elective> allElective, boolean IsCourseExist, boolean IsMisMatch) {
if (!IsCourseExist) {
System.out.println(data[2] + " does not exist");
wrongStore(data, allElective);
} else if (IsMisMatch) {
System.out.println(data[0] + " " + data[1] + " : access mode mismatch");
}
}
private static void wrongStore(String[] data, ArrayList<Elective> allElective) {
if (data.length == 5) {
allElective.add(new Elective(new Course("null", "null", "null"), new Student(Integer.parseInt(data[0]), data[1]), new TextScore(-1, -1)));
} else if (data.length == 4) {
allElective.add(new Elective(new Course("null", "null", "null"), new Student(Integer.parseInt(data[0]), data[1]), new InspectScore(-1)));
} else {
allElective.add(new Elective(new Course("null", "null", "null"), new Student(Integer.parseInt(data[0]), data[1]), new ExperimentScore()));
}
}
private static boolean isValidFormat(String[] data) {//信息错误
if (data.length == 2 && data[1].equals("必修")) {
return true;
} else if (data.length == 3) {
if (data[0].length() > 10) {
return false;
}
if (!data[1].equals("必修") && !data[1].equals("选修") && !data[1].equals("实验")) {
return false;
}
if (!data[2].equals("考试") && !data[2].equals("考察") && !data[2].equals("实验")) {
return false;
}
} else if (data.length == 4) {
if (data[0].length() != 8) {
return false;
}
if (data[1].length() > 10) {
return false;
}
if (Integer.parseInt(data[3]) > 100 || Integer.parseInt(data[3]) < 0) {
return false;
}
} else if (data.length == 5) {
if (data[0].length() != 8 || data[1].length() > 10) {
return false;
}
if (!data[3].matches("[0-9]+") || !data[4].matches("[0-9]+")) {
return false;
}
if (Integer.parseInt(data[3]) > 100 || Integer.parseInt(data[3]) < 0 || Integer.parseInt(data[4]) > 100 || Integer.parseInt(data[4]) < 0) {
return false;
}
} else if (data.length >= 5 && data.length <= 13) {
if (data[0].length() != 8 || data[1].length() > 10) {
return false;
}
if (Integer.parseInt(data[3]) < 4 || Integer.parseInt(data[3]) > 9) {
return false;
}
for (int i = 4; i < data.length; i++) {
if (Integer.parseInt(data[i]) > 100 || Integer.parseInt(data[i]) < 0) {
return false;
}
}
} else {
return false;
}
return true;
}
private static void studentSort(ArrayList<Student> allStudent) {
for (int i = 0; i < allStudent.size() - 1; i++) {
for (int j = 0; j < allStudent.size() - i - 1; j++) {
if (allStudent.get(j).getId() > allStudent.get(j + 1).getId()) {
Collections.swap(allStudent, j, j + 1);
}
}
}
}
private static void classSort(ArrayList<Class> allClass) {
for (int i = 0; i < allClass.size() - 1; i++) {
for (int j = 0; j < allClass.size() - i - 1; j++) {
if (allClass.get(j).getClassId().compareTo(allClass.get(j + 1).getClassId()) > 0) {
Collections.swap(allClass, j, j + 1);
}
}
}
}
private static void courseSort(ArrayList<Course> allCourse) {
allCourse.sort((o1, o2) -> {
Collator collator = Collator.getInstance(Locale.CHINA);
return collator.compare(o1.getName(), o2.getName());
});
}
private static void rightStore(String[] data, ArrayList<Course> allCourse, ArrayList<Elective> allElective, int i) {
if (data.length == 5) {
allElective.add(new Elective(allCourse.get(i), new Student(Integer.parseInt(data[0]), data[1]), new TextScore(Integer.parseInt(data[3]), Integer.parseInt(data[4]))));
} else if (data.length == 4) {
allElective.add(new Elective(allCourse.get(i), new Student(Integer.parseInt(data[0]), data[1]), new InspectScore(Integer.parseInt(data[3]))));
} else {
ArrayList<Integer> experiment = new ArrayList<>();
for (int j = 4; j < data.length; j++) {
experiment.add(Integer.valueOf(data[j]));
}
allElective.add(new Elective(allCourse.get(i), new Student(Integer.parseInt(data[0]), data[1]), new ExperimentScore(experiment)));
}
}
public static boolean isRepeatCourse(ArrayList<Course> allCourse, String[] data) {
for (int i = 0; i < allCourse.size(); i++) {
if (allCourse.get(i).getName().equals(data[0])) {
return true;
}
}
return false;
}
public static boolean isRepeatData(ArrayList<Elective> allElectiveCourse, String[] data) {
for (int i = 0; i < allElectiveCourse.size(); i++) {
if (allElectiveCourse.get(i).getStudent().getId() == Integer.parseInt(data[0]) && allElectiveCourse.get(i).getCourse().getName().equals(data[2])) {
return true;
}
}
return false;
}
}
相关复杂度分析:


与10-1差别不大
不足:在对10-1进行修改加实验功能时,发现10-1中的代码块分工有点混乱
相关类图:

12-1 课程成绩统计程序-3
要求:
- 修改类结构,将成绩类的继承关系改为组合关系,成绩信息由课程成绩类和分项成绩类组成,课程成绩类组合分项成绩类,分项成绩类由成绩分值和权重两个属性构成。
- 完成课程成绩统计程序-2、3两次程序后,比较继承和组合关系的区别。思考一下哪一种关系运用上更灵活,更能够适应变更。
11-1中有关Score类的代码:
abstract class Score {
protected int Scores;//总成绩
public int getScores() {
return Scores;
}
abstract public String returnMethod();//获取成绩类型
}
class TextScore extends Score {
private int GPA;//平时成绩
private int GRE;//考试成绩
public TextScore(int GPA, int GRE) {
this.GPA = GPA;
this.GRE = GRE;
Scores = (int) (0.3 * GPA + 0.7 * GRE);
}
public int getGPA() {
return GPA;
}
public int getGRE() {
return GRE;
}
@Override
public String returnMethod() {
return "考试";
}
}
class InspectScore extends Score {
private int GRE;//考试成绩
public InspectScore(int GRE) {
this.GRE = GRE;
Scores = GRE;
}
public int getGRE() {
return GRE;
}
@Override
public String returnMethod() {
return "考察";
}
}
class ExperimentScore extends Score {
private ArrayList<Integer> experimentScore;//成绩
public ExperimentScore() {
Scores = -1;
}
public ExperimentScore(ArrayList<Integer> experimentScore) {
this.experimentScore = experimentScore;
int all = 0;
for (int i = 0; i < experimentScore.size(); i++) {
all += this.experimentScore.get(i);
}
Scores = all / experimentScore.size();
}
@Override
public String returnMethod() {
return "考察";
}
}
12-1中有关Score类的代码:
class CourseScore {
private ArrayList<ItemizedScore> itemizedScores = new ArrayList<>();
public CourseScore() {
}
public CourseScore(float weight, int score) {
itemizedScores.add(new ItemizedScore(weight, score));
}
public ArrayList<ItemizedScore> getItemizedScores() {
return itemizedScores;
}
public int getScores() {
float add = 0;
for (int i = 0; i < itemizedScores.size(); i++) {
add += itemizedScores.get(i).getWeighedtScore();
}
if (itemizedScores.size() == 0) {
return -1;
}
return (int) add;
}
}
class ItemizedScore {
private float weight;
private int score = -1;
public ItemizedScore(float weight, int score) {
this.weight = weight;
this.score = score;
}
public ItemizedScore(float weight) {
this.weight = weight;
}
public ItemizedScore(int score) {
this.score = score;
}
public float getWeighedtScore() {
return weight * score;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
完整代码如下:
import java.text.Collator;
import java.util.*;
class Student {
private int id;
private String name;
private ArrayList<CourseScore> scores = new ArrayList<>();//记录所有成绩
public Student(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public ArrayList<CourseScore> getScores() {
return scores;
}
public int getAverageScore() {//取平均值
float add = 0;
int count = 0;
for (int i = 0; i < scores.size(); i++) {
count++;
add += scores.get(i).getScores();
}
if (count == 0) {
return -1;
} else {
return (int) (add / count);
}
}
}
class Class {
private String classId;
private ArrayList<Student> classments = new ArrayList<>();//存储一个班所有学生
public Class(String classId, Student student) {
this.classId = classId;
this.classments.add(student);
}
public String getClassId() {
return classId;
}
public ArrayList<Student> getClassments() {
return classments;
}
public int getAverageScore() {
int add = 0;
int count = 0;
for (int i = 0; i < classments.size(); i++) {
if (!classments.get(i).getScores().isEmpty() && !classments.get(i).getScores().get(0).getItemizedScores().isEmpty()) {
count++;
add += classments.get(i).getAverageScore();
}
}
if (count == 0) {
return -1;
} else {
return add / count;
}
}
}
class Course {
private String name;
private String nature;//性质
private String method;//方法
private ArrayList<CourseScore> scores = new ArrayList<>();
public Course(String name, String nature, String method) {
this.name = name;
this.nature = nature;
this.method = method;
CourseScore temp = new CourseScore();
temp.getItemizedScores().add(new ItemizedScore(1.0F));
scores.add(temp);
}
public Course(String name, String nature, String method, String[] data) {
this.name = name;
this.nature = nature;
this.method = method;
CourseScore temp = new CourseScore();
if (method.equals("考试")) {
for (int i = 3; i < data.length; i++) {
temp.getItemizedScores().add(new ItemizedScore(Float.parseFloat(data[i])));//加weight
}
} else {
for (int i = 4; i < data.length; i++) {
temp.getItemizedScores().add(new ItemizedScore(Float.parseFloat(data[i])));//加weight
}
}
scores.add(temp);
}
public String getName() {
return name;
}
public String getMethod() {
return method;
}
public ArrayList<CourseScore> getScores() {
return scores;
}
public void addScores(CourseScore score) {
if (scores.get(0).getItemizedScores().size() != score.getItemizedScores().size()) {
} else if (scores.size() == 1 && scores.get(0).getItemizedScores().get(0).getScore() == -1) {
for (int i = 0; i < scores.get(0).getItemizedScores().size(); i++) {
scores.get(0).getItemizedScores().get(i).setScore(score.getItemizedScores().get(i).getScore());
score.getItemizedScores().get(i).setWeight(scores.get(0).getItemizedScores().get(i).getWeight());
}
} else {
scores.add(new CourseScore(scores.get(0).getItemizedScores().get(0).getWeight(), score.getItemizedScores().get(0).getScore()));
score.getItemizedScores().get(0).setWeight(scores.get(0).getItemizedScores().get(0).getWeight());
for (int i = 1; i < scores.get(0).getItemizedScores().size(); i++) {
scores.get(scores.size() - 1).getItemizedScores().add(new ItemizedScore(scores.get(0).getItemizedScores().get(i).getWeight(), score.getItemizedScores().get(i).getScore()));
score.getItemizedScores().get(i).setWeight(scores.get(0).getItemizedScores().get(i).getWeight());
}
}
}
public int getAverageScore() {
int add = 0;
int count = 0;
for (int i = 0; i < scores.size(); i++) {
count++;
add += scores.get(i).getScores();
}
if (count == 0) {
return -1;
} else {
return add / count;
}
}
public boolean isDataMisMatch(String[] data) {
if ((data.length - 3 != scores.get(0).getItemizedScores().size()) && getMethod().equals("实验")) {
return true;
}
return data.length != 5 && getMethod().equals("考试") || data.length != 4 && getMethod().equals("考察");
}
}
class CourseScore {
private ArrayList<ItemizedScore> itemizedScores = new ArrayList<>();
public CourseScore() {
}
public CourseScore(float weight, int score) {
itemizedScores.add(new ItemizedScore(weight, score));
}
public ArrayList<ItemizedScore> getItemizedScores() {
return itemizedScores;
}
public int getScores() {
float add = 0;
for (int i = 0; i < itemizedScores.size(); i++) {
add += itemizedScores.get(i).getWeighedtScore();
}
if (itemizedScores.size() == 0) {
return -1;
}
return (int) add;
}
}
class ItemizedScore {
private float weight;
private int score = -1;
public ItemizedScore(float weight, int score) {
this.weight = weight;
this.score = score;
}
public ItemizedScore(float weight) {
this.weight = weight;
}
public ItemizedScore(int score) {
this.score = score;
}
public float getWeighedtScore() {
return weight * score;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
class Elective {
private Course course;
private Student student;
private CourseScore score = new CourseScore();
public Elective(Course course, Student student, String[] data) {
this.course = course;
this.student = student;
for (int i = 3; i < data.length; i++) {
ItemizedScore temp = new ItemizedScore(Integer.parseInt(data[i]));
score.getItemizedScores().add(temp);
}
}
public Elective(Course course, Student student) {
this.course = course;
this.student = student;
}
public Course getCourse() {
return course;
}
public Student getStudent() {
return student;
}
public CourseScore getScore() {
return score;
}
}
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String[] data = scan.nextLine().split(" ");//存输入信息
ArrayList<Course> allCourse = new ArrayList<>();//存储所有课程
ArrayList<Student> allStudent = new ArrayList<>();//存储所有学生
ArrayList<Class> allClass = new ArrayList<>();//存储所有班级
ArrayList<Elective> allElective = new ArrayList<>();//存储所有选课信息
dataInput(scan, data, allCourse, allElective);
dataStore(allCourse, allStudent, allClass, allElective);
dataSort(allCourse, allStudent, allClass);
dataOutput(allCourse, allStudent, allClass);
}
private static void dataOutput(ArrayList<Course> allCourse, ArrayList<Student> allStudent, ArrayList<Class> allClass) {
//对学生,课程,班级分别处理
for (int i = 0; i < allStudent.size(); i++) {
if (allStudent.get(i).getScores().isEmpty() || allStudent.get(i).getAverageScore() < 0) {
System.out.println(allStudent.get(i).getId() + " " + allStudent.get(i).getName() + " did not take any exams");
} else {
System.out.println(allStudent.get(i).getId() + " " + allStudent.get(i).getName() + " " + allStudent.get(i).getAverageScore());
}
}
for (int i = 0; i < allCourse.size(); i++) {
if (allCourse.get(i).getScores().isEmpty() || allCourse.get(i).getAverageScore() < 0) {
System.out.println(allCourse.get(i).getName() + " " + "has no grades yet");
} else {
System.out.println(allCourse.get(i).getName() + " " + allCourse.get(i).getAverageScore());
}
}
for (int i = 0; i < allClass.size(); i++) {
if (allClass.get(i).getClassments().isEmpty() || allClass.get(i).getAverageScore() < 0) {
System.out.println(allClass.get(i).getClassId() + " has no grades yet");
} else {
System.out.println(allClass.get(i).getClassId() + " " + allClass.get(i).getAverageScore());
}
}
}
private static void dataSort(ArrayList<Course> allCourse, ArrayList<Student> allStudent, ArrayList<Class> allClass) {
//对学生,课程,班级分别处理
if (!allStudent.isEmpty()) {
studentSort(allStudent);
}
if (!allCourse.isEmpty()) {
courseSort(allCourse);
}
if (!allClass.isEmpty()) {
classSort(allClass);
}
}
private static void dataStore(ArrayList<Course> allCourse, ArrayList<Student> allStudent, ArrayList<Class> allClass, ArrayList<Elective> allElective) {
//对课程,学生,班级分别处理
for (int i = 0; i < allElective.size(); i++) {
for (int j = 0; j < allCourse.size(); j++) {
if (allElective.get(i).getCourse().getName().equals(allCourse.get(j).getName())) {
allCourse.get(j).addScores(allElective.get(i).getScore());
}
}
}
for (int i = 0; i < allElective.size(); i++) {
boolean isExist = false;
for (int j = 0; j < allStudent.size(); j++) {
if (allElective.get(i).getStudent().getId() == allStudent.get(j).getId()) {
allStudent.get(j).getScores().add(allElective.get(i).getScore());
isExist = true;
}
}
if (!isExist) {
allElective.get(i).getStudent().getScores().add(allElective.get(i).getScore());//给学生加成绩
allStudent.add(allElective.get(i).getStudent());
}
}
for (int i = 0; i < allStudent.size(); i++) {
boolean isExist = false;
for (int j = 0; j < allClass.size(); j++) {
if (allClass.get(j).getClassId().equals(("" + allStudent.get(i).getId()).substring(0, 6))) {//转为String比较
allClass.get(j).getClassments().add(allStudent.get(i));
isExist = true;
}
}
if (!isExist) {
allClass.add(new Class(("" + allStudent.get(i).getId()).substring(0, 6), allStudent.get(i)));
}
}
}
private static void dataInput(Scanner scan, String[] data, ArrayList<Course> allCourse, ArrayList<Elective> allElective) {
//对学生,课程,班级分别处理
while (!data[0].equals("end")) {
//判断格式
if (!isValidFormat(data)) {
data = scan.nextLine().split("\\s+");//存输入信息
continue;
}
//信息输入
if (data.length == 2 && data[1].equals("必修")) {
allCourse.add(new Course(data[0], data[1], "考试"));
} else if (data.length >= 3 && (data[1].equals("必修") || data[1].equals("选修") || data[1].equals("实验"))) {
if (!isRepeatCourse(allCourse, data)) {//课程输入
if (data[2].equals("考察")) {
allCourse.add(new Course(data[0], data[1], data[2]));
} else {
allCourse.add(new Course(data[0], data[1], data[2], data));
}
}
} else if (data.length >= 4) {
//是否重复
if (isRepeatData(allElective, data)) {
data = scan.nextLine().split("\\s+");
continue;
}
//有关选课信息的输入
dataChooseInput(data, allCourse, allElective);
}
data = scan.nextLine().split("\\s+");//存输入信息
}
}
private static void dataChooseInput(String[] data, ArrayList<Course> allCourse, ArrayList<Elective> allElective) {
boolean IsCourseExist = false;
boolean IsMisMatch = false;
for (int i = 0; i < allCourse.size(); i++) {
if (data[2].equals(allCourse.get(i).getName())) {
if (allCourse.get(i).isDataMisMatch(data)) {//判断输入时课程性质.方式和所给成绩是否匹配
IsMisMatch = true;
wrongStore(data, allElective);
IsCourseExist = true;
break;
}
rightStore(data, allCourse, allElective, i);//储存该选课
IsCourseExist = true;
}
}
wrongData(data, allElective, IsCourseExist, IsMisMatch);
}
private static void wrongData(String[] data, ArrayList<Elective> allElective, boolean IsCourseExist, boolean IsMisMatch) {
if (!IsCourseExist) {
System.out.println(data[2] + " does not exist");
wrongStore(data, allElective);
} else if (IsMisMatch) {
System.out.println(data[0] + " " + data[1] + " : access mode mismatch");
}
}
private static void wrongStore(String[] data, ArrayList<Elective> allElective) {
if (data.length == 5) {
allElective.add(new Elective(new Course("null", "null", "null"), new Student(Integer.parseInt(data[0]), data[1])));
} else if (data.length == 4) {
allElective.add(new Elective(new Course("null", "null", "null"), new Student(Integer.parseInt(data[0]), data[1])));
} else {
allElective.add(new Elective(new Course("null", "null", "null"), new Student(Integer.parseInt(data[0]), data[1])));
}
}
private static boolean isValidFormat(String[] data) {//信息错误
if (data.length == 2 && data[1].equals("必修")) {
return true;
} else if (data.length >= 3 && (data[1].equals("必修") || data[1].equals("选修") || data[1].equals("实验"))) {
if (data[1].equals("必修") && data[2].equals("考察") || !data[1].equals("实验") && data[2].equals("实验") || data[1].equals("实验") && !data[2].equals("实验")) {//判断输入时课程性质和方式是否匹配
System.out.println(data[0] + " : course type & access mode mismatch");
return false;
}
if (data[2].equals("考试")) {
if (data.length != 5) {
System.out.println("wrong format");
return false;
}
float value = Float.parseFloat(data[3]) + Float.parseFloat(data[4]);
if (Math.abs(value - 1.0) >= 0.0001) {//值不为1
System.out.println(data[0] + " : weight value error");
return false;
}
} else if (data[2].equals("考察")) {
} else if (data[2].equals("实验")) {
if (Float.parseFloat(data[3]) < 4 || Float.parseFloat(data[3]) > 9) {
System.out.println("wrong format");
return false;
}
if (data.length != 4 + Float.parseFloat(data[3])) {//个数不对
System.out.println(data[0] + " : number of scores does not match");
return false;
}
float value = 0;
for (int i = 4; i < data.length; i++) {
value += Float.parseFloat(data[i]);
}
if (Math.abs(value - 1.0) >= 0.0001) {//值不为1
System.out.println(data[0] + " : weight value error");
return false;
}
for (int i = 0; i < Float.parseFloat(data[3]); i++) {
if (Float.parseFloat(data[4 + i]) >= 1 || Float.parseFloat(data[4 + i]) <= 0) {
System.out.println("wrong format");
return false;
}
}
} else {
System.out.println("wrong format");
return false;
}
if (data[0].length() > 10) {
System.out.println("wrong format");
return false;
}
} else if (data.length == 4) {
if (data[3].contains(".")) {
System.out.println("wrong format");
return false;
}
if (data[0].length() != 8) {
System.out.println("wrong format");
return false;
}
if (data[1].length() > 10) {
System.out.println("wrong format");
return false;
}
if (Integer.parseInt(data[3]) > 100 || Integer.parseInt(data[3]) < 0) {
System.out.println("wrong format");
return false;
}
} else if (data.length == 5) {
if (data[3].contains(".") || data[4].contains(".")) {
System.out.println("wrong format");
return false;
}
if (data[0].length() != 8 || data[1].length() > 10) {
System.out.println("wrong format");
return false;
}
if (!data[3].matches("[0-9]+") || !data[4].matches("[0-9]+")) {///可以给下面的else if也写一个
System.out.println("wrong format");
return false;
}
if (Integer.parseInt(data[3]) > 100 || Integer.parseInt(data[3]) < 0 || Integer.parseInt(data[4]) > 100 || Integer.parseInt(data[4]) < 0) {
System.out.println("wrong format");
return false;
}
} else if (data.length > 5 && data.length <= 13) {
if (data[0].length() != 8 || data[1].length() > 10) {
System.out.println("wrong format");
return false;
}
for (int i = 4; i < data.length; i++) {
if (data[i].contains(".")) {
System.out.println("wrong format");
return false;
}
if (Integer.parseInt(data[i]) > 100 || Integer.parseInt(data[i]) < 0) {
System.out.println("wrong format");
return false;
}
}
} else {
System.out.println("wrong format");
return false;
}
return true;
}
private static void studentSort(ArrayList<Student> allStudent) {
for (int i = 0; i < allStudent.size() - 1; i++) {
for (int j = 0; j < allStudent.size() - i - 1; j++) {
if (allStudent.get(j).getId() > allStudent.get(j + 1).getId()) {
Collections.swap(allStudent, j, j + 1);
}
}
}
}
private static void classSort(ArrayList<Class> allClass) {
for (int i = 0; i < allClass.size() - 1; i++) {
for (int j = 0; j < allClass.size() - i - 1; j++) {
if (allClass.get(j).getClassId().compareTo(allClass.get(j + 1).getClassId()) > 0) {
Collections.swap(allClass, j, j + 1);
}
}
}
}
private static void courseSort(ArrayList<Course> allCourse) {
allCourse.sort((o1, o2) -> {
Collator collator = Collator.getInstance(Locale.CHINA);
return collator.compare(o1.getName(), o2.getName());
});
}
private static void rightStore(String[] data, ArrayList<Course> allCourse, ArrayList<Elective> allElective, int i) {
if (data.length == 5) {
allElective.add(new Elective(allCourse.get(i), new Student(Integer.parseInt(data[0]), data[1]), data));
} else if (data.length == 4) {
allElective.add(new Elective(allCourse.get(i), new Student(Integer.parseInt(data[0]), data[1]), data));//同上 没错的话后面再删除
} else {
allElective.add(new Elective(allCourse.get(i), new Student(Integer.parseInt(data[0]), data[1]), data));//同上 没错的话后面再删除
}
}
public static boolean isRepeatCourse(ArrayList<Course> allCourse, String[] data) {
for (int i = 0; i < allCourse.size(); i++) {
if (allCourse.get(i).getName().equals(data[0])) {
return true;
}
}
return false;
}
public static boolean isRepeatData(ArrayList<Elective> allElectiveCourse, String[] data) {
for (int i = 0; i < allElectiveCourse.size(); i++) {
if (allElectiveCourse.get(i).getStudent().getId() == Integer.parseInt(data[0]) && allElectiveCourse.get(i).getCourse().getName().equals(data[2])) {
return true;
}
}
return false;
}
}
相关复杂度分析及反思:

不足:
-
过长的方法:方法看起来相对较长,这使得代码的维护和理解变得困难。应该将代码逻辑拆分成更小的方法。
-
过多的嵌套逻辑:代码中嵌套了多个if-else语句和for循环,导致代码的可读性较差。应该考虑重构逻辑,以减少嵌套深度。
-
代码重复:代码中出现了一些重复的逻辑,例如在不同条件下都有调用 "wrongStore" 方法。应该考虑将重复的逻辑抽取出来,以提高代码的可维护性。
- 对代码的作用、目的不明确,可读性差。
类图展示:

对比11-1和12-1的区别:
在我看来在该题目中12-2的组合关系运用上更灵活,更能够适应变更。
原因:
- 由于继承是一种静态关系,在继承关系中,子类的行为和结构是在编译时确定的。如果需要在运行时动态地更改类的行为或结构,继承关系可能会受到限制。而组合关系允许在运行时动态更改组合对象的类型,可以更灵活地适应变更,在添加新课程时不需要新建子类。
- 组合关系还可以避免多层次的继承关系带来的耦合性。继承关系可能会导致类之间的紧密耦合,一旦其中一个类发生变化,可能会影响到整个继承。而组合关系通过将功能性的代码提取到其他类中,可以降低类之间的耦合性,使代码更加易于维护和扩展。
采坑心得
1.在10-1和11-1中为了调用Score的一些父类没有的方法,使用了向下转型却没判断该类是否能进行向下转型,修改如下
public int getAverageGRE() {
public int getAverageGRE() {
int add = 0;
for (int i = 0; i < scores.size(); i++) {
if (scores.get(i).returnMethod().equals("考试")) {
TextScore temp = (TextScore) (scores.get(i));
add += temp.getGRE();
} else if (scores.get(i).returnMethod().equals("考察")) {
InspectScore temp = (InspectScore) (scores.get(i));//向下转型为了调用子类特有方法
add += temp.getGRE();
}
}
return add / scores.size();
}
public int getAverageGPA() {
int add = 0;
for (int i = 0; i < scores.size(); i++) {
if (scores.get(i).returnMethod().equals("考试")) {
TextScore temp = (TextScore) (scores.get(i));
add += temp.getGPA();
}
}
return add / scores.size();
}
2.常见的NullPointerException问题:
对于一个对象的创建要确保初始化后使用,且要确保空属性不能进行 equals。
改进建议
在这几次的编码中,通过后来和其他同学的对比发现,我的代码的效率和性能很差,编码时只关注对题目的解答是否正确,没有进行一些算法和逻辑的使用,代码紊乱,多有无效代码。
总结
收获:
- 学会了如何去使用各种各样的容器解决问题
-
学会了一些Collections类的一些排序方法
-
能熟练运用TreeMap,HashMap的底层原理以及更多的运用
-
对一些的数据的分类,储存,排序,遍历等有了更清晰的思路
不足:
- 对容器的正确选择和使用仍需要在实践中更加熟练地使用它们。特别是对于多线程和网络编程,我觉得我需要更多的实践经验来理解它们在实际问题中的应用。
- 需要对Java的设计模式有更深入的理解和使用,避免写出杂乱无章的代码。

浙公网安备 33010602011771号