第一次博客作业

一、前言:

题目总览:

第一次作业

1.身体质量指数(BMI)测算

2.长度质量计量单位换算

3.奇数求和

4.房产税费计算

5.游戏角色选择

6.学号识别

7.判断三角形类型

8.巴比伦法求平方根近似值

9.二进制数值提取

第二次作业:

1.成绩计算-1-类、数组的基本运用

2.成绩计算-2-关联类

3.有重复的数据

4.去掉重复的数据

5.面向对象编程(封装性)

6.GPS测绘中度分秒转换

7.菜单计价程序-1

8.jmu-java-日期类的基本使用

第三次作业:

1.jmu-java-日期类的基本使用

2.课程成绩统计程序-1

3.面向对象编程(封装性)

4.判断两个日期的先后,计算间隔天数、周数

​ 对于第一次作业,考验的都是java的一些基础语法以及对程序细节的处理,并未涉及有关面对对象的题目。

​ 第二第三次作业开始涉及有关面对对象的问题,特别是课程和菜单计价问题,做的晕晕乎乎的,不过经历了一段时间的学习,也对面对对象有了一些理解。

​ 总体来说题目量不算太大,但涉及面对对象知识的题目,对于我来说还是有一定难度的。

二、设计与分析:

第二次作业第一题:

创建学生类,包含

属性:学号(String)、姓名(String)、语文成绩(int)、数学成绩(int)、物理成绩(int)

方法:计算总分、计算平均分

输入5个学生的信息,将每个学生的信息封装在一个学生对象中。

按输入顺序依次输出5个学生的总分、平均分(精确到小数点后两位,舍去部分按四舍五入规则计入最后一位)。

先上源码:

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Scanner;

class Student {
    String id;
    String name;
    int chinesecode;
    int mathcode;
    int fcode;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getChinesecode() {
        return chinesecode;
    }

    public void setChinesecode(int chinesecode) {
        this.chinesecode = chinesecode;
    }

    public int getMathcode() {
        return mathcode;
    }

    public void setMathcode(int mathcode) {
        this.mathcode = mathcode;
    }

    public int getFcode() {
        return fcode;
    }

    public void setFcode(int fcode) {
        this.fcode = fcode;
    }

    Scanner cs = new Scanner(System.in);
    ArrayList<Student> students = new ArrayList<>();
    public void add() {
        for (int i = 0; i < 5; i++) {
            Student s = new Student();
            String id = cs.next();
            s.setId(id);
            String name = cs.next();
            s.setName(name);
            int chinesecode = cs.nextInt();
            s.setChinesecode(chinesecode);
            int mathcode = cs.nextInt();
            s.setMathcode(mathcode);
            int fcode = cs.nextInt();
            s.setFcode(fcode);
            students.add(s);
        }
    }
        public void allcode()
        {

        for (int i = 0; i < students.size(); i++) {
            Student s = students.get(i);
            int all = s.getChinesecode() + s.getMathcode() + s.getFcode();
            double average =  all/3.0;
            DecimalFormat df = new DecimalFormat("#.00");
            System.out.println(s.getId()+" "+s.getName()+" "+all+" "+df.format(average));


    }
        }
    }
public class Main {
    public static void main(String[] args) {
        Student s = new Student();
        s.add();
        s.allcode();
    }
}

时序图如上。

​ 程序分析:本题是一道比较简单的面向对象编程题。首先建了一个学生类,类成员包括学生ID,学生姓名,学生的语文、数学及物理成绩。通过建立一个student的ArrayList集合来存储输入的学生信息,使用add()方法存入5个学生信息,最后再通过allcode()方法计算各个学生的总成绩和平均成绩。

第二次作业第二题:

创建成绩类,包含:

属性:平时成绩(int)、期末成绩(int)

方法:计算总成绩(计算规则:平时成绩0.4+期末成绩0.6,保留整数部分,小数部分直接丢弃)

创建学生类,包含:

属性:学号(String)、姓名(String)、语文成绩(成绩类)、数学成绩(成绩类)、物理成绩(成绩类)

方法:计算总分、计算平均分

输入3个学生的信息,将每个学生的信息封装在一个学生对象中。

按输入顺序依次输出3个学生的总分、平均分(精确到小数点后两位,舍去部分按四舍五入规则计入最后一位)。

源码:

