首页 > 其他分享 >blog3

blog3

时间:2023-06-28 21:44:58浏览次数:40  
标签:studentId return String int blog3 score public

课程成绩统计程序-2在第一次的基础上增加了实验课,以下加粗字体显示为本次新增的内容。

某高校课程从性质上分为:必修课、选修课、实验课,从考核方式上分为:考试、考察、实验。

考试的总成绩由平时成绩、期末成绩分别乘以权重值得出,比如平时成绩权重0.3,期末成绩权重0.7,总成绩=平时成绩*0.3+期末成绩*0.7。

考察的总成绩直接等于期末成绩

实验的总成绩等于课程每次实验成绩的平均分

必修课的考核方式必须为考试,选修课可以选择考试、考察任一考核方式。实验课的成绩必须为实验。

1、输入:

包括课程、课程成绩两类信息。

课程信息包括:课程名称、课程性质、考核方式(可选,如果性质是必修课,考核方式可以没有)三个数据项。

课程信息格式:课程名称+英文空格+课程性质+英文空格+考核方式

课程性质输入项:必修、选修、实验

考核方式输入选项:考试、考察、实验

考试/考查课程成绩信息包括:学号、姓名、课程名称、平时成绩(可选)、期末成绩

考试/考查课程信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+平时成绩+英文空格+期末成绩

实验课程成绩信息包括:学号、姓名、课程名称、实验次数、每次成绩

实验次数至少4次,不超过9次

实验课程信息格式:学号+英文空格+姓名+英文空格+课程名称+英文空格+实验次数+英文空格+第一次实验成绩+...+英文空格+最后一次实验成绩

以上信息的相关约束:

1)平时成绩和期末成绩的权重默认为0.3、0.7

2)成绩是整数,不包含小数部分,成绩的取值范围是【0,100】

3)学号由8位数字组成

4)姓名不超过10个字符

5)课程名称不超过10个字符

6)不特别输入班级信息,班级号是学号的前6位。

2、输出:

输出包含三个部分,包括学生所有课程总成绩的平均分、单门课程成绩平均分、单门课程总成绩平均分、班级所有课程总成绩平均分。

为避免误差,平均分的计算方法为累加所有符合条件的单个成绩,最后除以总数。

1)学生课程总成绩平均分按学号由低到高排序输出

格式:学号+英文空格+姓名+英文空格+总成绩平均分

如果某个学生没有任何成绩信息,输出:学号+英文空格+姓名+英文空格+"did not take any exams"

2)单门课程成绩平均分分为三个分值:平时成绩平均分(可选)、期末考试平均分、总成绩平均分,按课程名称的字符顺序输出

考试/考察课程成绩格式:课程名称+英文空格+平时成绩平均分+英文空格+期末考试平均分+英文空格+总成绩平均分

实验课成绩格式:课程名称+英文空格+总成绩平均分

如果某门课程没有任何成绩信息,输出:课程名称+英文空格+"has no grades yet"

3)班级所有课程总成绩平均分按班级由低到高排序输出

格式:班级号+英文空格+总成绩平均分

如果某个班级没有任何成绩信息,输出:班级名称+英文空格+ "has no grades yet"

异常情况:

1)如果解析某个成绩信息时,课程名称不在已输入的课程列表中,输出:学号+英文空格+姓名+英文空格+":"+课程名称+英文空格+"does not exist"

2)如果解析某个成绩信息时,输入的成绩数量和课程的考核方式不匹配,输出:学号+英文空格+姓名+英文空格+": access mode mismatch"

以上两种情况如果同时出现,按第一种情况输出结果。

3)如果解析某个课程信息时,输入的课程性质和课程的考核方式不匹配,输出:课程名称+" : course type & access mode mismatch"

4)格式错误以及其他信息异常如成绩超出范围等,均按格式错误处理,输出"wrong format"

5)若出现重复的课程/成绩信息,只保留第一个课程信息,忽略后面输入的。

信息约束:

1)成绩平均分只取整数部分,小数部分丢弃

参考类图(与第一次相同,其余内容自行补充):


e724fa4193aa9ee32e78a68cd96fd6df_22401e04-c501-4b28-bb65-dabe39d374e7.png

import java.util.*;

