(1)前言:
这三次作业加强了我们对HashMap 类的理解,训练了我们对HashMap 类中方法的掌握,再次加强对我们对继承关系中的多态的掌握。也让我们通过例子掌握ArrayList-排序,以及各种 自定义接口。而本次的迭代题目是课程成绩统计,主要还是按照类图设计代码,以及考虑字符的比较以及排序,题目的测试点和错误提示非常让我头疼。
(2)设计与分析:6-1课程成绩统计程序-1
import java.util.*;
import java.text.Collator;
import java.util.TreeMap;
class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
Data_all data = new Data_all();
String regex0 = "^[\\u4E00-\\u9FA5A-Za-z0-9]{1,10} +(必修|选修)? *(考试|考察)?$";
String regex1 = "^\\d{8}\\s+[\\u4E00-\\u9FA5A-Za-z]{1,10}\\s+[\\u4E00-\\u9FA5A-Za-z0-9]{1,10}\\s*((100)|(\\d{1,2})|(0))?\\s+((100)|(\\d{1,2})|(0))$";
while (true) {
String input = scn.nextLine();
if (input.equals("end")) {
break;
}
String[] sts = input.split(" ");
if (input.matches(regex0)) {
if (sts[1].equals("必修") && sts[2].equals("考试")) {
data.setCourses(sts[0], sts[1], sts[2]);
} else if (sts[1].equals("选修")) {
data.setCourses(sts[0], sts[1], sts[2]);
} else {
System.out.println(sts[0] + " : course type & access mode mismatch");
}
} else if (input.matches(regex1)) {
data.setClasses(sts[0].substring(0,6));
data.setStudents(sts[0].substring(0, 6), sts[1], sts[0]);
if (data.courses.containsKey(sts[2])) {
if (data.courses.get(sts[2]).type.equals("选修")) {
if (data.courses.get(sts[2]).test_way.equals("考试") && sts.length == 5) {
data.setxuanke(sts[0], sts[2], sts[3], sts[4]);
} else if (data.courses.get(sts[2]).test_way.equals("考察") && sts.length == 4) {
data.setxuanke(sts[0], sts[2], sts[3]);
} else {
System.out.println(sts[0] + " " + sts[1] + " : access mode mismatch");
}
}else if(data.courses.get(sts[2]).type.equals("必修")){
if (sts.length == 5) {
data.setxuanke(sts[0], sts[2], sts[3],sts[4]);
} else {
data.setxuanke(sts[0], sts[2], "no access", "no access");
}
}
} else {
System.out.println(sts[0] + " " + sts[1] + " " + sts[2] + " does not exist");
}
} else {
System.out.println("wrong format");
}
}
data.classes.forEach((num,Class)-> Class.students.forEach((name, student)-> data.stu_xuanke.get(student.num).gradeMap.forEach((key, value)->{
if(value instanceof kaoshi &&((kaoshi) value).normal_score.matches("\\d+")&&((kaoshi) value).end_score.matches("\\d+")) {
final float weight0 =0.3F;
final float weight1 = 0.7F;
value.total_scores = String.valueOf((int)(weight0*Integer.parseInt(((kaoshi) value).normal_score)
+ weight1*Integer.parseInt(((kaoshi) value).end_score)));
}else if(value instanceof kaocha &&((kaocha) value).end_score.matches("\\d+")){
value.total_scores = ((kaocha) value).end_score;
}
})));
data.classes.forEach((cla_num,Class1)-> Class1.students.forEach((stu_num, student)->{
int count = 0;int sum = 0;
for (Map.Entry<String, Score> entry : data.stu_xuanke.get(stu_num).gradeMap.entrySet()) {
Score value = entry.getValue();
if(Integer.parseInt(value.total_scores)>=0) {
count++;
sum += Integer.parseInt(value.total_scores);
}
}
if(count!=0)
System.out.println(stu_num+" "+Class1.students.get(stu_num).name+" "+ (sum/count));
else
System.out.println(stu_num+" "+Class1.students.get(stu_num).name+" "+"did not take any exams");
}));
for (Map.Entry<String, Course> cours : data.courses.entrySet()) {
String key = cours.getKey();
Course v = cours.getValue();
int[] tem = new int[3];
int count = 0;
for (Map.Entry<String, ChooseClass> e : data.stu_xuanke.entrySet()) {
ChooseClass value1 = e.getValue();
for (Map.Entry<String, Score> entry : value1.gradeMap.entrySet()) {
String key2 = entry.getKey();
Score value2 = entry.getValue();
if (key2.equals(key)) {
if (Integer.parseInt(value2.total_scores) >= 0) {
count++;
tem[2] += Integer.parseInt(value2.total_scores);
if (value2 instanceof kaoshi) {
if (Integer.parseInt(value2.total_scores) >= 0) {
tem[0] += Integer.parseInt(((kaoshi) value2).normal_score);
tem[1] += Integer.parseInt(((kaoshi) value2).end_score);
}
} else if (value2 instanceof kaocha) {
if (Integer.parseInt(value2.total_scores) >= 0) {
tem[0] = -100;
tem[1] += Integer.parseInt(((kaocha) value2).end_score);
}
}
}
}
}
}
if (count != 0) {
for (int i = 0; i < 3; i++) {
tem[i] = tem[i] / count;
}
} else {
for (int i = 0; i < 3; i++) {
tem[i] = -100;
}
}
if (tem[0] < 0 && tem[1] < 0 && tem[2] < 0) {
System.out.println(key + " has no grades yet");
} else {
String x = key + " " + tem[0] + " " + tem[1] + " " + tem[2];
if (v.type.equals("选修")) {
if (v.test_way.equals("考察"))
System.out.println(key + " " + tem[1] + " " + tem[2]);
else
System.out.println(x);
} else if (v.type.equals("必修")) {
System.out.println(x);
}
}
}
data.classes.forEach((num, Class) -> {
int tem;
int sum = 0;
int count = 0;
for (Map.Entry<String, Student> mapEntry : data.classes.get(num).students.entrySet()) {
Student value = mapEntry.getValue();
for (Map.Entry<String, ChooseClass> e : data.stu_xuanke.entrySet()) {
String key1 = e.getKey();
ChooseClass value1 = e.getValue();
if (key1.equals(value.num)) {
for (Map.Entry<String, Score> entry : value1.gradeMap.entrySet()) {
Score gra = entry.getValue();
if (Integer.parseInt(gra.total_scores) >= 0) {
sum += Integer.parseInt(gra.total_scores);
count++;
}
}
}
}
}
if (count != 0)
tem = sum / count;
else
tem = -100;
if (tem >= 0) {
System.out.println(num + " " + tem);
} else
System.out.println(num + " has no grades yet");
});
}
}
class Class {
String num;
TreeMap<String, Student> students = new TreeMap<>();
Class(String num){
this.num = num;
}
}
class Course {
String type;
String test_way;
String name;
Course(String name,String type, String test_way){
this.type = type;
this.name = name;
this.test_way = test_way;
}
}
class Student {
String name;
String num;
Student(String name, String num) {
this.name = name;
this.num = num;
}
}
class ChooseClass {
String num;//学生
TreeMap<String,Score> gradeMap =new TreeMap<>();
ChooseClass(String stu_name, String course, String normal_score, String test_score)
{
this.num = stu_name;
gradeMap.put(course,new kaoshi(normal_score,test_score));
}
ChooseClass(String stu_name, String kechengming, String qimo)
{
this.num = stu_name;
gradeMap.put(kechengming,new kaocha(qimo));
}
void Setgrade(String name, String pingshi, String qimo)
{
if(!gradeMap.containsKey(name))
gradeMap.put(name, new kaoshi(pingshi,qimo));
}
void Setgrade(String course, String test_score)
{
if(!gradeMap.containsKey(course))
gradeMap.put(course,new kaocha(test_score));
}
}
class Data_all {
TreeMap<String, Course> courses;
TreeMap<String, Class> classes = new TreeMap<>();
TreeMap<String, ChooseClass> stu_xuanke;
Data_all() {
stu_xuanke = new TreeMap<>((o1, o2) -> {
try {
Comparator<Object> comparator = Collator.getInstance(Locale.CHINA);
if (comparator.compare(o1, o2) < 0) {
return -1;
} else if (comparator.compare(o1, o2) > 0) {
return 1;
}
} catch (Exception ignored) {
}
return 0;
});
courses = new TreeMap<>((o1, o2) -> {
try {
Comparator<Object> comparator = Collator.getInstance(Locale.CHINA);
if (comparator.compare(o1, o2) < 0) {
return -1;
} else if (comparator.compare(o1, o2) > 0) {
return 1;
}
} catch (Exception ignored) {
}
return 0;
});
}
void setCourses(String name, String type, String test_way) {
if (!courses.containsKey(name)) {
courses.put(name, new Course(name, type, test_way));
}
}
void setClasses(String num) {
if (!classes.containsKey(num))
classes.put(num, new Class(num));
}
void setStudents(String clas_num, String name, String num) {
if (classes.containsKey(clas_num)) {
classes.get(clas_num).students.put(num, new Student(name, num));
}
}
void setxuanke(String num, String course, String normal_score, String end_score) {
if (!stu_xuanke.containsKey(num)) {
stu_xuanke.put(num, new ChooseClass(num, course, normal_score, end_score));
} else {
stu_xuanke.get(num).Setgrade(course, normal_score, end_score);
}
}
void setxuanke(String num, String course, String end_score) {
if (!stu_xuanke.containsKey(num)) {
stu_xuanke.put(num, new ChooseClass(num, course, end_score));
} else {
stu_xuanke.get(num).Setgrade(course, end_score);
}
}
}
class kaoshi extends Score
{
String normal_score;
String end_score;
kaoshi(String normal_score, String end_score) {
this.normal_score = normal_score;
this.end_score = end_score;
}
}
class kaocha extends Score{
String end_score;
kaocha(String end_score) {
this.end_score = end_score;
}
}
abstract class Score {
String total_scores = "-100";
}
7-1 容器-HashMap-检索
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;
class Main {
public static void main(String[] args) {
Scanner b = new Scanner(System.in);
String xx = null;
xx = b.nextLine();
ArrayList<xuesheng> xss = new ArrayList<>();
HashMap<String,xuesheng> map = new HashMap<String,xuesheng>();
for (; !"end".equals(xx); ) {
xuesheng xs=new xuesheng();
String[] t1 = xx.split(" ");
xs.xh=t1[0];
xs.xm=t1[1];
xs.cj=Integer.valueOf(t1[2]);
xss.add(xs);
map.put(xs.xh,xs);
xx = b.nextLine();
}
xx = b.nextLine();
if(map.get(xx)==null)
{
System.out.println("The student "+xx+" does not exist");
}
else
{
xuesheng x;
x=map.get(xx);
System.out.println(xx+" "+x.xm+" "+x.cj);}
}
}
class xuesheng{
String xh;
String xm;
int cj;
}
7-2 容器-HashMap-排序
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner b = new Scanner(System.in);
String xx = null;
xx = b.nextLine();
ArrayList<xuesheng> xss = new ArrayList<>();
ArrayList<String> ii = new ArrayList<>();
HashMap<String,xuesheng> map = new HashMap<String,xuesheng>();
for (; !"end".equals(xx); ) {
xuesheng xs=new xuesheng();
String[] t1 = xx.split(" ");
ii.add(t1[0]);
xs.xh=t1[0];
xs.xm=t1[1];
xs.cj=Integer.valueOf(t1[2]);
xss.add(xs);
map.put(xs.xh,xs);
xx = b.nextLine();
}
Collections.sort(ii);//先升序
Collections.reverse(ii);//后反转
for(String i:ii)
System.out.println(i+" "+map.get(i).xm+" "+map.get(i).cj);
}
}
class xuesheng {
String xh;
String xm;
int cj;
}
7-3 课程成绩统计程序-2
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;
public class main {
public static void main(String[] args) {
Scanner b = new Scanner(System.in);
String xx = null;
int i = 0;
int j = 0;
ArrayList<xuanke> xk = new ArrayList<>();//xk是选课
ArrayList<kecheng> kk = new ArrayList<>();//kk是课程
ArrayList<xuesheng> xs = new ArrayList<>();
ArrayList<banji> bb = new ArrayList<>();
ArrayList<chengji> cj = new ArrayList<>();
for (; !"end".equals(xx); ) {
xx = b.nextLine();
if (xx.equals("end")) {
//panduan(kk,xk);
panduan1(xs);
panduan2(kk);
panduan3(bb);
break;
}
if (xx.matches("\\S+\\s+(必修|选修)\\s(考试|考察)")) {
String[] t1 = xx.split(" ");
kecheng kc = new kecheng();
int flag = 0;
kc.km = t1[0];
kc.xz = t1[1];
kc.fs = t1[2];
if (kk.contains(kc)) {
flag = 1;
}
if (t1[1].equals("必修") && t1[2].equals("考察")) {
flag = 1;
System.out.println(t1[0] + " : course type & access mode mismatch");
System.out.println(t1[0] + " does not exist");
}
if (flag == 0) {
kk.add(kc);
}
continue;
} else if (xx.matches("\\d{8}\\s+\\S+\\s+\\S+\\s+\\d+\\s*\\d*")) {
String[] t2 = xx.split(" ");
int h = 0;
int flag = 0;
int qq = 0;
for (kecheng n : kk) {
if (n.km.equals(t2[2])) {
flag = 1;
qq = kk.indexOf(n);
}
}
if (flag == 1 && kk.get(qq).fs.equals("考试")) {
//先存起来,后面再判断和输出
int flag1 = 0;
banji bjj = new banji();
xuesheng xss = new xuesheng();
kaoshi cjj = new kaoshi();
xuanke xkk = new xuanke();
cjj.setchengji(Integer.valueOf(t2[3]), Integer.valueOf(t2[4]));
if (xss.cj != 0)
xss.cj = (xss.cj + cjj.cj) / 2;
else
xss.cj = cjj.cj;
if (!xs.contains(xss)) {
xs.add(xss);
}
if (kk.get(qq).zcj != 0)
kk.get(qq).zcj = (kk.get(qq).zcj + cjj.cj) / 2;
else
kk.get(qq).zcj = cjj.cj;
if (kk.get(qq).pscj != 0)
kk.get(qq).pscj = (kk.get(qq).pscj + Integer.valueOf(t2[3])) / 2;
else
kk.get(qq).pscj = Integer.valueOf(t2[3]);
if (kk.get(qq).qmcj != 0)
kk.get(qq).qmcj = (kk.get(qq).qmcj + Integer.valueOf(t2[4])) / 2;
else
kk.get(qq).qmcj = Integer.valueOf(t2[4]);
if (bjj.cj != 0)
bjj.cj = (bjj.cj + cjj.cj) / 2;
else
bjj.cj = cjj.cj;
bjj.banjihao = xx.substring(0, 6);
if (!bb.contains(bjj)) {
bb.add(bjj);
}
//bb.indexOf(bjj);
xss.xingming = t2[1];
xss.xuehao = t2[0];
xss.b = bb.get(bb.indexOf(bjj));
cj.add(cjj);
xkk.x = xss;
xkk.c = cjj;
xkk.k = kk.get(qq);
xk.add(xkk);
// panduan(kk,x,i-1,j);
continue;
} else if (flag == 1 && kk.get(qq).fs.equals("考察")) {
//先存起来,后面再判断和输出
int flag1 = 0;
banji bjj = new banji();
xuesheng xss = new xuesheng();
kaocha cjj = new kaocha();
xuanke xkk = new xuanke();
cjj.setchengji(Integer.valueOf(t2[3]));
if (xss.cj != 0)
xss.cj = (xss.cj + cjj.cj) / 2;
else
xss.cj = cjj.cj;
if (kk.get(qq).zcj != 0)
kk.get(qq).zcj = (kk.get(qq).zcj + cjj.cj) / 2;
else
kk.get(qq).zcj = cjj.cj;
if (kk.get(qq).qmcj != 0)
kk.get(qq).qmcj = (kk.get(qq).qmcj + cjj.cj) / 2;
else
kk.get(qq).qmcj = cjj.cj;
if (bjj.cj != 0)
bjj.cj = (bjj.cj + cjj.cj) / 2;
else
bjj.cj = cjj.cj;
bjj.banjihao = xx.substring(0, 6);
if (!bb.contains(bjj)) {
bb.add(bjj);
}
//bb.indexOf(bjj);
xss.xingming = t2[1];
xss.xuehao = t2[0];
xss.b = bb.get(bb.indexOf(bjj));
if (!xs.contains(xss)) {
xs.add(xss);
}
cj.add(cjj);
xkk.x = xss;
xkk.c = cjj;
xkk.k = kk.get(qq);
xk.add(xkk);
// panduan(kk,x,i-1,j);
continue;
} else {
//System.out.println(t2[0]+" "+t2[1]+" "+":"+t2[2]+" "+"does not exist");
continue;
}
}
}
}
/*public static void panduan(ArrayList<kecheng> list, ArrayList<xuanke> list1){
int flag=0;
for (kecheng n : list) {
flag=0;
for (xuanke m : list1){
if (n.km.equals(m.k.km))
{flag=1;
}
}
if(flag==0)
System.out.println(n.km+" has no grades yet");
}
}*/
public static void panduan1(ArrayList<xuesheng> list2) {
for (xuesheng n : list2) {
if (n.cj != 0 && n.cj <= 100 && n.cj > 0)
System.out.println(n.xuehao + " " + n.xingming + " " + n.cj);
else if (n.cj > 100 || n.cj < 0) {
System.out.println("wrong format");
} else if (n.cj == 0)
System.out.println(n.xuehao + " " + n.xingming + " " + "has no grades yet");
}
}
public static void panduan2(ArrayList<kecheng> list2) {
for (kecheng n : list2) {
if (n.zcj != 0&&n.pscj!=0)
System.out.println(n.km + " " + n.pscj + " " + n.qmcj + " " + n.zcj);
else if(n.zcj != 0&&n.pscj==0)
System.out.println(n.km + " " + n.qmcj + " " + n.zcj);
else
System.out.println(n.km + " " + "has no grades yet");
}
}
public static void panduan3(ArrayList<banji> list2) {
for (banji n : list2) {
if (n.cj != 0 && n.cj <= 100 && n.cj > 0)
System.out.println(n.banjihao + " " + n.cj);
else if (n.cj == 0) {
System.out.println(n.banjihao + "has no grades yet");
}
}
}}
class xuanke {//选课
chengji c;
xuesheng x;
kecheng k;
public void setchengji(chengji a) {
this.c = a;
}
public void setxuesheng(xuesheng a) {
this.x = a;
}
public void setkecheng(kecheng a) {
this.k = a;
}
}
class kecheng {
String km;//课程名称
String xz;//课程性质
String fs;//考核方式
int pscj = 0;
int qmcj = 0;
int zcj = 0;
}
class xuesheng {//学生
banji b;
String xuehao;
String xingming;
int cj = 0;
}
class banji {
String banjihao;
int cj = 0;
}
class chengji {
int cj;
}
class kaocha extends chengji {
public void setchengji(int a) {
this.cj = a;
}
}
class kaoshi extends chengji {
public void setchengji(int a, int b) {
this.cj = (int) (a * 0.3 + b * 0.7);
}
}
7-4 动物发声模拟器(多态)
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.*;
//动物发生模拟器. 请在下面的【】处添加代码。
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){
animal.shout();
}
}
//定义抽象类Animal
class Animal{
public static void shout(){}
}
//基于Animal类,定义猫类Cat,并重写两个抽象方法
class Cat extends Animal{
//@override
public static void shout(){
System.out.println("猫的叫声:喵喵");
}
}
//基于Animal类,定义狗类Dog,并重写两个抽象方法
class Dog extends Animal{
//@override
public static void shout(){
System.out.println("狗的叫声:汪汪");
}
}
//基于Animal类,定义山羊类Goat,并重写两个抽象方法
class Goat extends Animal{
//@override
public static void shout(){
System.out.println("山羊的叫声:咩咩");
}
}
7-3 jmu-Java-02基本语法-03-身份证排序
import java.util.Arrays;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc=new Scanner (System.in);
int n;
n=sc.nextInt();
String s[]=new String[n];
String birth[]=new String[n];
String str;
for(int i=0;i<n;i++) {
s[i]=sc.next();
}
while(true) {
str=sc.next();
if(str.equals("e")) {
System.out.println("exit");
break;
}else if(str.equals("sort1")) {
for(int i=0;i<n;i++) {
birth[i]=s[i].substring(6,10 )+'-'+ s[i].substring(10, 12)+'-'+s[i].substring(12, 14);
}
Arrays.sort(birth);
for(int i=0;i<n;i++) {
System.out.println(birth[i]);
}
}else if(str.equals("sort2")) {
for(int i=0;i<n;i++) {
birth[i]=s[i].substring(6,10 )+ s[i].substring(10, 12)+s[i].substring(12, 14);
}
Arrays.sort(birth);
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++){
if(s[j].contains(birth[i])) {
System.out.println(s[j]);
break;
}
}
}
}
}
}
}
7-1 容器-ArrayList-排序
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner b = new Scanner(System.in);
String xx = null;
xx = b.nextLine();
ArrayList<xuesheng> xss = new ArrayList<>();
ArrayList<Integer> cjj = new ArrayList<>();
HashMap<Integer,xuesheng> map = new HashMap<Integer,xuesheng>();
for (; !"end".equals(xx); ) {
xuesheng xs=new xuesheng();
String[] t1 = xx.split(" ");
xs.xh=t1[0];
xs.xm=t1[1];
xs.scj=Integer.valueOf(t1[2]);
xs.wcj=Integer.valueOf(t1[3]);
xs.cj=xs.scj+xs.wcj;
cjj.add(xs.cj);
map.put(xs.cj,xs);
xss.add(xs);
xx = b.nextLine();
}
class okk implements Comparator<xuesheng>{
@Override
public int compare(xuesheng u1, xuesheng u2) {
BigDecimal num1 = new BigDecimal(u1.cj);
BigDecimal num2 = new BigDecimal(u2.cj);
return (num2 - num1);
}
}
Collections.sort(xss, new okk());
for(xuesheng i:xss)
System.out.println(i.xh+" "+i.xm+" "+i.cj);
}
}
class xuesheng{
String xh;
String xm;
int scj;
int wcj;
int cj;
}
(3)采坑心得:对源码的提交过程中出现的问题及心得进行总结,务必做到详实,拿数据、源码及测试结果说话,切忌假大空
(4)改进建议:对相应题目的编码改进给出自己的见解,做到可持续改进
(5)总结:对本阶段(10-16周)综合性总结,学到了什么,哪些地方需要进一步学习及研究,对教师、课程、作业、实验、课上及课下组织方式等方面的改进建议及意见。
标签:cj,String,get,int,BLOG,num,new From: https://www.cnblogs.com/byetears/p/17499513.html