import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Student[]s = new Student[3];
        Scanner cs = new Scanner(System.in);

        for (int i = 0; i < s.length ; i++) {
            Student s1 = new Student();
            s1.chinesecode = new code();
            s1.mathcode = new code();
            s1.fcode = new code();
            for (int j = 0; j < 3; j++) {
                s1.id = cs.next();
                s1.name = cs.next();
                String a = cs.next();
                if (a.equals("语文")) {
                    s1.chinesecode.normalcode = cs.nextInt();
                    s1.chinesecode.classcode = cs.nextInt();
                } else if (a.equals("数学")) {
                    s1.mathcode.normalcode = cs.nextInt();
                    s1.mathcode.classcode = cs.nextInt();
                } else if (a.equals("物理")) {
                    s1.fcode.normalcode = cs.nextInt();
                    s1.fcode.classcode = cs.nextInt();

                }
            }
                s[i] = s1;
        }
        for (int i = 0; i <s.length; i++) {
            System.out.printf(s[i].id+" "+s[i].name+" "+s[i].getTotalcode()+" ");
            System.out.printf("%.2f",s[i].getnomalAverage());
            System.out.print(" ");
            System.out.printf("%.2f",s[i].getclassAverage());
            System.out.print(" ");
            System.out.printf("%.2f",s[i].getAverage());
            System.out.println();
        }

    }
}


 class code {
    int normalcode;
    int classcode;

    public code(int normalcode, int classcode) {
        this.normalcode = normalcode;
        this.classcode = classcode;
    }

    public code() {
    }

    public int getNormalcode() {
        return normalcode;
    }

    public void setNormalcode(int normalcode) {
        this.normalcode = normalcode;
    }

    public int getClasscode() {
        return classcode;
    }

    public void setClasscode(int classcode) {
        this.classcode = classcode;
    }

    public int allcode()
    {
        double allcode = this.normalcode*0.4+this.classcode*0.6;
        int a = (int)allcode;
        return a;
    }
}



 class Student {
    String id;
    String name;
    code chinesecode;
    code mathcode;
    code fcode;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public code getChinesecode() {
        return chinesecode;
    }

    public void setChinesecode(code chinesecode) {
        this.chinesecode = chinesecode;
    }

    public code getMathcode() {
        return mathcode;
    }

    public void setMathcode(code mathcode) {
        this.mathcode = mathcode;
    }

    public code getFcode() {
        return fcode;
    }

    public void setFcode(code fcode) {
        this.fcode = fcode;
    }

    public Student(String id, String name, code chinesecode, code mathcode, code fcode) {
        this.id = id;
        this.name = name;
        this.chinesecode = chinesecode;
        this.mathcode = mathcode;
        this.fcode = fcode;
    }

    public Student() {
    }

    public int getTotalcode()
    {
        return chinesecode.allcode()+ mathcode.allcode()+ fcode.allcode();
    }

    public double getAverage() {
        return getTotalcode()/3.0;
    }

    public double getnomalAverage()
    {
        return (chinesecode.normalcode+mathcode.normalcode+ fcode.normalcode)/3.0;
    }

    public double getclassAverage()
    {
        return (chinesecode.classcode+mathcode.classcode+ fcode.classcode)/3.0;
    }

}

时序图如上。

​ 源码分析:先建两个类student类和code类,添加相应的成员。再创建getTotalcode()方法求得学生总成绩,getAverage()方法计算学生平均成绩,getnormalAverage(),getclassAverage()方法分别计算平时平均成绩和期末平均成绩。这些类与方法的创建是比较简单的,但在输入数据方面就花了我很长时间。首先先创键一个数组存储学生信息,特别注意的是这里不仅要new学生对象,还要new成绩对象,因为这里的chinesecode,mathcode和fcode都是code类型的,并不是上题的int类型,写代码时也是在这里困很久,其余地方也没有什么可说的了。

第二次作业第七题:

某饭店提供4种菜,每种菜品的基础价格如下:
西红柿炒蛋 15
清炒土豆丝 12
麻婆豆腐 12
油淋生菜 9

设计点菜计价程序,根据输入的订单,计算并输出总价格。
订单由一条或多条点菜记录组成,每条记录一行,最后以"end"结束
每条点菜记录包含:菜名、份额两个信息。
份额可选项包括:1、2、3,分别代表小、中、大份)

不同份额菜价的计算方法:
小份菜的价格=菜品的基础价格。
中份菜的价格=菜品的基础价格1.5。
小份菜的价格=菜品的基础价格
2。
如果计算出现小数,按四舍五入的规则进行处理。