// 学生类
class Student {


private String studentId;
private String name;

public Student(String studentId, String name) {
this.studentId = studentId;
this.name = name;
}

public String getStudentId() {
return studentId;
}

public String getName() {
return name;
}
}

// 课程类
class Course {
private String courseName;
private String courseType;
private String assessmentMode;

public Course(String courseName, String courseType, String assessmentMode) {
this.courseName = courseName;
this.courseType = courseType;
this.assessmentMode = assessmentMode;
}

public String getCourseName() {
return courseName;
}

public String getCourseType() {
return courseType;
}

public String getAssessmentMode() {
return assessmentMode;
}
}

// 成绩类
abstract class Score {

private int score;

private String course;


public String getCourse() {
return course;
}

public Score setCourse(String course) {
this.course = course;
return this;
}

public Score(int score, String course) {
this.score = score;
this.course = course;
}

public int getScore() {
return score;
}

public Score setScore(int score) {
this.score = score;
return this;
}

public abstract int calculateTotalScore();
}

// 考试成绩类
class ExamScore extends Score {

private int ordinaryScore;
private int finalScore;

public ExamScore(String course, int ordinaryScore, int finalScore) {
super((int) (ordinaryScore * 0.3 + finalScore * 0.7), course);
this.ordinaryScore = ordinaryScore;
this.finalScore = finalScore;
}

public int getOrdinaryScore() {
return ordinaryScore;
}

public ExamScore setOrdinaryScore(int ordinaryScore) {
this.ordinaryScore = ordinaryScore;
return this;
}

public int getFinalScore() {
return finalScore;
}

public ExamScore setFinalScore(int finalScore) {
this.finalScore = finalScore;
return this;
}

@Override
public int calculateTotalScore() {
return getScore();
}
}

// 考察成绩类
class ReviewScore extends Score {

public int ordinaryScore;

public int getOrdinaryScore() {
return ordinaryScore;
}

public ReviewScore setOrdinaryScore(int ordinaryScore) {
this.ordinaryScore = ordinaryScore;
return this;
}

public ReviewScore(String course, int ordinaryScore, int score) {
super(score, course);
this.ordinaryScore = ordinaryScore;
}

@Override
public int calculateTotalScore() {
return getScore();
}
}

// 实验成绩类
class LabScore extends Score {

private List<Integer> Score;

public LabScore(String course, List<Integer> score) {
super(score.stream().reduce(Integer::sum).orElse(0) / score.size(), course);
}

@Override
public int calculateTotalScore() {
return getScore();
}
}

