pta第三部分总结oop训练集09-11
一,
前言:
oop 09:7-1 统计Java程序中关键词的出现次数:对Java中字符串,元字符,正则表达式的应用。
oop 10:7-1 容器-HashMap-检索:对Java程序中HashMap的特性对输入内容进行检索的应用。
7-2 容器-HashMap-排序:对Java升序中HashMap的无序性的应用将其排序。
7-3 课程成绩统计程序-2:根据上两个题目对之前所写的课程成绩统计程序进行优化。
7-4 动物发声模拟器(多态):熟悉Java程序在继承多态上的优势。
oop 11:7-1 容器-ArrayList-排序:学习新容器ArrayList并应用其进行排序。
7-2 课程成绩统计程序-3:应用7-1对课程成绩统计程序进行优化。
7-3 jmu-Java-02基本语法-03-身份证排序:学习使用Comparator接口对目标进行排序。
7-4 jmu-Java-04面向对象进阶-03-接口-自定义接口ArrayIntegerStack:加深对该接口的学习以及应用。
7-5 jmu-Java-03面向对象基础-05-覆盖:对PersonOverride类中的一些功能进行复写。
设计与分析:
oop 09:7-1 统计Java程序中关键词的出现次数。
import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); Found found = new Found(); while (true) { String Line = in.nextLine().trim(); if (Line.isEmpty()) { System.out.println("Wrong Format"); break; } else if (Line.equals("exit")) { found.foundKeyWord(); break; } else { found.addLine(Line); } } } } class Found { Set<String> keywords = new TreeSet<>(Arrays.asList( "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", "continue", "default", "do", "double", "else", "enum", "extends", "final", "finally", "float", "for", "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" )); Map<String, Integer> counter = new TreeMap<>(); Pattern stringPattern = Pattern.compile("\"(?:\\\\.|[^\"])*\""); Pattern commentPattern = Pattern.compile("//.*|/\\*.*?\\*/", Pattern.DOTALL); Pattern wordPattern = Pattern.compile("\\b(" + String.join("|", keywords) + ")\\b"); public Found() { } public void foundKeyWord() { for (Map.Entry<String, Integer> entry : counter.entrySet()) { System.out.println(entry.getValue() + "\t" + entry.getKey()); } } public void addLine(String Line) { Matcher matcher = stringPattern.matcher(Line); StringBuilder stringBuilder = new StringBuilder(Line); while (matcher.find()) { stringBuilder.replace(matcher.start(), matcher.end(), ""); } Line = stringBuilder.toString(); matcher = commentPattern.matcher(Line); stringBuilder.setLength(0); int i = 0; while (matcher.find()) { stringBuilder.append(Line, i, matcher.start()); i = matcher.end(); } stringBuilder.append(Line, i, Line.length()); Line = stringBuilder.toString(); matcher = wordPattern.matcher(Line); while (matcher.find()) { String word = matcher.group(); counter.put(word, counter.getOrDefault(word, 0) + 1); } } }
oop 10:7-1 容器-HashMap-检索。
import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input=new Scanner(System.in); HashMap<String,String > student=new HashMap<>(); while(true){ String Line=input.nextLine(); if(Line.equals("end")){ break; } String[] theLine=Line.split(" "); student.put(theLine[0],theLine[1]+" "+theLine[2]); } String studentID=input.nextLine(); String result=student.get(studentID); if(result==null){ System.out.println("The student "+studentID+" does not exist"); }else{ System.out.println(studentID+" "+result); } } }
7-2 容器-HashMap-排序。
import java.util.*; import java.util.List; public class Main { public static void main(String[] args) { Scanner input=new Scanner(System.in); HashMap<String,String > student=new HashMap<>(); while(true){ String Line=input.nextLine(); if(Line.equals("end")){ break; } String[] theLine=Line.split(" "); student.put(theLine[0],theLine[1]+" "+theLine[2]); } List<Map.Entry<String,String>> list=new ArrayList<Map.Entry<String, String>>(student.entrySet()); Collections.sort(list, new Comparator<Map.Entry<String, String>>() { public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) { return o2.getKey().compareTo(o1.getKey()); } }); for (Map.Entry<String, String> student1:list) { System.out.println(student1.getKey()+" "+student1.getValue()); } } }
7-3 课程成绩统计程序-2。
import java.util.Scanner; // 按两次 Shift 打开“随处搜索”对话框并输入 `show whitespaces`, // 然后按 Enter 键。现在,您可以在代码中看到空格字符。 public class Main { public static void main(String[] args) { String input; Scanner in = new Scanner(System.in); Course[] courses = null; String student_num; Class[] theClass = null; // 添加课程 课程名称、课程性质、考核方式 while (true) { input = in.nextLine(); if (input.equals("end")) { break; } if (checkCourse(input)) { String[] course = input.split(" "); if (courses == null) { courses = new Course[1]; //如果数组为空,先初始化一个长度为1的数组 courses[0] = new Course(course[0], course[1], course[2]); //在数组的第一个位置上添加新的Course对象 } else { Course[] temp = new Course[courses.length + 1]; //先创建一个长度比原数组大1的临时数组 System.arraycopy(courses, 0, temp, 0, courses.length); //将原数组中的元素复制到临时数组中 temp[temp.length - 1] = new Course(course[0], course[1], course[2]); //在临时数组的最后一个位置上添加新的Course对象 courses = temp; //将临时数组赋值给原数组 } } else if (checkScore1(input)) { String[] scores = input.split(" "); int class_num = Integer.parseInt(scores[0].substring(0, 6)); Student student = new Student(scores[1], scores[0]); if (theClass == null) { theClass=new Class[class_num+1]; theClass[class_num] = new Class(class_num, student); } else if (!checkClass(theClass, class_num)) { theClass[class_num] = new Class(class_num, student); } else { theClass[class_num] = new Class(student); } for (int i = 0; i < courses.length; i++) { if (courses[i].getCourse().equals(scores[2])) { if (courses[i].getExamKind().equals("考察")) { InvestigateScore score = new InvestigateScore(Integer.parseInt(scores[3])); CourseSelection courseSelection = new CourseSelection(courses[i], student, score); student.setCourseSelection(courseSelection); } else { ExamScore score = new ExamScore(Integer.parseInt(scores[3]), Integer.parseInt(scores[4])); CourseSelection courseSelection = new CourseSelection(courses[i], student, score); student.setCourseSelection(courseSelection); } } } } else if (checkScore2(input)) { String[] scores = input.split(" "); int class_num = Integer.parseInt(scores[0].substring(0, 6)); Student student = new Student(scores[1], scores[0]); if (theClass == null) { theClass=new Class[class_num+1]; theClass[class_num] = new Class(class_num, student); } else if (!checkClass(theClass, class_num)) { theClass[class_num] = new Class(class_num, student); } else { theClass[class_num] = new Class(student); } for (int i = 0; i < courses.length; i++) { if (courses[i].getCourse().equals(scores[2])) { ExamScore score = new ExamScore(Integer.parseInt(scores[3]), Integer.parseInt(scores[4])); CourseSelection courseSelection = new CourseSelection(courses[i], student, score); student.setCourseSelection(courseSelection); } } } else{ System.out.println("wrong format"); } } if(!(theClass ==null)){ outputFirst(theClass); outputSecond(courses,theClass); outputThird(theClass); }else outputSecond(courses,theClass); // 输入学生成绩 学号、姓名、课程名称、平时成绩(可选)、期末成绩 // 输出包含三个部分,包括学生所有课程总成绩的平均分、单门课程成绩平均分、单门课程总成绩平均分、班级所有课程总成绩平均分 // 学生所有课程总成绩的平均分(总成绩/课程数) // 单门课程成绩平均分 // 单门课程总成绩平均分 // 班级所有课程总成绩平均分 } public static boolean checkScore1(String input) { return input.matches("\\S+\\s\\S+\\s\\S+\\s\\S+\\s\\S+"); } public static boolean checkScore2(String input){ return input.matches("\\S+\\s\\S+\\s\\S+\\s\\S+"); } public static boolean checkCourse(String input) { return input.matches("\\S+\\s\\S+\\s\\S+"); } // 判断班级是否存在 public static boolean checkClass(Class[] theClass, int class_num) { boolean result = false; for (int i = 0; i < theClass.length; i++) { if (theClass[i].class_num == class_num) { result = true; break; } } return result; } // 1)学生课程总成绩平均分按学号由低到高排序输出 // 格式:学号+英文空格+姓名+英文空格+总成绩平均分 // 如果某个学生没有任何成绩信息,输出:学号+英文空格+姓名+英文空格+"did not take any exams" public static void outputFirst(Class[] theClass){ for(int i=0;i<theClass.length;i++){ for(int j=0;j<theClass[i].students.length;j++){ if(theClass[i].students[j].courseSelection.scores==null){ System.out.println(theClass[i].students[j].student_num+" "+theClass[i].students[j].studentName+"did not take any exams"); }else{ double result=theClass[i].students[j].courseSelection.studentAllCourseScore()/theClass[i].students[j].courseSelection.courses.length; System.out.println(theClass[i].students[j].student_num+" "+theClass[i].students[j].studentName+" "+result); } } } } // 2)单门课程成绩平均分分为三个分值:平时成绩平均分(可选)、期末考试平均分、总成绩平均分,按课程名称的字符顺序输出 // 格式:课程名称+英文空格+平时成绩平均分+英文空格+期末考试平均分+英文空格+总成绩平均分 // 如果某门课程没有任何成绩信息,输出:课程名称+英文空格+"has no grades yet" public static void outputSecond(Course[] courses,Class[] theClass){ double usualresult=0; double examresult=0; double finalresult=0; if(!(theClass ==null)){ for(int k=0;k<courses.length;k++){ for(int i=0;i< theClass.length;i++){ for(int j=0;j<theClass[i].students.length;j++){ for(int w=0;w<theClass[i].students[j].courseSelection.courses.length;w++){ if(courses[k]!=null&&!theClass[i].students[j].courseSelection.courses[w].getCourse().equals(courses[k].getCourse())){ System.out.println(courses[k].getCourse()+" "+"has no grades yet"); }else{ usualresult+=theClass[i].students[j].courseSelection.scores[w].usualscore; examresult+=theClass[i].students[j].courseSelection.scores[w].getExamScore(); finalresult=theClass[i].students[j].courseSelection.scores[w].finalScore(); } } } System.out.println(courses[k].getCourse()+" "+usualresult/theClass[i].students.length+" "+examresult/theClass[i].students.length+" "+finalresult/theClass[i].students.length); } } }else{ for(int i=0;i<courses.length;i++){ System.out.println(courses[i].getCourse()+" "+"has no grades yet"); } } } // 3)班级所有课程总成绩平均分按班级由低到高排序输出 格式:班级号+英文空格+总成绩平均分 // 如果某个班级没有任何成绩信息,输出:班级名称+英文空格+ "has no grades yet" public static void outputThird(Class[] theClass){ for(int i=0;i<theClass.length;i++){ boolean flag=true; double result=0; for(int j=0;j<theClass[i].students.length;j++){ if(theClass[i].students[j].courseSelection.scores==null){ flag=false; }else{ flag=true; } result+=theClass[i].students[j].courseSelection.studentAllCourseScore(); } if(flag){ System.out.println(theClass[i].class_num+" "+result / theClass[i].students.length); }else{ System.out.println(theClass[i].class_num+" "+"has no grades yet"); } } } } //选课 class CourseSelection{ public Course[] courses; public Student student; public Score[] scores; public CourseSelection(){ } public CourseSelection( Course course,Student student,Score score){ if (courses == null && scores== null) { courses = new Course[1]; courses[0] = course; scores=new Score[1]; scores[0]=score; } else { int currentLength = courses == null ? 0 : courses.length; Course[] newCourses = new Course[currentLength + 1]; if (courses != null) { System.arraycopy(courses, 0, newCourses, 0, currentLength); } newCourses[currentLength] = course; courses = newCourses; this.scores[courses.length-1]=score; } this.student=student; } public double studentAllCourseScore(){ double result = 0; for(int i=0;i<courses.length;i++){ result+=scores[i].finalScore(); } return result; } } class Student{ public String studentName; public String student_num; public CourseSelection courseSelection; public Student(){ } public Student(String studentName,String student_num){ this.studentName=studentName; this.student_num=student_num; } public void setCourseSelection(CourseSelection courseSelection) { this.courseSelection = courseSelection; } } class Class{ public int class_num; public Student[] students; public Class(){ } public Class(int class_num,Student student){ this.class_num=class_num; if (students == null) { students = new Student[1]; students[0] = student; } else { int currentLength = students.length; Student[] newStudents = new Student[currentLength + 1]; System.arraycopy(students, 0, newStudents, 0, currentLength); newStudents[currentLength] = student; students = newStudents; } } public Class(Student student){ if (students == null) { students = new Student[1]; students[0] = student; } else { int currentLength = students.length; Student[] newStudents = new Student[currentLength + 1]; System.arraycopy(students, 0, newStudents, 0, currentLength); newStudents[currentLength] = student; students = newStudents; } } } class Course{ // 课程名称 private String course; // 课程性质(选修|必修) private String character; // 考核方式 private String examKind; public Course(){ } public Course(String course,String character,String examKind){ this.course=course; this.character=character; this.examKind=examKind; } public String getCourse() { return course; } public void setCourse(String course) { this.course = course; } public String getExamKind() { return examKind; } public void setExamKind(String examKind) { this.examKind = examKind; } } abstract class Score{ private String scorekind; public int usualscore=0; public Score(){ } public abstract double finalScore(); public abstract int getExamScore(); } class InvestigateScore extends Score{ private int score; public InvestigateScore(){ } public InvestigateScore(int score){ this.score=score; } public void setScore(int score) { this.score = score; } public int getScore() { return score; } public double finalScore(){ return this.score; } @Override public int getExamScore() { return this.score; } } class ExamScore extends Score{ private int score; public ExamScore(){ } public ExamScore(int usualscore,int score){ this.score=score; this.usualscore=usualscore; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public double finalScore(){ return this.usualscore*0.3+this.score*0.7; } @Override public int getExamScore() { return this.score; } }
7-4 动物发声模拟器(多态)。
//动物发生模拟器. 请在下面的【】处添加代码。 public class Main{ public static void main(String[] args) { Cat cat = new Cat(); Dog dog = new Dog(); Goat goat = new Goat(); speak(cat); speak(dog); speak(goat); } //定义静态方法speak() public static void speak(Animal animal){ System.out.println(animal.getAniamlClass()+":"+animal.shout()); } } //定义抽象类Animal abstract class Animal{ public abstract String getAniamlClass(); public abstract String shout(); } //基于Animal类,定义猫类Cat,并重写两个抽象方法 class Cat extends Animal{ @Override public String getAniamlClass() { String theClass = "猫的叫声"; return theClass; } @Override public String shout() { String shout = "喵喵"; return shout; } } //基于Animal类,定义狗类Dog,并重写两个抽象方法 class Dog extends Animal{ @Override public String getAniamlClass() { String theClass = "狗的叫声"; return theClass; } @Override public String shout() { String shout="汪汪"; return shout; } } //基于Animal类,定义山羊类Goat,并重写两个抽象方法 class Goat extends Animal{ @Override public String getAniamlClass() { String theClass = "山羊的叫声"; return theClass; } @Override public String shout() { String shout="咩咩"; return shout; } }
oop 11:7-1 容器-ArrayList-排序。
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.Scanner; public class Main { public static void main(String[] args) { ArrayList<Student> students = new ArrayList<Student>(); Scanner scanner = new Scanner(System.in); String line = scanner.nextLine(); while (!line.equals("end")) { String[] parts = line.split(" "); String id = parts[0]; String name = parts[1]; int mathScore = Integer.parseInt(parts[2]); int physicsScore = Integer.parseInt(parts[3]); Student student = new Student(id, name, mathScore, physicsScore); students.add(student); line = scanner.nextLine(); } Collections.sort(students, new Comparator<Student>() { @Override public int compare(Student s1, Student s2) { int sum1 = s1.getMathScore() + s1.getPhysicsScore(); int sum2 = s2.getMathScore() + s2.getPhysicsScore(); if (sum1 != sum2) { return sum2 - sum1; } else { return students.indexOf(s1) - students.indexOf(s2); } } }); for (Student student : students) { System.out.println(student.getId() + " " + student.getName() + " " + (student.getMathScore() + student.getPhysicsScore())); } } } class Student { private String id; private String name; private int mathScore; private int physicsScore; public Student(String id, String name, int mathScore, int physicsScore) { this.id = id; this.name = name; this.mathScore = mathScore; this.physicsScore = physicsScore; } public String getId() { return id; } public String getName() { return name; } public int getMathScore() { return mathScore; } public int getPhysicsScore() { return physicsScore; } }
7-2 课程成绩统计程序-3。
import java.util.Scanner; // 按两次 Shift 打开“随处搜索”对话框并输入 `show whitespaces`, // 然后按 Enter 键。现在,您可以在代码中看到空格字符。 public class Main { public static void main(String[] args) { String input; Scanner in = new Scanner(System.in); Course[] courses = null; String student_num; Class[] theClass = null; // 添加课程 课程名称、课程性质、考核方式 while (true) { input = in.nextLine(); if (input.equals("end")) { break; } if (checkCourse(input)) { String[] course = input.split(" "); if (courses == null) { courses = new Course[1]; //如果数组为空,先初始化一个长度为1的数组 courses[0] = new Course(course[0], course[1], course[2]); //在数组的第一个位置上添加新的Course对象 } else { Course[] temp = new Course[courses.length + 1]; //先创建一个长度比原数组大1的临时数组 System.arraycopy(courses, 0, temp, 0, courses.length); //将原数组中的元素复制到临时数组中 temp[temp.length - 1] = new Course(course[0], course[1], course[2]); //在临时数组的最后一个位置上添加新的Course对象 courses = temp; //将临时数组赋值给原数组 } } else if (checkScore1(input)) { String[] scores = input.split(" "); int class_num = Integer.parseInt(scores[0].substring(0, 6)); Student student = new Student(scores[1], scores[0]); if (theClass == null) { theClass=new Class[class_num+1]; theClass[class_num] = new Class(class_num, student); } else if (!checkClass(theClass, class_num)) { theClass[class_num] = new Class(class_num, student); } else { theClass[class_num] = new Class(student); } for (int i = 0; i < courses.length; i++) { if (courses[i].getCourse().equals(scores[2])) { if (courses[i].getExamKind().equals("考察")) { InvestigateScore score = new InvestigateScore(Integer.parseInt(scores[3])); CourseSelection courseSelection = new CourseSelection(courses[i], student, score); student.setCourseSelection(courseSelection); } else { ExamScore score = new ExamScore(Integer.parseInt(scores[3]), Integer.parseInt(scores[4])); CourseSelection courseSelection = new CourseSelection(courses[i], student, score); student.setCourseSelection(courseSelection); } } } } else if (checkScore2(input)) { String[] scores = input.split(" "); int class_num = Integer.parseInt(scores[0].substring(0, 6)); Student student = new Student(scores[1], scores[0]); if (theClass == null) { theClass=new Class[class_num+1]; theClass[class_num] = new Class(class_num, student); } else if (!checkClass(theClass, class_num)) { theClass[class_num] = new Class(class_num, student); } else { theClass[class_num] = new Class(student); } for (int i = 0; i < courses.length; i++) { if (courses[i].getCourse().equals(scores[2])) { ExamScore score = new ExamScore(Integer.parseInt(scores[3]), Integer.parseInt(scores[4])); CourseSelection courseSelection = new CourseSelection(courses[i], student, score); student.setCourseSelection(courseSelection); } } } else{ System.out.println("wrong format"); } } if(!(theClass ==null)){ outputFirst(theClass); outputSecond(courses,theClass); outputThird(theClass); }else outputSecond(courses,theClass); // 输入学生成绩 学号、姓名、课程名称、平时成绩(可选)、期末成绩 // 输出包含三个部分,包括学生所有课程总成绩的平均分、单门课程成绩平均分、单门课程总成绩平均分、班级所有课程总成绩平均分 // 学生所有课程总成绩的平均分(总成绩/课程数) // 单门课程成绩平均分 // 单门课程总成绩平均分 // 班级所有课程总成绩平均分 } public static boolean checkScore1(String input) { return input.matches("\\S+\\s\\S+\\s\\S+\\s\\S+\\s\\S+"); } public static boolean checkScore2(String input){ return input.matches("\\S+\\s\\S+\\s\\S+\\s\\S+"); } public static boolean checkCourse(String input) { return input.matches("\\S+\\s\\S+\\s\\S+"); } // 判断班级是否存在 public static boolean checkClass(Class[] theClass, int class_num) { boolean result = false; for (int i = 0; i < theClass.length; i++) { if (theClass[i].class_num == class_num) { result = true; break; } } return result; } // 1)学生课程总成绩平均分按学号由低到高排序输出 // 格式:学号+英文空格+姓名+英文空格+总成绩平均分 // 如果某个学生没有任何成绩信息,输出:学号+英文空格+姓名+英文空格+"did not take any exams" public static void outputFirst(Class[] theClass){ for(int i=0;i<theClass.length;i++){ for(int j=0;j<theClass[i].students.length;j++){ if(theClass[i].students[j].courseSelection.scores==null){ System.out.println(theClass[i].students[j].student_num+" "+theClass[i].students[j].studentName+"did not take any exams"); }else{ double result=theClass[i].students[j].courseSelection.studentAllCourseScore()/theClass[i].students[j].courseSelection.courses.length; System.out.println(theClass[i].students[j].student_num+" "+theClass[i].students[j].studentName+" "+result); } } } } // 2)单门课程成绩平均分分为三个分值:平时成绩平均分(可选)、期末考试平均分、总成绩平均分,按课程名称的字符顺序输出 // 格式:课程名称+英文空格+平时成绩平均分+英文空格+期末考试平均分+英文空格+总成绩平均分 // 如果某门课程没有任何成绩信息,输出:课程名称+英文空格+"has no grades yet" public static void outputSecond(Course[] courses,Class[] theClass){ double usualresult=0; double examresult=0; double finalresult=0; if(!(theClass ==null)){ for(int k=0;k<courses.length;k++){ for(int i=0;i< theClass.length;i++){ for(int j=0;j<theClass[i].students.length;j++){ for(int w=0;w<theClass[i].students[j].courseSelection.courses.length;w++){ if(courses[k]!=null&&!theClass[i].students[j].courseSelection.courses[w].getCourse().equals(courses[k].getCourse())){ System.out.println(courses[k].getCourse()+" "+"has no grades yet"); }else{ usualresult+=theClass[i].students[j].courseSelection.scores[w].usualscore; examresult+=theClass[i].students[j].courseSelection.scores[w].getExamScore(); finalresult=theClass[i].students[j].courseSelection.scores[w].finalScore(); } } } System.out.println(courses[k].getCourse()+" "+usualresult/theClass[i].students.length+" "+examresult/theClass[i].students.length+" "+finalresult/theClass[i].students.length); } } }else{ for(int i=0;i<courses.length;i++){ System.out.println(courses[i].getCourse()+" "+"has no grades yet"); } } } // 3)班级所有课程总成绩平均分按班级由低到高排序输出 格式:班级号+英文空格+总成绩平均分 // 如果某个班级没有任何成绩信息,输出:班级名称+英文空格+ "has no grades yet" public static void outputThird(Class[] theClass){ for(int i=0;i<theClass.length;i++){ boolean flag=true; double result=0; for(int j=0;j<theClass[i].students.length;j++){ if(theClass[i].students[j].courseSelection.scores==null){ flag=false; }else{ flag=true; } result+=theClass[i].students[j].courseSelection.studentAllCourseScore(); } if(flag){ System.out.println(theClass[i].class_num+" "+result / theClass[i].students.length); }else{ System.out.println(theClass[i].class_num+" "+"has no grades yet"); } } } } //选课 class CourseSelection{ public Course[] courses; public Student student; public Score[] scores; public CourseSelection(){ } public CourseSelection( Course course,Student student,Score score){ if (courses == null && scores== null) { courses = new Course[1]; courses[0] = course; scores=new Score[1]; scores[0]=score; } else { int currentLength = courses == null ? 0 : courses.length; Course[] newCourses = new Course[currentLength + 1]; if (courses != null) { System.arraycopy(courses, 0, newCourses, 0, currentLength); } newCourses[currentLength] = course; courses = newCourses; this.scores[courses.length-1]=score; } this.student=student; } public double studentAllCourseScore(){ double result = 0; for(int i=0;i<courses.length;i++){ result+=scores[i].finalScore(); } return result; } } class Student{ public String studentName; public String student_num; public CourseSelection courseSelection; public Student(){ } public Student(String studentName,String student_num){ this.studentName=studentName; this.student_num=student_num; } public void setCourseSelection(CourseSelection courseSelection) { this.courseSelection = courseSelection; } } class Class{ public int class_num; public Student[] students; public Class(){ } public Class(int class_num,Student student){ this.class_num=class_num; if (students == null) { students = new Student[1]; students[0] = student; } else { int currentLength = students.length; Student[] newStudents = new Student[currentLength + 1]; System.arraycopy(students, 0, newStudents, 0, currentLength); newStudents[currentLength] = student; students = newStudents; } } public Class(Student student){ if (students == null) { students = new Student[1]; students[0] = student; } else { int currentLength = students.length; Student[] newStudents = new Student[currentLength + 1]; System.arraycopy(students, 0, newStudents, 0, currentLength); newStudents[currentLength] = student; students = newStudents; } } } class Course{ // 课程名称 private String course; // 课程性质(选修|必修) private String character; // 考核方式 private String examKind; public Course(){ } public Course(String course,String character,String examKind){ this.course=course; this.character=character; this.examKind=examKind; } public String getCourse() { return course; } public void setCourse(String course) { this.course = course; } public String getExamKind() { return examKind; } public void setExamKind(String examKind) { this.examKind = examKind; } } abstract class Score{ private String scorekind; public int usualscore=0; public Score(){ } public abstract double finalScore(); public abstract int getExamScore(); } class InvestigateScore extends Score{ private int score; public InvestigateScore(){ } public InvestigateScore(int score){ this.score=score; } public void setScore(int score) { this.score = score; } public int getScore() { return score; } public double finalScore(){ return this.score; } @Override public int getExamScore() { return this.score; } } class ExamScore extends Score{ private int score; public ExamScore(){ } public ExamScore(int usualscore,int score){ this.score=score; this.usualscore=usualscore; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } public double finalScore(){ return this.usualscore*0.3+this.score*0.7; } @Override public int getExamScore() { return this.score; } }
7-3 jmu-Java-02基本语法-03-身份证排序。
import java.util.*; public class Main{ public static void main(String[] args) { Scanner in = new Scanner(System.in); int num = in.nextInt(); in.nextLine(); String[] allLine = new String[num]; for(int i = 0; i < num; i++) { allLine[i] = in.nextLine(); } Check check = new Check(allLine); String line = ""; while(true){ line = in.nextLine(); if(line.equals("sort1")){ check.checkFunction1(); }else if(line.equals("sort2")){ check.checkFunction2(); }else{ System.out.println("exit"); break; } } } } class Check{ private ArrayList<String> allId = new ArrayList<>(); public Check(String[] line){ for (String theLine:line) { allId.add(theLine); } } public void checkFunction1(){ ArrayList<String> resultList = new ArrayList<>(); for(String line : this.allId){ String date = line.substring(6, 14); String formattedDate = date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6); resultList.add(formattedDate); } Collections.sort(resultList); for(String date : resultList){ System.out.println(date); } } public void checkFunction2(){ Comparator<String> idCardComparator = new Comparator<String>() { @Override public int compare(String o1, String o2) { // 按照年月日升序排序 String date1 = o1.substring(6, 14); String date2 = o2.substring(6, 14); return date1.compareTo(date2); } }; Collections.sort(allId, idCardComparator); for(String line : this.allId){ System.out.println(line); } } }
7-4 jmu-Java-04面向对象进阶-03-接口-自定义接口ArrayIntegerStack。
import java.util.Scanner; import java.util.Arrays; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); ArrayIntegerStack stack = new ArrayIntegerStack(n); ArrayIntegerStack stack1 = new ArrayIntegerStack(n); int num=scanner.nextInt(); for (int i = 0; i < num; i++) { int value = scanner.nextInt(); int result = stack.push(value); stack1.push(value); if(result==-1){ System.out.println("error"); }else{ System.out.println(result); } } System.out.println(stack.peek()+","+stack.isEmpty()+","+ stack.size()); System.out.println(Arrays.toString(stack.toArray())); int x = scanner.nextInt(); for (int i = 0; i < x; i++) { int value = stack.pop(); System.out.println(value); } System.out.println(stack.peek()+","+stack.isEmpty()+","+ stack.size()); System.out.println(Arrays.toString(stack1.toArray())); } } class ArrayIntegerStack { private int[] array; private int top; public ArrayIntegerStack(int capacity) { array = new int[capacity]; top = -1; } public int push(int value) { if (top == array.length - 1) { return -1; } else { array[++top] = value; return value; } } public int pop() { if (top == -1) { return -1; } else { return array[top--]; } } public int peek() { if (top == -1) { return -1; } else { return array[top]; } } public boolean isEmpty() { return top == -1; } public int size() { return top + 1; } public Integer[] toArray() { Integer[] result = new Integer[array.length]; Arrays.fill(result, null); for (int i = 0; i <= top; i++) { result[i] = array[i]; } return result; } }
7-5 jmu-Java-03面向对象基础-05-覆盖。
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; class PersonOverride{ private String name; private int age; private boolean gender; public PersonOverride() { this("default", 1, true); } public PersonOverride(String name, int age, boolean gender) { this.name = name; this.age = age; this.gender = gender; } @Override public String toString() { return name + "-" + age + "-" + gender; } @Override public boolean equals(Object obj) { if (obj == null || !(obj instanceof PersonOverride)) { return false; } PersonOverride p = (PersonOverride) obj; return this.name.equals(p.name) && this.age == p.age && this.gender == p.gender; } } public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n1 = scanner.nextInt(); ArrayList<PersonOverride> persons1 = new ArrayList<>(); for (int i = 0; i < n1; i++) { persons1.add(new PersonOverride()); } int n2 = scanner.nextInt(); ArrayList<PersonOverride> persons2 = new ArrayList<>(); for (int i = 0; i < n2; i++) { String name = scanner.next(); int age = scanner.nextInt(); boolean gender = scanner.nextBoolean(); PersonOverride p = new PersonOverride(name, age, gender); if (!persons2.contains(p)) { persons2.add(p); } } for (PersonOverride p : persons1) { System.out.println(p); } for (PersonOverride p : persons2) { System.out.println(p); } System.out.println(persons2.size()); System.out.println(Arrays.toString(PersonOverride.class.getConstructors())); } }