参考以下类的模板进行设计:
菜品类:对应菜谱上一道菜的信息。
Dish {
String name;//菜品名称
int unit_price; //单价
int getPrice(int portion)//计算菜品价格的方法,输入参数是点菜的份额(输入数据只能是1/2/3,代表小/中/大份)
}

菜谱类:对应菜谱,包含饭店提供的所有菜的信息。
Menu {
Dish[] dishs ;//菜品数组,保存所有菜品信息
Dish searthDish(String dishName)//根据菜名在菜谱中查找菜品信息,返回Dish对象。
}

点菜记录类:保存订单上的一道菜品记录
Record {**
Dish d;//菜品
int portion;//份额(1/2/3代表小/中/大份)
int getPrice()//计价,计算本条记录的价格
}

订单类:保存用户点的所有菜的信息。
Order {**
Record[] records;//保存订单上每一道的记录
int getTotalPrice()//计算订单的总价
Record addARecord(String dishName,int portion)
//添加一条菜品信息到订单中。
}

每条点菜记录的格式:
菜名+空格(英文)+份额
注:份额可输入(1/2/3), 1代表小份,2代表中份,3代表大份。
最后一条记录以“end”结束。

订单上所有菜品的总价(整数数值),每份菜
如果订单中包含不能识别的菜名,则在总价之前输出“** does not exist”,**是不能识别的菜名

源码:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner cs = new Scanner(System.in);
        String[]menu = new String[]{"西红柿炒蛋","清炒土豆丝","麻婆豆腐","油淋生菜"};
        Dish dish  = new Dish();
        int portion = 0;
        int price = 0;
        for(; ;) {
            int count = 0;
            dish.name = cs.next();
            if (dish.name.equals("end"))
                break;
            portion = cs.nextInt();
            if (dish.name.equals(menu[0])) {
                count = 1;
                if (portion == 1) dish.unit_price = 15;
                if (portion == 2) dish.unit_price = 23;
                if (portion == 3) dish.unit_price = 30;
                price+= dish.unit_price;
            }
            else if (dish.name.equals(menu[1])) {
                count = 1;
                if (portion == 1) dish.unit_price = 12;
                if (portion == 2) dish.unit_price = 18;
                if (portion == 3) dish.unit_price = 24;
                price+= dish.unit_price;
            }
            else if (dish.name.equals(menu[2])) {
                count = 1;
                if (portion == 1) dish.unit_price = 12;
                if (portion == 2) dish.unit_price = 18;
                if (portion == 3) dish.unit_price = 24;
                price+= dish.unit_price;
            }
            else if (dish.name.equals(menu[3])) {
                count = 1;
                if (portion == 1) dish.unit_price = 9;
                if (portion == 2) dish.unit_price = 14;
                if (portion == 3) dish.unit_price = 18;
                price+= dish.unit_price;
            }

            if (count == 0)
               
                System.out.println(dish.name +" "+ "does" + " " + "not" + " " + "exist");
        }
        System.out.println(price);
    }
}


   class Dish {
    String name;//菜品名称
    int unit_price; //单价

    public Dish(String name, int unit_price) {
        this.name = name;
        this.unit_price = unit_price;
    }

    public Dish() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getUnit_price() {
        return unit_price;
    }

    public void setUnit_price(int unit_price) {
        this.unit_price = unit_price;
    }




}

时序图如上。

程序分析:题目中给定了类的定义,无形中给这道题增加了难度,个人能力问题,我所写程序虽然可以满分通过PTA但并未使用题目中的类定义,对于面对对象来说就是0分,基本没有用到面对对象思想。

第三次作业第二题:

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

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

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

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

1、输入:

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

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

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

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

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

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

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

以上信息的相关约束:

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)成绩平均分只取整数部分,小数部分丢弃

参考类图:

image.png

源码:

import java.text.Collator;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        String cs = s.nextLine();
        ParseInput handle=new ParseInput();
        while (!cs.equals("end")) {
            handle.parseInput(cs);
            cs = s.nextLine();
        }
        handle.showStudents();
        handle.showCourses();
        handle.showClasses();
    }
}
class ParseInput{
    ArrayList<Student> listStudent=new ArrayList<>();
    ArrayList<Course> listCourse=new ArrayList<>();
    ArrayList<Class> listClass=new ArrayList<>();
    ArrayList<ChooseCourse> listChooseCourse=new ArrayList<>();
    public void parseInput(String str){
        InputMatching mat=new InputMatching();
        int a =mat.matchingInput(str);
        switch (a){
            case 0:System.out.println("wrong format");
                break;
            //课程信息
            case 1:courseMessage(str);
                break;
            //成绩信息
            case 2:gradeMessage(str);
                break;
        }
    }
    public void courseMessage(String str){

        String arr[]=str.split(" ");

        String courseName=arr[0];
        String type=arr[1];
        String testType=arr[2];

        Course course=new Course(courseName,type,testType);

        if(checkCourse(course)){
            if(searchCourse(courseName)==null)
                listCourse.add(course);
        }

    }


    public boolean checkCourse(Course course){
        int flag1,flag2;
        switch(course.getType()){
            case "必修":flag1=0;break;
            case "选修":flag1=1;break;
            default:flag1=-1;break;
        }
        switch(course.getTestType()){
            case "考试":flag2=0;break;
            case "考察":flag2=1;break;
            default:flag2=-1;break;
        }
        if(flag1==0&&flag2==0)
            return true;
        if(flag1==1&&(flag2==0||flag2==1))
            return true;
        System.out.println(course.getCourseName()+" : course type & access mode mismatch");
        return false;
    }
    public Class searchClass(String classId){
        for(Class cls:listClass){
            if(cls.getClassId().equals(classId))
                return cls;
        }
        return null;
    }
    public Course searchCourse(String name){
        for(Course course:listCourse){
            if(course.getCourseName().equals(name))
                return course;
        }
        return null;
    }
    public boolean searchStudent(String id){
        for(Student stu:listStudent){
            if(stu.getId().equals(id))
                return true;
        }
        return false;
    }
    //查找是否有重复选课成绩
    public boolean searchChooseCourse(String stuName,String courseName){
        for(ChooseCourse cs:listChooseCourse){
            if(cs.student.getStuName().equals(stuName)&&cs.course.getCourseName().equals(courseName))
                return true;
        }
        return false;
    }
    public void gradeMessage(String str){
        String dp[]=str.split(" ");

        String stuId= dp[0];//学生学号
        String classID= dp[0].substring(0,6);
        String name=dp[1];//学生姓名
        String courseName=dp[2];//课程名字

        //如果该班级第一次出现
        if(searchClass(classID)==null){
            Class cla=new Class(classID);
            listClass.add(cla);
        }

        Student stu=new Student(classID,stuId,name);
        if(!searchStudent(stuId))
            listStudent.add(stu);//将学生加入列表中
        //课程是否存在
        if(searchCourse(courseName)==null){
            System.out.println(courseName+" "+"does not exist");
        }
        //当课程存在时
        else if(searchCourse(courseName)!=null){
            Course course=searchCourse(courseName);
            //考察
            if(dp.length==4&&course.testType.equals("考察")){
                int finalGrade= Integer.parseInt(dp[3]);
                AssessGrade assessGrade=new AssessGrade(finalGrade);
                ChooseCourse chooseCourse=new ChooseCourse(course,stu,assessGrade);
                if(!searchChooseCourse(name,courseName))
                    listChooseCourse.add(chooseCourse);
            }
            //考试
            else if(dp.length==5&&course.testType.equals("考试")){
                int usualGrade= Integer.parseInt(dp[3]);
                int finalGrade= Integer.parseInt(dp[4]);
                ExamGrade examGrade=new ExamGrade(usualGrade,finalGrade);
                ChooseCourse chooseCourse=new ChooseCourse(course,stu,examGrade);
                listChooseCourse.add(chooseCourse);
            }
            else{
                //学号+英文空格+姓名+英文空格+": access mode mismatch"
                System.out.println(stuId+" "+name+" "+": access mode mismatch");
            }
        }
    }
    public void showStudents(){
        Collections.sort(listStudent);
        for(int i=0;i<listStudent.size();i++){
            Student stu=listStudent.get(i);
            //从总选课表listChooseCourse中获取该生的选课记录
            ArrayList<ChooseCourse> stuCourseSelects=getStudentSelects(stu.getId());
            if(stuCourseSelects.size()!=0) {
                System.out.println(stu.getId()+" "+stu.getStuName());
            }
            else if(stuCourseSelects.size()==0){
                System.out.println(stu.getId()+" "+stu.getStuName()+" "+"did not take any exams");
            }
        }
    }
    public void showCourses(){
        Collections.sort(listCourse);
        for(int i=0;i<listCourse.size();i++){
            Course course=listCourse.get(i);
            ArrayList<ChooseCourse> stuCourseSelects=getCourseSelects(course.getCourseName());
            if(stuCourseSelects.size()!=0){
                if(course.testType.equals("考试"))
                    System.out.println(course.getCourseName()+" "+getAvgUsualScore(stuCourseSelects));
                if(course.testType.equals("考察"))
                    System.out.println(course.getCourseName());
            }
            else if(stuCourseSelects.size()==0){
                System.out.println(course.courseName+" "+"has no grades yet");
            }
        }
    }
    public void showClasses(){
        Collections.sort(listClass);
        for(int i=0;i<listClass.size();i++){
            Class cls=listClass.get(i);
            ArrayList<ChooseCourse> stuCourseSelects=getClassSelects(cls.getClassId());
            if(stuCourseSelects.size()!=0){
                System.out.println(cls.getClassId());
            }
            else if(stuCourseSelects.size()==0){
                System.out.println(cls.getClassId()+" "+"has no grades yet");
            }
        }
    }
    public ArrayList<ChooseCourse> getStudentSelects(String id){
        ArrayList<ChooseCourse> choose=new ArrayList<>();
        for(ChooseCourse cos:listChooseCourse) {
            if (cos.student.getId().equals(id))
                choose.add(cos);
        }
        return choose;
    }
    public ArrayList<ChooseCourse> getCourseSelects(String courseName){
        ArrayList<ChooseCourse> choose=new ArrayList<>();
        for(ChooseCourse cos:listChooseCourse) {
            if (cos.course.getCourseName().equals(courseName))
                choose.add(cos);
        }
        return choose;
    }
    public ArrayList<ChooseCourse> getClassSelects(String clsId){
        ArrayList<ChooseCourse> choose =new ArrayList<>();
        for(ChooseCourse cos:listChooseCourse) {
            if (cos.student.getClsId().equals(clsId))
                choose.add(cos);
        }
        return choose;
    }