// 主程序类
public class Main {
private Map<String, Student> students;
private Map<String, Course> courses;
private Map<String, List<Score>> scores;

public Main() {
students = new HashMap<>();
courses = new HashMap<>();
scores = new HashMap<>();
}

public void processInput(String input) {
String[] tokens = input.split(" ");
String type = tokens[0];

if (type.matches("[0-9]+")) {
processScore(tokens);
} else if (type.equals("end")) {
calculateGrades();
} else {
processCourse(tokens);
}

// switch (type) {
// case "java":
// case "C语言":
// case "编译原理":
// processCourse(tokens);
// break;
// case "end":
// calculateGrades();
// break;
// default:
// processScore(tokens);
// break;
// }
}

private void processCourse(String[] tokens) {
String courseName = tokens[0];
String courseType = tokens[1];
String assessmentMode = tokens[2];

if (courseType.equals("必修") && !assessmentMode.equals("考试")) {
System.out.println(courseName + " : course type & access mode mismatch");
return;
}
if (courseType.equals("考察") && assessmentMode.equals("实验")) {
System.out.println(courseName + " : course type & access mode mismatch");
return;
}
if (courseType.equals("实验") && !assessmentMode.equals("实验")) {
System.out.println(courseName + " : course type & access mode mismatch");
return;
}

if(courses.get(courseName)==null) {
Course course = new Course(courseName, courseType, assessmentMode);
courses.put(courseName, course);
}
}

private void processScore(String[] tokens) {
String studentId = tokens[0];
String name = tokens[1];
String courseName = tokens[2];


boolean b = true;
for (int i = 3; i < tokens.length; i++) {
String token = tokens[i];
int i1 = Integer.parseInt(token);
if (i1 > 100 || i1 < 0) {
System.out.println("wrong format");
b = false;
return;
}

}

if (tokens.length > 5) {
int testNumber = Integer.parseInt(tokens[3]);
if (testNumber < 4 || testNumber > 9) {
System.out.println("wrong format");
b = false;
return;
}
Course course = courses.get(courseName);
if (course == null) {
System.out.println(studentId + " " + name + " : " + courseName + " does not exist");
return;
}
if (tokens.length - 4 != testNumber) {
System.out.println(studentId + " " + name + " : access mode mismatch");
Student student = students.get(studentId);

if (student == null) {
student = new Student(studentId, name);
students.put(studentId, student);
}
return;
}
}

if (b) {
Student student = students.get(studentId);

if (student == null) {
student = new Student(studentId, name);
students.put(studentId, student);
}
}


Course course = courses.get(courseName);
if (course == null) {
System.out.println(studentId + " " + name + " : " + courseName + " does not exist");
return;
}

String courseType = course.getCourseType();
String assessmentMode = course.getAssessmentMode();


int numScores = tokens.length - 3;
if (courseType.equals("必修") && numScores != 2) {
System.out.println(studentId + " " + name + " : access mode mismatch");
}

if ((numScores > 2 || numScores < 1) && (assessmentMode.equals("考察"))) {
System.out.println(studentId + " " + name + " : access mode mismatch");

}

List<Score> scoreList = scores.get(studentId);

if (scoreList == null) {
scoreList = new ArrayList<>();
scores.put(studentId, scoreList);
}

switch (assessmentMode) {
case "考试":
int ordinaryScore = Integer.parseInt(tokens[3]);
int finalScore = Integer.parseInt(tokens[4]);
ExamScore examScore = new ExamScore(courseName, ordinaryScore, finalScore);
scoreList.add(examScore);
break;
case "考察":
if (numScores == 1) {
int reviewScore = Integer.parseInt(tokens[3]);
scoreList.add(new ReviewScore(courseName, 0, reviewScore));
} else {
int ordinaryScores = Integer.parseInt(tokens[3]);
int reviewScore = Integer.parseInt(tokens[4]);
scoreList.add(new ReviewScore(courseName, ordinaryScores, reviewScore));
}
break;
case "实验":
List<Integer> labScores = new ArrayList<>();
for (int i = 4; i < tokens.length; i++) {
int labScore = Integer.parseInt(tokens[i]);
labScores.add(labScore);
}
scoreList.add(new LabScore(courseName, labScores));
break;
}
}

private void calculateGrades() {
List<String> studentIds = new ArrayList<>(students.keySet());
Collections.sort(studentIds);

for (String studentId : studentIds) {
Student student = students.get(studentId);
List<Score> scoreList = scores.get(studentId);

if (scoreList == null || scoreList.isEmpty()) {
System.out.println(studentId + " " + student.getName() + " did not take any exams");
continue;
}

int totalScore = calculateTotalScore(scoreList);
int averageScore = totalScore / scoreList.size();
System.out.println(studentId + " " + student.getName() + " " + averageScore);
}

List<String> courseNames = new ArrayList<>(courses.keySet());
Collections.sort(courseNames);

for (String courseName : courseNames) {
Course course = courses.get(courseName);
String courseType = course.getCourseType();
String assessmentMode = course.getAssessmentMode();

List<Integer> examScores = new ArrayList<>();
List<Integer> ordinaryScores = new ArrayList<>();
List<Integer> totalScores = new ArrayList<>();
List<Integer> labScores = new ArrayList<>();

for (String studentId : scores.keySet()) {
List<Score> scoreList = scores.get(studentId);
Score score = findScoreByCourse(scoreList, courseName);

if (score == null) {
continue;
}

if (score instanceof ExamScore) {
ExamScore examScore = (ExamScore) score;
examScores.add(examScore.getFinalScore());
ordinaryScores.add(examScore.getOrdinaryScore());
totalScores.add(examScore.getScore());
} else if (score instanceof ReviewScore) {
ReviewScore reviewScore = (ReviewScore) score;
ordinaryScores.add(reviewScore.getOrdinaryScore());
examScores.add(reviewScore.getScore());
} else if (score instanceof LabScore) {
LabScore labScore = (LabScore) score;
labScores.add(labScore.getScore());
}
}
//
int averageExamScore = calculateAverage(examScores);
int averageOrdinaryScore = (calculateAverage(ordinaryScores) != 0) ? calculateAverage(ordinaryScores) : calculateAverage(examScores);
int averageLabScore = calculateAverage(labScores);
int averageTotalScore = calculateAverage(totalScores);

if (assessmentMode.equals("考试")) {
System.out.println(courseName + " " + averageOrdinaryScore + " " + averageExamScore + " " + averageTotalScore);
} else if (assessmentMode.equals("考察")) {
System.out.println(courseName + " " + averageOrdinaryScore + " " + averageExamScore);
} else if (assessmentMode.equals("实验")) {
System.out.println(courseName + " " + (averageLabScore != 0 ? averageLabScore : "has no grades yet"));
}
}

List<String> classIds = getClassIds();
Collections.sort(classIds);

for (String classId : classIds) {
List<String> studentsInClass = getStudentsInClass(classId);
int totalScore = 0;
int count = 0;

for (String studentId : studentsInClass) {
List<Score> scoreList = scores.get(studentId);

if (scoreList != null) {
for (Score score : scoreList) {
totalScore += score.getScore();
count++;
}
}
}

if (count > 0) {
int averageScore = totalScore / count;
System.out.println(classId + " " + averageScore);
} else {
System.out.println(classId + " has no grades yet");
}
}
}

private Score findScoreByCourse(List<Score> scoreList, String courseName) {
for (Score score : scoreList) {
if (score.getCourse().equals(courseName)) {
return score;
}
}
return null;
}

private int calculateTotalScore(List<Score> scoreList) {
int totalScore = 0;
for (Score score : scoreList) {
totalScore += score.getScore();
}
return totalScore;
}

private int calculateAverage(List<Integer> scores) {
int total = 0;
if (scores.size() != 0) {
for (int score : scores) {
total += score;
}
return total / scores.size();
}
return 0;
}

private List<String> getClassIds() {
List<String> classIds = new ArrayList<>();
for (String studentId : students.keySet()) {
String classId = studentId.substring(0, 6);
if (!classIds.contains(classId)) {
classIds.add(classId);
}
}
return classIds;
}

private List<String> getStudentsInClass(String classId) {
List<String> studentsInClass = new ArrayList<>();
for (String studentId : students.keySet()) {
if (studentId.startsWith(classId)) {
studentsInClass.add(studentId);
}
}
return studentsInClass;
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Main scoreStatistics = new Main();
StringBuffer stringBuffer = new StringBuffer();
while (scanner.hasNextLine()) {

String input = scanner.nextLine();
stringBuffer.append(input).append("\n");
if (input.equals("end")) {
break;
}

}
scoreStatistics.processAllInput(stringBuffer.toString());
}

private void processAllInput(String allInput) {
String[] input = allInput.split("\n");
for (String s : input) {
processInput(s);
}
}
}