    public int getAvgUsualScore(ArrayList<ChooseCourse> cs){
        int average=0;
        int sum=0;
        for(ChooseCourse c:cs){
            if(c.course.getTestType().equals("考试")){
                sum+=c.grade.getUsualGrade();
            }
        }
        average=sum/cs.size();
        return average;
    }

}
class ChooseCourse{
    Course course;
    Student student;
    Grade grade;

    public ChooseCourse(Course course,Student student,Grade grade) {
        this.course = course;
        this.student=student;
        this.grade=grade;
    }
}
class Student implements Comparable<Student>{
    String stuName;
    String id;
    String clsId;
    public String getId(){
        return id;
    }
    public String getStuName(){
        return stuName;
    }
    public String getClsId(){
        return clsId;
    }
    public Student(String clsId,String id,String stuName) {
        this.clsId=clsId;
        this.id=id;
        this.stuName=stuName;
    }
    public int compareTo(Student stu){
        return getId().compareTo(stu.getId());
    }
}
class Course implements Comparable<Course>{
    String courseName;
    String type;
    String testType;

    public Course() {

    }
    public Course(String courseName,String type,String testType) {
        this.courseName=courseName;
        this.type=type;
        this.testType=testType;
    }
    public String getCourseName(){
        return courseName;
    }
    public String getType(){
        return type;
    }
    public String getTestType(){
        return  testType;
    }
    @Override
    public int compareTo(Course o) {
        Comparator<Object> compare = Collator.getInstance(java.util.Locale.CHINA);
        return compare.compare(courseName,o.getCourseName());
    }

}
class Class implements Comparable<Class>{
    String classId;

    public Class() {
    }

    public String getClassId(){
        return classId;
    }
    public Class(String classId) {
        this.classId = classId;
    }
    @Override
    public int compareTo(Class o) {
        return getClassId().compareTo(o.getClassId());
    }
}
abstract class Grade{
    int finalGrade;
    public Grade() {
    }
    public abstract int getUsualGrade();
    public abstract int getTotalGrade();
}
class ExamGrade extends Grade{
    int usualGrade;

    public ExamGrade(int usualGrade,int finalGrade) {
        this.usualGrade=usualGrade;
        this.finalGrade=finalGrade;
    }
    public int getUsualGrade(){
        return usualGrade;
    }
    public  int getFinalGrade(){
        return 0;
    }
    public int getTotalGrade(){
        return (int)(usualGrade*0.3+finalGrade*0.7);
    }
}
class AssessGrade extends Grade{

    public AssessGrade(int finalGrade) {
        this.finalGrade=finalGrade;
    }
    public int getFinalGrade(){
        return finalGrade;
    }

    @Override
    public int getUsualGrade() {
        return 0;
    }

    public int getTotalGrade(){
        return finalGrade;
    }
}
class InputMatching {
    static String stuNumMatching = "[0-9]{8}";
    static String stuNameMatching = "\\S{1,10}";
    static String scoreMatching = "([1-9]?[0-9]|100)";
    static String courseNameMatching = "\\S{1,10}";
    static String courseTypeMatching = "(选修|必修)";
    static String checkCourseTypeMatching = "(考试|考察)";
    //courseInput用于定义课程信息模式(正则表达式)
    static String courseInput = courseNameMatching + " " + courseTypeMatching + " " + checkCourseTypeMatching;
    //scoreInput用于定义成绩信息模式(正则表达式)
    static String scoreInput1 = stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " +
            scoreMatching ;
    static String scoreInput2=  stuNumMatching + " " + stuNameMatching + " " + courseNameMatching + " " +
            scoreMatching + " " +scoreMatching;
    public InputMatching() {
    }
    public  int matchingInput(String s) {

        if (matchingCourse(s))
            return 1;
        if (matchingScore(s))
            return 2;
        return 0;

    }
    //课程信息
    private static boolean matchingCourse(String s) {
        return s.matches(courseInput);
    }
    //成绩信息
    private static boolean matchingScore(String s) {
        //System.out.println(match);
        if(s.matches(scoreInput1)||s.matches(scoreInput2))
            return true;
        return false;
    }
}

时序图如上。

​ 代码分析:这道题非常有面对对象那味道了,参考了一些大佬的博客,才能勉强写出一些,首先建类,ParseInput类,InputMatching类,Course类,Class类,Student类,AssessGrade类,ChooseCourse类,ExamGrade类;再建一些有关的方法,注意代码之间的逻辑。个人能力原因,不能完善的分析代码。

三、踩坑心得:

说到踩坑,最多的莫过与第一次作业,由于第一次写java的pta,对基础语法不大了解及对答题规范的模糊,导致第一次作业大部分不能完美通过。

首先是数据类型的问题:

在第一次作业的第四题中:数据类型只能用float而不能用double类型;

若用double类型,则:

按理来说double类型并不是不可以,应该是测试点的原因。

还有第一次作业第7题,三角形的判断

源码:

import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        Scanner cs = new Scanner(System.in);
        double a = cs.nextDouble();
        double b = cs.nextDouble();
        double c = cs.nextDouble();
        if (a <= 200 && a >= 1 && b <= 200 && b >= 1 && c <= 200 && c >= 1) {
            if (a + b > c && a + c > b && b + c > a) {
                if (a == b && b == c)
                    System.out.print("Equilateral triangle");
                else {
                    if ((a == b) || (a == c) || (b == c)) {
                        if ((a * a + b * b - c * c  <0.01) || (a * a + c * c - b * b <0.01) || (b * b + c * c - a * a <0.01))
                            System.out.print("Isosceles right-angled triangle");
                        else
                            System.out.print("Isosceles triangle");
                    } else {
                        if ((a * a + b * b - c * c <0.01) || (a * a + c * c - b * b <0.01) || (b * b + c * c - a * a <0.01))
                            System.out.print("Right-angled triangle");
                        else
                            System.out.print("General triangle");
                    }
                }
            }
            else
                System.out.print("Not a triangle");

        }
        else
            System.out.print("Wrong Format");

      }
    }

在判断是否为直角三角形时,不能写a * a + b * b - c * c == 0而要写小于一个很小的数。不然:

四、主要困难及改进:

​ 主要困难还是在于对面对对象的理解,对面对对象相关语法的的使用以及对代码逻辑的的理解。

​ 比如第二次作业的第七题,对于题目给好了类,要求根据题目来写题,我还没有掌握此类题型,在今后的学习中我会在此类题型中下多点功夫。

五、总结:

​ 在经过三次作业后,我基本掌握了java的基本语法,但在面对对象思想及实现上显得力不从心。对于java实验,希望老师能有相应的讲解。

posted @ 2023-10-07 22:09  NovaSpark雪  阅读(42)  评论(0)    收藏  举报