ava每个对象都继承自Object,都有equals、toString等方法。
现在需要定义PersonOverride类并覆盖其toStringequals方法。

1. 新建PersonOverride类

a. 属性:String nameint ageboolean gender,所有的变量必须为私有(private)。

b. 有参构造方法,参数为name, age, gender

c. 无参构造方法,使用this(name, age,gender)调用有参构造方法。参数值分别为"default",1,true

d.toString()方法返回格式为:name-age-gender

e. equals方法需比较name、age、gender,这三者内容都相同,才返回true.

2. main方法

2.1 输入n1,使用无参构造方法创建n1个对象,放入数组persons1。
2.2 输入n2,然后指定name age gender。每创建一个对象都使用equals方法比较该对象是否已经在数组中存在,如果不存在,才将该对象放入数组persons2。
2.3 输出persons1数组中的所有对象
2.4 输出persons2数组中的所有对象
2.5 输出persons2中实际包含的对象的数量
2.5 使用System.out.println(Arrays.toString(PersonOverride.class.getConstructors()));输出PersonOverride的所有构造方法。

提示:使用ArrayList代替数组大幅复简化代码,请尝试重构你的代码。

import java.util.*;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n1 = scanner.nextInt();
List<PersonOverride> persons1 = new ArrayList<>();
for (int i = 0; i < n1; i++) {
PersonOverride person = new PersonOverride();
persons1.add(person);
}
int n2 = scanner.nextInt();
List<PersonOverride> persons2 = new ArrayList<>();
for (int i = 0; i < n2; i++) {
String name = scanner.next();
int age = scanner.nextInt();
boolean gender = scanner.nextBoolean();
PersonOverride person = new PersonOverride(name, age, gender);
if (!persons2.contains(person)) {
persons2.add(person);
}
}
for (PersonOverride person : persons1) {
System.out.println(person);
}
for (PersonOverride person : persons2) {
System.out.println(person);
}
System.out.println(persons2.size());
System.out.println(Arrays.toString(PersonOverride.class.getConstructors()));
}
}

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 (this == obj) {
return true;
}
if (!(obj instanceof PersonOverride)) {
return false;
}
PersonOverride person = (PersonOverride) obj;
return name.equals(person.name) && age == person.age && gender == person.gender;
}
}

标签:studentId,return,String,int,blog3,score,public
From: https://www.cnblogs.com/wangziheng/p/17512642.html

相关文章

  • BLOG3
    importjava.text.Collator;importjava.util.ArrayList;importjava.util.Collections;importjava.util.Comparator;importjava.util.Scanner;publicclassMain{privatestaticfinalintCOURSE_RECORD_TYPE=1;privatestaticfinalintSCORE_RECORD_......
  • 面向对象程序设计题目集总结blog3
    一、前言本次是第三次对面向对象程序设计题目集的总结分析博客。 关于知识点本次的题目集所体现的知识点重点考虑设计问题,许多题目不提供类图,或者只提供一个参考框架(只有类和类之间的关系),题目的代码量也较于前几次提升了不少。题目集七注重类的设计,通过三道设......
  • Blog3
    (1)前言:第7次PTA是沿袭之前的菜单题,这个题对之前的PTA题目集的完成度依赖很大,所以难度相较于成绩题更难一些。第8次PTA是成绩的第一题,开始我想采用对象数组进行编程,发现使用起来特别不方便,后来学习了set和map,在之后的成绩题目中全部改用set和map,不仅使用更加方便,得到的分数也......
  • BLOG3
    一、前言通过一个学期的学习,我们终于结束了面向对象程序设计这门课,在过去的几周时间内,我们进行了实验、pta以及最后的期末考试,课程虽然结束了,但需要学习和巩固的知识点还有很多,这也是最后一次blog总结。      显而易见,这两次的PTA作业所考察的知识点以及难度呈现不......
  • MyBlog3:4月5日
    MyBlog3:4月5日LeetCode239滑动窗口最大值importjava.util.LinkedList;//leetcodesubmitregionbegin(Prohibitmodificationanddeletion)classSolution{publicint[]maxSlidingWindow(int[]nums,intk){if(k<1||nums==null||nums.length<......
  • blog3
    一、前言这几次的作业题量适中,主要重点封装、继承、多态、抽象类的使用、正则表达式,接口,还有一些和javafx相关的知识,主要题型就是看类图补全代码,其中电信计费系列是比较难......
  • Blog3:nchu-software-oop-2022-6~8
    Blog3:nchu-software-oop-2022-6~8一、前言不知不觉,已到了本学期末尾,面向对象程序设计这门课也已经结课。课程虽然结束了,但需要学习和巩固的知识点还有很多。同时,这也......
  • Remember的Java-blog3
    一、前言    此次blog为电信计费系列的座机,手机,短信大作业总结。相对于前几次的多边形,这次在算法上更简单,但是涉及到的内容很多,比如容器的使用、抛出异常、抽象类......
  • BLOG3
    一、前言1、知识点 类和对象:  类(class)和对象(object)是两种以计算机为载体的计算机语言的合称。对象是对客观事物的抽象,类是对对象的抽象。类是一种抽象......
  • Blog3-电信计费系列题目集
    前言电信计费系列题目虽然难度相对于多边形系列有所下降,但涉及知识点很广,主要如下:1、容器的使用2、抛出异常3、抽象类4、继承与多态5、正则表达式6、类和对象设计......