首页 > 其他分享 >pta第三次博客

pta第三次博客

时间:2022-12-09 17:59:05浏览次数:37  
标签:inputs return String ArrayList 博客 void pta public 第三次

目录

pta第三次博客

1.前言

本次博客主要分析电信计费的三个作业,相较于之前的多边形,这三次作业相对简单一点,主要的难点还是理解老师给出的几张类图,分析起来很麻烦,要花费的时间比真正写代码的时间还要长,因为里面的继承关系很多很杂,经常写到一半又忘了这个类的继承对象。其次就是正则表达式,因为输入的规范要求很严格,就导致正则经常满足了这一条又不满足另一条,而且改起来很麻烦。

2.设计与分析

第6次作业第一题

1.题目:
7-1 电信计费系列1-座机计费
实现一个简单的电信计费程序:
假设南昌市电信分公司针对市内座机用户采用的计费方式:
月租20元,接电话免费,市内拨打电话0.1元/分钟,省内长途0.3元/分钟,国内长途拨打0.6元/分钟。不足一分钟按一分钟计。
南昌市的区号:0791,江西省内各地市区号包括:0790~0799以及0701。

输入格式:
输入信息包括两种类型
1、逐行输入南昌市用户开户的信息,每行一个用户,
格式:u-号码 计费类型 (计费类型包括:0-座机 1-手机实时计费 2-手机A套餐)
例如:u-079186300001 0
座机号码除区号外由是7-8位数字组成。
本题只考虑计费类型0-座机计费,电信系列2、3题会逐步增加计费类型。
2、逐行输入本月某些用户的通讯信息,通讯信息格式:
座机呼叫座机:t-主叫号码 接听号码 起始时间 结束时间
t-079186330022 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:11
以上四项内容之间以一个英文空格分隔,
时间必须符合"yyyy.MM.dd HH:mm:ss"格式。提示:使用SimpleDateFormat类。
以上两类信息,先输入所有开户信息,再输入所有通讯信息,最后一行以“end”结束。
注意:
本题非法输入只做格式非法的判断,不做内容是否合理的判断(时间除外,否则无法计算),比如:
1、输入的所有通讯信息均认为是同一个月的通讯信息,不做日期是否在同一个月还是多个月的判定,直接将通讯费用累加,因此月租只计算一次。
2、记录中如果同一电话号码的多条通话记录时间出现重合,这种情况也不做判断,直接 计算每条记录的费用并累加。
3、用户区号不为南昌市的区号也作为正常用户处理。

输出格式:
根据输入的详细通讯信息,计算所有已开户的用户的当月费用(精确到小数点后2位,
单位元)。假设每个用户初始余额是100元。
每条通讯信息单独计费后累加,不是将所有时间累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。

错误处理:
输入数据中出现的不符合格式要求的行一律忽略。

建议类图:
参见图1、2、3,可根据理解自行调整:
img
图1中User是用户类,包括属性:
userRecords (用户记录)、balance(余额)、chargeMode(计费方式)、number(号码)。

ChargeMode是计费方式的抽象类:
chargeRules是计费方式所包含的各种计费规则的集合,ChargeRule类的定义见图3。
getMonthlyRent()方法用于返回月租(monthlyRent)。

UserRecords是用户记录类,保存用户各种通话、短信的记录,
各种计费规则将使用其中的部分或者全部记录。
其属性从上到下依次是:
市内拨打电话、省内(不含市内)拨打电话、省外拨打电话、
市内接听电话、省内(不含市内)接听电话、省外接听电话的记录
以及发送短信、接收短信的记录。
img
图2中CommunicationRecord是抽象的通讯记录类:
包含callingNumber拨打号码、answerNumber接听号码两个属性。
CallRecord(通话记录)、MessageRecord(短信记录)是它的子类。

CallRecord(通话记录类)包含属性:
通话的起始、结束时间以及
拨号地点的区号(callingAddressAreaCode)、接听地点的区号(answerAddressAreaCode)。
区号用于记录在哪个地点拨打和接听的电话。座机无法移动,就是本机区号,如果是手机号,则会有差异。
img
图3是计费规则的相关类,这些类的核心方法是:
calCost(ArrayList callRecords)。
该方法针根据输入参数callRecords中的所有记录计算某用户的某一项费用;如市话费。
输入参数callRecords的约束条件:必须是某一个用户的符合计费规则要求的所有记录。

LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三个类分别是
座机拨打市内、省内、省外电话的计费规则类,用于实现这三种情况的费用计算。
(提示:可以从UserRecords类中获取各种类型的callRecords)。
后续扩展说明:
后续题目集将增加手机用户,手机用户的计费方式中除了与座机计费类似的主叫通话费之外,还包含市外接听电话的漫游费以及发短信的费用。在本题的设计时可统一考虑。
通话记录中,手机需要额外记录拨打/接听的地点的区号,比如:
座机打手机:t-主叫号码 接听号码 接听地点区号 起始时间 结束时间
t-079186330022 13305862264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
手机互打:t-主叫号码 拨号地点 接听号码 接听地点区号 起始时间 结束时间
t-18907910010 0791 13305862264 0371 2022.1.3 10:00:25 2022.1.3 10:05:11
短信的格式:m-主叫号码,接收号码,短信内容
m-18907910010 13305862264 welcome to jiangxi
m-13305862264 18907910010 thank you
试题分析
1.本题需要根据给出的类图来确定各个主要的类的属性以及各个类之间的继承关系
2.需要通过正则表达式来判断输入的开户信息和通话记录是否符合格式要求
3.本题只要考虑南昌的座机打完市内,省内和省外的座机的通话记录来计费和月租来计算总费用
源代码展示:

查看代码
import java.util.Comparator;
import java.util.ArrayList;
import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.text.ParseException;

public class Main {
    public static void main(String[] args) {
        ArrayList<User> users = new ArrayList<>();
        Input inputdeal = new Input();
        Reduce e=new Reduce();
        Scanner in = new Scanner(System.in);
        String input= in.nextLine();
        while(!input.equals("end")){
            if(input.matches("[u]-[0][7]([9][0-9]|[0][1])[0-9]{7,8} [0-3]") || input.matches("^t-0\\d{9,11}\\s0\\d{9,11}((\\s\\d{4}\\.([1-9]|([1]{1}[0-2]{1}))\\.([1-9]|([1-2]{1}[0-9]{1})|3[0-1])\\s(([0-1][0-9])|(2[0-3])):([0-5][0-9]):([0-5][0-9])){2})")){
                String[] line=input.split(" ");
                if(line.length==2){
                    inputdeal.writeUser(users, input);
                }
                if(line.length==6){
                    inputdeal.writeRecord(users, input);
                }
            }
            input= in.nextLine();
        }
        users.sort(new Comparator<User>() {
            @Override
            public int compare(User u1, User u2) {
                if (Double.parseDouble(u1.getNumber()) > Double.parseDouble(u2.getNumber())) {
                    return 1;
                } else {
                    return -1;
                }
            }
        });

        for (User u : users) {
            System.out.println(u.getNumber() + " "+e.exchange(u.calCost())+ " "+e.exchange(u.calBalance()));
        }
    }
}
class Input {
    public void writeUser(ArrayList<User> users, String input) {
        User newuser = new User();
        String[] inputs = input.split(" ");
        String num = inputs[0].substring(2);
        for (int i=0;i<users.size();i++) {
            User user=users.get(i);
            if (user.getNumber().equals(num)) {
                return;
            }
        }
        newuser.setNumber(num);
        int mode = Integer.parseInt(inputs[1]);
        if (mode == 0) {
            newuser.setChargeMode(new LandlinePhoneCharging());
        }
        users.add(newuser);
    }

    public void writeRecord(ArrayList<User> users, String input) {
        String[] inputs = input.split(" ");
        inputs[0] = inputs[0].replace("t-", "");
        User call = null, answer = null;
        CallRecord callrecord = new CallRecord(inputs);

        for (int i=0;i<users.size();i++) {
            User user=users.get(i);
            if (user.getNumber().equals(inputs[0])) {
                call = user;
            }
            if (user.getNumber().equals(inputs[1])) {
                answer= user;
            }
            if (call != null && answer != null) {
                break;
            }
        }

        if (call != null) {
            if (callrecord.getCallType() == 1) {
                call.getUserRecords().addCallingInCityRecords(callrecord);
            } else if (callrecord.getCallType() == 2) {
                call.getUserRecords().addCallingInProvinceRecords(callrecord);
            } else {
                call.getUserRecords().addCallingInLandRecords(callrecord);
            }
        }

        if (answer != null) {
            if (callrecord.getCallType() == 1) {
                answer.getUserRecords().addAnswerInCityRecords(callrecord);
            } else if (callrecord.getCallType() == 2) {
                answer.getUserRecords().addAnswerInProvinceRecords(callrecord);
            } else {
                answer.getUserRecords().addAnswerInLandRecords(callrecord);
            }
        }

    }

}
class User {

    private UserRecords userRecords = new UserRecords();
    private double balance = 100;
    private ChargeMode chargeMode;
    private String number;

    public double calCost() {
        return chargeMode.calCost(userRecords);
    }

    public double calBalance() {
        return balance - chargeMode.getMonthlyRent() - chargeMode.calCost(userRecords);
    }

    public UserRecords getUserRecords() {
        return userRecords;
    }

    public void setUserRecords(UserRecords userRecords) {
        this.userRecords = userRecords;
    }

    public ChargeMode getChargeMode() {
        return chargeMode;
    }

    public void setChargeMode(ChargeMode chargeMode) {
        this.chargeMode = chargeMode;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

}
class UserRecords {

    private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
    private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();
    private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();

    public void addCallingInCityRecords(CallRecord callRecord) {
        callingInCityRecords.add(callRecord);
    }

    public void addCallingInProvinceRecords(CallRecord callRecord) {
        callingInProvinceRecords.add(callRecord);
    }

    public void addCallingInLandRecords(CallRecord callRecord) {
        callingInLandRecords.add(callRecord);
    }

    public void addAnswerInCityRecords(CallRecord callRecord) {
        answerInCityRecords.add(callRecord);
    }

    public void addAnswerInProvinceRecords(CallRecord callRecord) {
        answerInProvinceRecords.add(callRecord);
    }

    public void addAnswerInLandRecords(CallRecord callRecord) {
        answerInLandRecords.add(callRecord);
    }

    public void addSendMessageRecords(MessageRecord callRecord) {
        sendMessageRecords.add(callRecord);
    }

    public void addReceiveMessageRecords(MessageRecord callRecord) {
        receiveMessageRecords.add(callRecord);
    }

    public ArrayList<CallRecord> getCallingInCityRecords() {
        return callingInCityRecords;
    }

    public void setCallingInCityRecords(ArrayList<CallRecord> callingInCityRecords) {
        this.callingInCityRecords = callingInCityRecords;
    }

    public ArrayList<CallRecord> getCallingInProvinceRecords() {
        return callingInProvinceRecords;
    }

    public void setCallingInProvinceRecords(ArrayList<CallRecord> callingInProvinceRecords) {
        this.callingInProvinceRecords = callingInProvinceRecords;
    }

    public ArrayList<CallRecord> getCallingInLandRecords() {
        return callingInLandRecords;
    }

    public void setCallingInLandRecords(ArrayList<CallRecord> callingInLandRecords) {
        this.callingInLandRecords = callingInLandRecords;
    }

    public ArrayList<CallRecord> getAnswerInCityRecords() {
        return answerInCityRecords;
    }

    public void setAnswerInCityRecords(ArrayList<CallRecord> answerInCityRecords) {
        this.answerInCityRecords = answerInCityRecords;
    }

    public ArrayList<CallRecord> getAnswerInProvinceRecords() {
        return answerInProvinceRecords;
    }

    public void setAnswerInProvinceRecords(ArrayList<CallRecord> answerInProvinceRecords) {
        this.answerInProvinceRecords = answerInProvinceRecords;
    }

    public ArrayList<CallRecord> getAnswerInLandRecords() {
        return answerInLandRecords;
    }

    public void setAnswerInLandRecords(ArrayList<CallRecord> answerInLandRecords) {
        this.answerInLandRecords = answerInLandRecords;
    }

    public ArrayList<MessageRecord> getSendMessageRecords() {
        return sendMessageRecords;
    }

    public void setSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords) {
        this.sendMessageRecords = sendMessageRecords;
    }

    public ArrayList<MessageRecord> getReceiveMessageRecords() {
        return receiveMessageRecords;
    }

    public void setReceiveMessageRecords(ArrayList<MessageRecord> receiveMessageRecords) {
        this.receiveMessageRecords = receiveMessageRecords;
    }

}
abstract class CommunicationRecord {
    protected String callingNumber;
    protected String answerNumbe;

    public String getCallingNumber() {return callingNumber;}

    public void setCallingNumber(String callingNumber) {this.callingNumber = callingNumber;}

    public String getAnswerNumbe() {return answerNumbe;}

    public void setAnswerNumbe(String answerNumbe) {this.answerNumbe = answerNumbe;}
}

class MessageRecord extends CommunicationRecord {

    private String message;

    public String getMessage() {return message;}

    public void setMessage(String message) {this.message = message;}
}

abstract class ChargeMode {
    protected ArrayList<ChargeRule> chargeRules = new ArrayList<>();

    public abstract double calCost(UserRecords userRecords);

    public abstract double getMonthlyRent();

    public ArrayList<ChargeRule> getChargeRules() {return chargeRules;}

    public void setChargeRules(ArrayList<ChargeRule> chargeRules) {this.chargeRules = chargeRules;}
}

abstract class CallChargeRule extends ChargeRule {

}

abstract class ChargeRule {
    abstract public double calCost(UserRecords userRecords);
}

class LandlinePhoneCharging extends ChargeMode {
    private double monthlyRent = 20;

    LandlinePhoneCharging() {
        chargeRules.add(new LandPhoneInCityRule());
        chargeRules.add(new LandPhoneInProvinceRule());
        chargeRules.add(new LandPhoneInlandRule());
    }

    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (ChargeRule rule : chargeRules) {
            sumCost += rule.calCost(userRecords);
        }
        return sumCost;
    }

    @Override
    public double getMonthlyRent() {
        return monthlyRent;
    }

}
class LandPhoneInCityRule extends CallChargeRule {

    @Override
    public double calCost(UserRecords userRecords) {
        double sum=0;
        for (CallRecord call : userRecords.getCallingInCityRecords()) {
            double S = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (S < 0) {
                continue;
            }
            double M = (int) S / 60;
            if (S % 60 != 0) {
                M += 1;
            }
            sum += M * 0.1;
        }
        return sum;
    }
}
class LandPhoneInProvinceRule extends CallChargeRule {

    @Override
    public double calCost(UserRecords userRecords) {
        double sum=0;
        for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
            double S = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (S < 0) {
                continue;
            }
            double M = (int) S / 60;
            if (S % 60 != 0) {
                M += 1;
            }
            sum += M * 0.3;
        }
        return sum;
    }
}

class LandPhoneInlandRule extends CallChargeRule {
    @Override
    public double calCost(UserRecords userRecords) {
        double sum=0,S,M;
        for (CallRecord call : userRecords.getCallingInLandRecords()) {
            S = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (S < 0) {
                continue;
            }
            M = (int) S / 60;
            if (S % 60 != 0) {
                M += 1;
            }
            sum += M * 0.6;
        }
        return sum;
    }
}

class CallRecord extends CommunicationRecord {
    private Date startTime;
    private Date endTime;
    private String callingAddressAreaCode;
    private String answerAddressAreaCode;
    public int getCallType() {
        if (callingAddressAreaCode.equals(answerAddressAreaCode)) {
            return 1;
        }
        if (callingAddressAreaCode.matches("^079[0-9]$") || callingAddressAreaCode.equals("0701")) {
            if (answerAddressAreaCode.matches("^079[0-9]$") || answerAddressAreaCode.equals("0701")) {
                return 2;
            }
        }
        return 3;
    }
    public CallRecord(String[] input){
        if(input[0].length()==10){
            callingAddressAreaCode = input[0].substring(0, 3);
        } else {
            callingAddressAreaCode = input[0].substring(0, 4);
        }
        if (input[1].length() == 10) {
            answerAddressAreaCode = input[1].substring(0, 3);
        } else {
            answerAddressAreaCode = input[1].substring(0, 4);
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
        try {
            startTime = simpleDateFormat.parse(input[2] + " " + input[3]);
            endTime = simpleDateFormat.parse(input[4] + " " + input[5]);
        } catch (ParseException e) {
        }
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public String getCallingAddressAreaCode() {
        return callingAddressAreaCode;
    }

    public void setCallingAddressAreaCode(String callingAddressAreaCode) {
        this.callingAddressAreaCode = callingAddressAreaCode;
    }

    public String getAnswerAddressAreaCode() {
        return answerAddressAreaCode;
    }

    public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
        this.answerAddressAreaCode = answerAddressAreaCode;
    }
}
class Reduce{
    public double exchange(double a){
        String c=String.format("%."+2+"f",a);a= Double.parseDouble(c);
        return a;
    }
}

SourceMonitor生成的报表内容:
img
类图
img
代码分析总结:
总体来说,本题只要先写出正则表达式,再按照题目给出的类图写好大部分的类,再将三种计费类型的计费计算算法写好,大致就结束了。

第6次作业第二题

题目:
7-2 多态测试
定义容器Container接口。模拟实现一个容器类层次结构,并进行接口的实现、抽象方法重写和多态机制测试。各容器类实现求表面积、体积的方法。

定义接口Container:
属性:
public static final double pi=3.1415926;
抽象方法:
public abstract double area();
public abstract double volume();
static double sumofArea(Container c[]);
static double sumofVolume(Container c[]);
其中两个静态方法分别计算返回容器数组中所有对象的面积之和、周长之和;
定义Cube类、Cylinder类均实现自Container接口。
Cube类(属性:边长double类型)、Cylinder类(属性:底圆半径、高,double类型)。

输入格式:

第一行n表示对象个数,对象类型用cube、cylinder区分,cube表示立方体对象,后面输入边长,输入cylinder表示圆柱体对象,后面是底圆半径、高。
输出格式:
分别输出所有容器对象的表面积之和、体积之和,结果保留小数点后2位。
试题分析
本题考查对多态的理解,根据题目要求写好类和继承关系就行
源代码展示:

查看代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int i,n=in.nextInt();
        Container[] sum=new Container[n];
        for(i=0;i<n;i++){
            String input = in.next();
            if(input.equals("cube")){
                double a=in.nextDouble();
                sum[i]=new Cube(a);
            }
            if(input.equals("cylinder")){
                double r=in.nextDouble();
                double h=in.nextDouble();
                sum[i]=new Cylinder(r,h);
            }
        }

        System.out.printf("%.2f\n%.2f",Container.sumofArea(sum),Container.sumofVolume(sum));
    }
}

abstract class Container {
    public static final double pi=3.1415926;
    public abstract double area();
    public abstract double volume();

    static double sumofArea(Container[] c) {
        int i;double S=0;
        for(i=0;i<c.length;i++){
            S+=c[i].area();
        }
        return S;
    }

    static double sumofVolume(Container[] c) {
        int i;double V=0;
        for(i=0;i<c.length;i++){
            V+=c[i].volume();
        }
        return V;
    }
}
class Cube extends Container{
    double a;
    Cube(double a){
        this.a=a;
    }
    @Override
    public double volume() {
        return Math.pow(this.a,3);
    }

    @Override
    public double area() {
        return 6*Math.pow(this.a,2);
    }
}
class Cylinder extends Container{
    double r,h;
Cylinder(double r,double h){
        this.r=r;
        this.h=h;
    }
    @Override
    public double volume() {
        return Math.pow(this.r,2)* this.pi* this.h;
    }

    @Override
    public double area() {
        return 2* this.pi*Math.pow(this.r,2)+2* this.pi* this.r* this.h;
    }
}



SourceMonitor生成的报表内容:
img
类图
img
代码分析总结:
本题难度不高,确定好继承关系就行

第七次作业第一题

题目:
电信计费系列2-手机+座机计费
实现南昌市电信分公司的计费程序,假设该公司针对手机和座机用户分别采取了两种计费方案,分别如下:
1、针对市内座机用户采用的计费方式(与电信计费系列1内容相同):
月租20元,接电话免费,市内拨打电话0.1元/分钟,省内长途0.3元/分钟,国内长途拨打0.6元/分钟。不足一分钟按一分钟计。
假设本市的区号:0791,江西省内各地市区号包括:0790~0799以及0701。
2、针对手机用户采用实时计费方式:
月租15元,市内省内接电话均免费,市内拨打市内电话0.1元/分钟,市内拨打省内电话0.2元/分钟,市内拨打省外电话0.3元/分钟,省内漫游打电话0.3元/分钟,省外漫游接听0.3元/分钟,省外漫游拨打0.6元/分钟;
注:被叫电话属于市内、省内还是国内由被叫电话的接听地点区号决定,比如以下案例中,南昌市手机用户13307912264在区号为020的广州接听了电话,主叫号码应被计算为拨打了一个省外长途,同时,手机用户13307912264也要被计算省外接听漫游费:
u-13307912264 1
t-079186330022 13307912264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
输入格式:
输入信息包括两种类型
1、逐行输入南昌市用户开户的信息,每行一个用户,含手机和座机用户
格式:u-号码 计费类型 (计费类型包括:0-座机 1-手机实时计费 2-手机A套餐)
例如:u-079186300001 0
座机号码由区号和电话号码拼接而成,电话号码包含7-8位数字,区号最高位是0。
手机号码由11位数字构成,最高位是1。
本题在电信计费系列1基础上增加类型1-手机实时计费。
手机设置0或者座机设置成1,此种错误可不做判断。
2、逐行输入本月某些用户的通讯信息,通讯信息格式:
座机呼叫座机:t-主叫号码 接听号码 起始时间 结束时间
t-079186330022 058686330022 2022.1.3 10:00:25 2022.1.3 10:05:11
以上四项内容之间以一个英文空格分隔,
时间必须符合"yyyy.MM.dd HH:mm:ss"格式。提示:使用SimpleDateFormat类。
输入格式增加手机接打电话以及收发短信的格式,手机接打电话的信息除了号码之外需要额外记录拨打/接听的地点的区号,比如:
座机打手机:
t-主叫号码 接听号码 接听地点区号 起始时间 结束时间
t-079186330022 13305862264 020 2022.1.3 10:00:25 2022.1.3 10:05:11
手机互打:
t-主叫号码 拨号地点 接听号码 接听地点区号 起始时间 结束时间
t-18907910010 0791 13305862264 0371 2022.1.3 10:00:25 2022.1.3 10:05:11

注意:以上两类信息,先输入所有开户信息,再输入所有通讯信息,最后一行以“end”结束。

输出格式:
根据输入的详细通讯信息,计算所有已开户的用户的当月费用(精确到小数点后2位,单位元)。假设每个用户初始余额是100元。
每条通讯、短信信息均单独计费后累加,不是将所有信息累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。
错误处理:
输入数据中出现的不符合格式要求的行一律忽略。

本题只做格式的错误判断,无需做内容上不合理的判断,比如同一个电话两条通讯记录的时间有重合、开户号码非南昌市的号码等,此类情况都当成正确的输入计算。但时间的输入必须符合要求,比如不能输入2022.13.61 28:72:65。
建议类图:
参见图1、2、3:

img

图1中User是用户类,包括属性:
userRecords (用户记录)、balance(余额)、chargeMode(计费方式)、number(号码)。
ChargeMode是计费方式的抽象类:
chargeRules是计费方式所包含的各种计费规则的集合,ChargeRule类的定义见图3。
getMonthlyRent()方法用于返回月租(monthlyRent)。
UserRecords是用户记录类,保存用户各种通话、短信的记录,
各种计费规则将使用其中的部分或者全部记录。
其属性从上到下依次是:
市内拨打电话、省内(不含市内)拨打电话、省外拨打电话、
市内接听电话、省内(不含市内)接听电话、省外接听电话的记录
以及发送短信、接收短信的记录。
img

图2中CommunicationRecord是抽象的通讯记录类:
包含callingNumber拨打号码、answerNumber接听号码两个属性。
CallRecord(通话记录)、MessageRecord(短信记录)是它的子类。CallRecord(通话记录类)包含属性:
通话的起始、结束时间以及
拨号地点的区号(callingAddressAreaCode)、接听地点的区号(answerAddressAreaCode)。
区号用于记录在哪个地点拨打和接听的电话。座机无法移动,就是本机区号,如果是手机号,则会有差异。
img
图3是计费规则的相关类,这些类的核心方法是:
calCost(ArrayList callRecords)。
该方法针根据输入参数callRecords中的所有记录计算某用户的某一项费用;如市话费。
输入参数callRecords的约束条件:必须是某一个用户的符合计费规则要求的所有记录。
SendMessageRule是发送短信的计费规则类,用于计算发送短信的费用。
LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三个类分别是座机拨打市内、省内、省外电话的计费规则类,用于实现这三种情况的费用计算。
(提示:可以从UserRecords类中获取各种类型的callRecords)。
注意:以上图中所定义的类不是限定要求,根据实际需要自行补充或修改。
试题分析
本题是在上一题的基础上加入了移动电话,所以需要在上一题的代码的基础上加入移动电话与座机,移动电话与移动电话以及更多的通话类型如省内的户主在省外向省内打电话之类等更复杂的情况。
源代码展示:

查看代码

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.text.ParseException;


public class Main {
    public static void main(String[] args) {
        ArrayList<User> users = new ArrayList<>();
        Input inputdeal = new Input();
        Reduce e=new Reduce();
        Scanner in = new Scanner(System.in);
        String input= in.nextLine();
        while(!input.equals("end")){
            if(input.matches("[u]-([0][7]([9][0-9]|[0][1])[0-9]{7,8}) [0-3]")
                    ||input.matches("[u]-[1][0-9]{10} [0-3]")
                    ||input.matches("(([t]-0791[0-9]{7,8}\\s0[0-9]{9,11})|([t]-0791[0-9]{7,8}\\s1[0-9]{10}\\s0[0-9]{2,3})|([t]-1[0-9]{10}\\s0[0-9]{2,3}\\s0[0-9]{9,11})|([t]-1[0-9]{10}\\s0[0-9]{2,3}\\s1[0-9]{10}\\s0[0-9]{2,3}))(\\s\\d{4}\\.([1-9]|([1]{1}[0-2]{1}))\\.([1-9]|([1-2]{1}[0-9]{1})|3[0-1])\\s(([0-1][0-9])|(2[0-3]))\\:([0-5][0-9])\\:([0-5][0-9])){2}")==true)

            {String[] line=input.split(" ");
                if(line.length==2){
                    inputdeal.writeUser(users, input);
                }
                if(line.length==6||line.length==7||line.length==8){
                    inputdeal.writeRecord(users, input);
                }
            }
            input= in.nextLine();
        }
        users.sort(new Comparator<User>() {

            @Override
            public int compare(User u1, User u2) {
                if (u1.getNumber().charAt(0) == '0' && u2.getNumber().charAt(0) != '0') {
                    return -1;
                } else if (u1.getNumber().charAt(0) != '0' && u2.getNumber().charAt(0) == '0') {
                    return 1;
                }
                if (Double.parseDouble(u1.getNumber()) > Double.parseDouble(u2.getNumber())) {
                    return 1;
                } else {
                    return -1;
                }
            }
        });

        for (User u : users) {
            //System.out.println(u.getUserRecords().getCallingInCityRecords().size());
            System.out.println(u.getNumber() + " "+e.exchange(u.calCost())+ " "+e.exchange(u.calBalance()));
        }
    }
}
class Input {
    public void writeUser(ArrayList<User> users, String input) {
        User newuser = new User();
        String[] inputs = input.split(" ");
        String num = inputs[0].substring(2);
        for (int i=0;i<users.size();i++) {
            User user=users.get(i);
            if (user.getNumber().equals(num)) {
                return;
            }
        }
        newuser.setNumber(num);
        int mode = Integer.parseInt(inputs[1]);
        if (mode == 0) {
            newuser.setChargeMode(new LandlinePhoneCharging());
        } else if (mode ==1) {
            newuser.setChargeMode(new MobilePhoneCharging());
        }
        users.add(newuser);
    }

    public void writeRecord(ArrayList<User> users, String input) {
        String[] inputs = input.split(" ");

        User callu = null, answeru = null;
        CallRecord callrecord = new CallRecord(inputs);

        if (input.charAt(0) == 't') {
            String out = inputs[0];
            String in = "";
            if (inputs.length == 6) {
                in = inputs[1];
            } else if (inputs.length == 7) {
                in = inputs[1];
            } else if (inputs.length == 8) {
                in = inputs[2];
            }

            for (User i : users) {
                if (i.getNumber().equals(out)) {
                    callu = i;
                }
                if (i.getNumber().equals(in)) {
                    answeru = i;
                }
                if (callu != null && answeru != null) {
                    break;
                }
            }

            if (callu != null) {
                if (callrecord.getCallType().matches("^1[1-3]$")) {
                    callu.getUserRecords().addCallingInCityRecords(callrecord);
                } else if (callrecord.getCallType().matches("^2[1-3]$")) {
                    callu.getUserRecords().addCallingInProvinceRecords(callrecord);
                } else {
                    callu.getUserRecords().addCallingInLandRecords(callrecord);
                }
            }

            if (answeru != null) {
                if (callrecord.getCallType().matches("^[1-3]1$")) {
                    answeru.getUserRecords().addAnswerInCityRecords(callrecord);
                } else if (callrecord.getCallType().matches("^[1-3]2$")) {
                    answeru.getUserRecords().addAnswerInProvinceRecords(callrecord);
                } else {
                    answeru.getUserRecords().addAnswerInLandRecords(callrecord);
                }
            }
        } else if (input.charAt(0) == 'm') {

        }

    }

}

class User {

    private UserRecords userRecords = new UserRecords();
    private double balance = 100;
    private ChargeMode chargeMode;
    private String number;

    public double calCost() {
        return chargeMode.calCost(userRecords);
    }

    public double calBalance() {
        return balance - chargeMode.getMonthlyRent() - chargeMode.calCost(userRecords);
    }

    public UserRecords getUserRecords() {
        return userRecords;
    }

    public void setUserRecords(UserRecords userRecords) {
        this.userRecords = userRecords;
    }

    public ChargeMode getChargeMode() {
        return chargeMode;
    }

    public void setChargeMode(ChargeMode chargeMode) {
        this.chargeMode = chargeMode;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

}
class UserRecords {

    private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
    private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();
    private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();

    public void addCallingInCityRecords(CallRecord callRecord) {
        callingInCityRecords.add(callRecord);
    }

    public void addCallingInProvinceRecords(CallRecord callRecord) {
        callingInProvinceRecords.add(callRecord);
    }

    public void addCallingInLandRecords(CallRecord callRecord) {
        callingInLandRecords.add(callRecord);
    }

    public void addAnswerInCityRecords(CallRecord callRecord) {
        answerInCityRecords.add(callRecord);
    }

    public void addAnswerInProvinceRecords(CallRecord callRecord) {
        answerInProvinceRecords.add(callRecord);
    }

    public void addAnswerInLandRecords(CallRecord callRecord) {
        answerInLandRecords.add(callRecord);
    }

    public void addSendMessageRecords(MessageRecord callRecord) {
        sendMessageRecords.add(callRecord);
    }

    public void addReceiveMessageRecords(MessageRecord callRecord) {
        receiveMessageRecords.add(callRecord);
    }

    public ArrayList<CallRecord> getCallingInCityRecords() {
        return callingInCityRecords;
    }

    public void setCallingInCityRecords(ArrayList<CallRecord> callingInCityRecords) {
        this.callingInCityRecords = callingInCityRecords;
    }

    public ArrayList<CallRecord> getCallingInProvinceRecords() {
        return callingInProvinceRecords;
    }

    public void setCallingInProvinceRecords(ArrayList<CallRecord> callingInProvinceRecords) {
        this.callingInProvinceRecords = callingInProvinceRecords;
    }

    public ArrayList<CallRecord> getCallingInLandRecords() {
        return callingInLandRecords;
    }

    public void setCallingInLandRecords(ArrayList<CallRecord> callingInLandRecords) {
        this.callingInLandRecords = callingInLandRecords;
    }

    public ArrayList<CallRecord> getAnswerInCityRecords() {
        return answerInCityRecords;
    }

    public void setAnswerInCityRecords(ArrayList<CallRecord> answerInCityRecords) {
        this.answerInCityRecords = answerInCityRecords;
    }

    public ArrayList<CallRecord> getAnswerInProvinceRecords() {
        return answerInProvinceRecords;
    }

    public void setAnswerInProvinceRecords(ArrayList<CallRecord> answerInProvinceRecords) {
        this.answerInProvinceRecords = answerInProvinceRecords;
    }

    public ArrayList<CallRecord> getAnswerInLandRecords() {
        return answerInLandRecords;
    }

    public void setAnswerInLandRecords(ArrayList<CallRecord> answerInLandRecords) {
        this.answerInLandRecords = answerInLandRecords;
    }

    public ArrayList<MessageRecord> getSendMessageRecords() {
        return sendMessageRecords;
    }

    public void setSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords) {
        this.sendMessageRecords = sendMessageRecords;
    }

    public ArrayList<MessageRecord> getReceiveMessageRecords() {
        return receiveMessageRecords;
    }

    public void setReceiveMessageRecords(ArrayList<MessageRecord> receiveMessageRecords) {
        this.receiveMessageRecords = receiveMessageRecords;
    }

}
abstract class CommunicationRecord {
    protected String callingNumber;
    protected String answerNumbe;

    public String getCallingNumber() {return callingNumber;}

    public void setCallingNumber(String callingNumber) {this.callingNumber = callingNumber;}

    public String getAnswerNumbe() {return answerNumbe;}

    public void setAnswerNumbe(String answerNumbe) {this.answerNumbe = answerNumbe;}
}

class MessageRecord extends CommunicationRecord {

    private String message;

    public String getMessage() {return message;}

    public void setMessage(String message) {this.message = message;}
}

abstract class ChargeMode {
    protected ArrayList<ChargeRule> chargeRules = new ArrayList<>();

    public abstract double calCost(UserRecords userRecords);

    public abstract double getMonthlyRent();

    public ArrayList<ChargeRule> getChargeRules() {return chargeRules;}

    public void setChargeRules(ArrayList<ChargeRule> chargeRules) {this.chargeRules = chargeRules;}
}

abstract class CallChargeRule extends ChargeRule {

}

abstract class ChargeRule {
    abstract public double calCost(UserRecords userRecords);
}

class LandlinePhoneCharging extends ChargeMode {
    private double monthlyRent = 20;

    LandlinePhoneCharging() {
        chargeRules.add(new LandPhoneInCityRule());
        chargeRules.add(new LandPhoneInProvinceRule());
        chargeRules.add(new LandPhoneInlandRule());
    }

    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (ChargeRule rule : chargeRules) {
            sumCost += rule.calCost(userRecords);
        }
        return sumCost;
    }

    @Override
    public double getMonthlyRent() {
        return monthlyRent;
    }

}
class LandPhoneInCityRule extends CallChargeRule {

    @Override
    public double calCost(UserRecords userRecords) {
        double sum=0;
        for (CallRecord call : userRecords.getCallingInCityRecords()) {
            double S = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (S < 0) {
                continue;
            }
            double M = (int) S / 60;
            if (S % 60 != 0) {
                M += 1;
            }
            if (call.getCallType().equals("11")) {
                sum += M * 0.1;
            } else if (call.getCallType().equals("12")) {
                sum += M * 0.3;
            } else if (call.getCallType().equals("13")) {
                sum += M * 0.6;
            }
        }
        return sum;
    }
}
class LandPhoneInProvinceRule extends CallChargeRule {

    @Override
    public double calCost(UserRecords userRecords) {
        double sum=0;
        for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
            double S = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (S < 0) {
                continue;
            }
            double M = (int) S / 60;
            if (S % 60 != 0) {
                M += 1;
            }
            sum += M * 0.3;
        }
        return sum;
    }
}

class LandPhoneInlandRule extends CallChargeRule {
    @Override
    public double calCost(UserRecords userRecords) {
        double sum=0,S,M;
        for (CallRecord call : userRecords.getCallingInLandRecords()) {
            S = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (S < 0) {
                continue;
            }
            M = (int) S / 60;
            if (S % 60 != 0) {
                M += 1;
            }
            sum += M * 0.6;
        }
        return sum;
    }
}


class MobilePhoneCharging extends ChargeMode {

    private double monthlyRent = 15;

    public MobilePhoneCharging() {
        super();
        chargeRules.add(new MobilePhoneInCityRule());
        chargeRules.add(new MobilePhoneInProvinceRule());
        chargeRules.add(new MobilePhoneInlandRule());
    }

    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (ChargeRule rule : chargeRules) {
            sumCost += rule.calCost(userRecords);
        }
        return sumCost;
    }

    @Override
    public double getMonthlyRent() {
        return monthlyRent;
    }

}

class MobilePhoneInCityRule extends CallChargeRule {

    @Override
    public double calCost(UserRecords userRecords) {
        double sum=0;
        for (CallRecord call : userRecords.getCallingInCityRecords()) {
            double S = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (S < 0) {
                continue;
            }
            double M = (int) S / 60;
            if (S % 60 != 0) {
                M += 1;
            }
            if (call.getCallType().equals("11")) {
                sum += M * 0.1;
            } else if (call.getCallType().equals("12")) {
                sum += M * 0.2;
            } else if (call.getCallType().equals("13")) {
                sum += M * 0.3;
            }
        }
        //System.out.println("A "+sum);
        return sum;
    }
}

class MobilePhoneInProvinceRule extends CallChargeRule {

    @Override
    public double calCost(UserRecords userRecords) {
        double sum=0;
        for (CallRecord call : userRecords.getCallingInProvinceRecords()) {
            double S = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (S < 0) {
                continue;
            }
            double M = (int) S / 60;
            if (S % 60 != 0) {
                M += 1;
            }
            if (call.getCallType().equals("21")) {
                sum += M * 0.3;
            } else if (call.getCallType().equals("22")) {
                sum += M * 0.3;
            } else if (call.getCallType().equals("23")) {
                sum += M * 0.3;
            }
//            System.out.println(M);
//            System.out.println(call.getCallType());
        }
        //System.out.println("B "+sum);

        return sum;
    }
}

class MobilePhoneInlandRule extends CallChargeRule {
    @Override
    public double calCost(UserRecords userRecords) {
        double sum=0,S,M;
        for (CallRecord call : userRecords.getCallingInLandRecords()) {
            S = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (S < 0) {
                continue;
            }
            M = (int) S / 60;
            if (S % 60 != 0) {
                M += 1;
            }
            sum += M * 0.6;
        }
        for (CallRecord call : userRecords.getAnswerInLandRecords()) {
            S = (-call.getStartTime().getTime() + call.getEndTime().getTime()) / 1000;
            if (S < 0) {
                continue;
            }
            M = (int) S / 60;
            if (S % 60 != 0) {
                M += 1;
            }
            sum += M * 0.3;
        }
        //System.out.println("C "+sum);
        return sum;
    }
}

class CallRecord extends CommunicationRecord {
    private Date startTime;
    private Date endTime;
    private String callingAddressAreaCode;
    private String answerAddressAreaCode;
    public String getCallType() {
        String type = "";
        if (callingAddressAreaCode.equals("0791")) {
            type = type.concat("1");
        } else if (callingAddressAreaCode.matches("^079[023456789]$") || callingAddressAreaCode.equals("0701")) {
            type = type.concat("2");
        } else {
            type = type.concat("3");
        }

        if (answerAddressAreaCode.equals("0791")) {
            type = type.concat("1");
        } else if (answerAddressAreaCode.matches("^079[023456789]$") || answerAddressAreaCode.equals("0701")) {
            type = type.concat("2");
        } else {
            type = type.concat("3");
        }

        return type;
    }
    public CallRecord(String[] inputs) {
        super();
        char type = inputs[0].charAt(0);
        inputs[0] = inputs[0].substring(2);
        String sd = null, st = null, ed = null, et = null;

        if (type == 't') {
            if (inputs.length == 6) {
                sd = inputs[2];
                st = inputs[3];
                ed = inputs[4];
                et = inputs[5];
                callingAddressAreaCode = inputs[0].substring(0, 4);
                answerAddressAreaCode = inputs[1].substring(0, 4);
            } else if (inputs.length == 7) {
                sd = inputs[3];
                st = inputs[4];
                ed = inputs[5];
                et = inputs[6];
                if (inputs[0].charAt(0) != '0') {
                    if (inputs[2].length() == 10) {
                        answerAddressAreaCode = inputs[2].substring(0, 3);
                    } else {
                        answerAddressAreaCode = inputs[2].substring(0, 4);
                    }
                    callingAddressAreaCode = inputs[1];
                } else {
                    if (inputs[0].length() == 10) {
                        callingAddressAreaCode = inputs[0].substring(0, 3);
                    } else {
                        callingAddressAreaCode = inputs[0].substring(0, 4);
                    }
                    answerAddressAreaCode = inputs[2];
                }
            } else if (inputs.length == 8) {
                sd = inputs[4];
                st = inputs[5];
                ed = inputs[6];
                et = inputs[7];
                callingAddressAreaCode = inputs[1];
                answerAddressAreaCode = inputs[3];
            }
        } else if (type == 'm') {

        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
        try {
            startTime = simpleDateFormat.parse(sd + " " + st);
            endTime = simpleDateFormat.parse(ed + " " + et);
        } catch (ParseException e) {
        }

    }

    public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) {
        super();
        this.startTime = startTime;
        this.endTime = endTime;
        this.callingAddressAreaCode = callingAddressAreaCode;
        this.answerAddressAreaCode = answerAddressAreaCode;
    }
    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public String getCallingAddressAreaCode() {
        return callingAddressAreaCode;
    }

    public void setCallingAddressAreaCode(String callingAddressAreaCode) {
        this.callingAddressAreaCode = callingAddressAreaCode;
    }

    public String getAnswerAddressAreaCode() {
        return answerAddressAreaCode;
    }

    public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
        this.answerAddressAreaCode = answerAddressAreaCode;
    }
}
class Reduce{
    public double exchange(double a){
        String c=String.format("%."+2+"f",a);a= Double.parseDouble(c);
        return a;
    }
}


SourceMonitor生成的报表内容:
img
类图
img
代码分析总结:
本题只要理清拨号用户与接听用户的位置,同时不仅要计算拨号用户的计费,还要计算漫游接听用户的费用,如果上一次的代码够全面的话,这次只要加上多出的几种计费算法即可。

第七次作业第二题

题目:
sdut-Collection-sort--C~K的班级(II)
经过不懈的努力,C~K终于当上了班主任。

现在他要统计班里学生的名单,但是C~K在教务系统中导出班级名单时出了问题,发现会有同学的信息重复,现在他想把重复的同学信息删掉,只保留一个,
但是工作量太大了,所以找到了会编程的你,你能帮他解决这个问题吗?

输入格式:
第一行输入一个N,代表C~K导出的名单共有N行(N<100000).

接下来的N行,每一行包括一个同学的信息,学号 姓名 年龄 性别。

输出格式:
第一行输出一个n,代表删除重复名字后C~K的班级共有几人。

接下来的n行,输出每一个同学的信息,输出按照学号从小到大的顺序。
试题分析
本题需要定义一个学生类,再将学生类存入一个list里,再去除list中重复的学生信息,然后排序输出即可
源代码展示:

查看代码
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<String> stu=new ArrayList<>();
        Scanner in = new Scanner(System.in);
        int n=in.nextInt();
        in.nextLine();
        stu.add(in.nextLine());
        for(int i=0;i<n-1;i++){
            String s=in.nextLine();
            for(int j=0;j<stu.size();j++) {
                if(s.equals(stu.get(j)))
                    break;
                else if(j==stu.size()-1){
                    stu.add(s);
                }
            }
        }
        ArrayList<Student> student=new ArrayList<>();
        for(int j=0;j<stu.size();j++) {
            String s[];
            s=stu.get(j).split(" ");
            //System.out.println(s[0]+s[1]+s[2]+s[3]);
            student.add(new Student(s[0],s[1],s[2],s[3])) ;
        }
        Collections.sort(student,new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getSno().compareTo(o2.getSno());
            }
        });
        System.out.println(student.size());
        for(int j=0;j<student.size();j++) {
            System.out.println(student.get(j).getSno()+" "+student.get(j).Sna+" "+student.get(j).age+" "+student.get(j).sex);
        }
    }
}
class Student {
    
    String Sno;
    String Sna;
    String age;
    String sex;
    public String getSno() {
        return Sno;
    }

    public void setSno(String sno) {
        Sno = sno;
    }
    Student(String Sno,String Sna,String age,String sex){
        this.age=age;
        this.sex=sex;
        this.Sna=Sna;
        this.Sno=Sno;
    }

}
/*
学号 姓名 年龄 性别
 */


SourceMonitor生成的报表内容:
img
类图
img
代码分析总结:
本题只要稍微分析一下就行,难度不高。

第七次作业第三题

题目:
阅读程序,按照题目需求修改程序
功能需求:
使用集合存储3个员工的信息(有序);
通过迭代器依次找出所有的员工。
提示:学生复制以下代码到编程区,并按需求进行调试修改。

// 1、导入相关包

//定义员工类
class Employee {

private String name;
private int age;

public Employee() {
	super();
}

public Employee(String name, int age) {
	super();
	this.name = name;
	this.age = age;
}

public String getName() {
	return name;
}

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

public int getAge() {
	return age;
}

public void setAge(int age) {
	this.age = age;
}

}

//主函数
public class Main {

public static void main(String[] args) {
			// 1、创建有序集合对象
			Collection c ;

  // 创建3个员工元素对象
	for (int i = 0; i < 3; i++) {
		Scanner sc = new Scanner(System.in);
		String employeeName = sc.nextLine();
		int employeeAge = sc.nextInt();
		
		Employee employee = new Employee(employeeName, employeeAge);
		c.add(employee);
	}			
			
			
			
			// 2、创建迭代器遍历集合
			Iterator it;
			
			//3、遍历
			while (it.hasnext) {
				
				//4、集合中对象未知,向下转型
				Employee e =  it.next();
				
				System.out.println(e.getName() + "---" + e.getAge());
			}
}

}

输入格式:
输入样例:
在这里给出一组输入。例如:

zs
10
ls
20
ww
30

输出格式:
输出样例:
在这里给出相应的输出。例如:

zs---10
ls---20
ww---30
试题分析
本题已经给出了代码,只要创建有序集合对象和创建迭代器遍历集合就行
源代码展示:

查看代码


import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Scanner;

// 1、导入相关包
//主函数
public class Main {

    public static void main(String[] args) {
        // 1、创建有序集合对象
        Collection<Employee> c = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        // 创建3个员工元素对象
        for (int i = 0; i < 3; i++) {
            String employeeName = sc.next().replace(" ","");
            int employeeAge = sc.nextInt();
            Employee employee = new Employee(employeeName, employeeAge);
            c.add(employee);
        }

        // 2、创建迭代器遍历集合
        Iterator<Employee> it = c.iterator();

        //3、遍历
        while (it.hasNext()) {
            //4、集合中对象未知,向下转型
            Employee e =  it.next();
            System.out.println(e.getName() + "---" + e.getAge());
        }
    }

}

//定义员工类
class Employee {

    private String name;
    private int age;

    public Employee() {
        super();
    }

    public Employee(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}




SourceMonitor生成的报表内容:
img
类图
img
代码分析总结:
本题基本没有难度,不做分析

第八次作业第一题

题目:
电信计费系列3-短信计费
实现一个简单的电信计费程序,针对手机的短信采用如下计费方式:
1、接收短信免费,发送短信0.1元/条,超过3条0.2元/条,超过5条0.3元/条。
2、如果一次发送短信的字符数量超过10个,按每10个字符一条短信进行计算。

输入格式:
输入信息包括两种类型
1、逐行输入南昌市手机用户开户的信息,每行一个用户。
格式:u-号码 计费类型 (计费类型包括:0-座机 1-手机实时计费 2-手机A套餐 3-手机短信计费)
例如:u-13305862264 3
座机号码由区号和电话号码拼接而成,电话号码包含7-8位数字,区号最高位是0。
手机号码由11位数字构成,最高位是1。
本题只针对类型3-手机短信计费。
2、逐行输入本月某些用户的短信信息,短信的格式:
m-主叫号码,接收号码,短信内容 (短信内容只能由数字、字母、空格、英文逗号、英文句号组成)
m-18907910010 13305862264 welcome to jiangxi.
m-13305862264 18907910010 thank you.

注意:以上两类信息,先输入所有开户信息,再输入所有通讯信息,最后一行以“end”结束。

输出格式:
根据输入的详细短信信息,计算所有已开户的用户的当月短信费用(精确到小数点后2位,单位元)。假设每个用户初始余额是100元。
每条短信信息均单独计费后累加,不是将所有信息累计后统一计费。
格式:号码+英文空格符+总的话费+英文空格符+余额
每个用户一行,用户之间按号码字符从小到大排序。
错误处理:
输入数据中出现的不符合格式要求的行一律忽略。
本题只做格式的错误判断,无需做内容上不合理的判断,比如同一个电话两条通讯记录的时间有重合、开户号码非南昌市的号码、自己给自己打电话等,此类情况都当成正确的输入计算。但时间的输入必须符合要求,比如不能输入2022.13.61 28:72:65。

本题只考虑短信计费,不考虑通信费用以及月租费。

建议类图:
参见图1、2、3:

img

图1

图1中User是用户类,包括属性:
userRecords (用户记录)、balance(余额)、chargeMode(计费方式)、number(号码)。
ChargeMode是计费方式的抽象类:
chargeRules是计费方式所包含的各种计费规则的集合,ChargeRule类的定义见图3。
getMonthlyRent()方法用于返回月租(monthlyRent)。
UserRecords是用户记录类,保存用户各种通话、短信的记录,
各种计费规则将使用其中的部分或者全部记录。
其属性从上到下依次是:
市内拨打电话、省内(不含市内)拨打电话、省外拨打电话、
市内接听电话、省内(不含市内)接听电话、省外接听电话的记录
以及发送短信、接收短信的记录。
img
图2

图2中CommunicationRecord是抽象的通讯记录类:

包含callingNumber拨打号码、answerNumber接听号码两个属性。
CallRecord(通话记录)、MessageRecord(短信记录)是它的子类。
img
图3
图3是计费规则的相关类,这些类的核心方法是:
calCost(ArrayList callRecords)。
该方法针根据输入参数callRecords中的所有记录计算某用户的某一项费用;如市话费。
输入参数callRecords的约束条件:必须是某一个用户的符合计费规则要求的所有记录。
SendMessageRule是发送短信的计费规则类,用于计算发送短信的费用。
LandPhoneInCityRule、LandPhoneInProvinceRule、LandPhoneInLandRule三个类分别是座机拨打市内、省内、省外电话的计费规则类,用于实现这三种情况的费用计算。

(提示:可以从UserRecords类中获取各种类型的callRecords)。
注意:以上图中所定义的类不是限定要求,根据实际需要自行补充或修改。
试题分析
本题没有了之前的通话计费进行计算,只需要对发短信进行计费,所以只需要将第六次作业的代码中通信计费换成短信计费,再写好短信计费的代码就行
源代码展示:

查看代码

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.text.ParseException;

public class Main {

    public static void main(String[] args) {
        Inputdeal inputdeal = new Inputdeal();
        ArrayList<User> users = new ArrayList<>();
        Reduce e=new Reduce();
        Scanner in = new Scanner(System.in);
        String input = in.nextLine();
        while (!input.equals("end")) {
            if (1 == inputdeal.check(input)) {
                inputdeal.writeUser(users, input);
            } else if (2 == inputdeal.check(input)) {
                inputdeal.writeRecord(users, input);
            }
            input = in.nextLine();
        }

        users.sort(new Comparator<User>() {
            @Override
            public int compare(User u1, User u2) {
                if (u1.getNumber().charAt(0) == '0' && u2.getNumber().charAt(0) != '0') {
                    return -1;
                } else if (u1.getNumber().charAt(0) != '0' && u2.getNumber().charAt(0) == '0') {
                    return 1;
                }
                if (Double.parseDouble(u1.getNumber()) > Double.parseDouble(u2.getNumber())) {
                    return 1;
                } else {
                    return -1;
                }
            }
        });

        for (User u : users) {
            //System.out.println(u.getUserRecords().getCallingInCityRecords().size());
            System.out.println(u.getNumber() + " "+e.exchange(u.calCost())+ " "+e.exchange(u.calBalance()));
        }
    }

}

abstract class ChargeMode {
    protected ArrayList<ChargeRule> chargeRules = new ArrayList<>();

    public abstract double calCost(UserRecords userRecords);

    public abstract double getMonthlyRent();

    public ArrayList<ChargeRule> getChargeRules() {
        return chargeRules;
    }

    public void setChargeRules(ArrayList<ChargeRule> chargeRules) {
        this.chargeRules = chargeRules;
    }
}

class UserRecords {

    private ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
    private ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
    private ArrayList<MessageRecord> sendMessageRecords = new ArrayList<MessageRecord>();
    private ArrayList<MessageRecord> receiveMessageRecords = new ArrayList<MessageRecord>();

    public void addCallingInCityRecords(CallRecord callRecord) {
        callingInCityRecords.add(callRecord);
    }

    public void addCallingInProvinceRecords(CallRecord callRecord) {
        callingInProvinceRecords.add(callRecord);
    }

    public void addCallingInLandRecords(CallRecord callRecord) {
        callingInLandRecords.add(callRecord);
    }

    public void addAnswerInCityRecords(CallRecord callRecord) {
        answerInCityRecords.add(callRecord);
    }

    public void aaddAnswerInProvinceRecords(CallRecord callRecord) {
        answerInProvinceRecords.add(callRecord);
    }

    public void addAnswerInLandRecords(CallRecord callRecord) {
        answerInLandRecords.add(callRecord);
    }

    public void addSendMessageRecords(MessageRecord callRecord) {
        sendMessageRecords.add(callRecord);
    }

    public void addReceiveMessageRecords(MessageRecord callRecord) {
        receiveMessageRecords.add(callRecord);
    }

    public ArrayList<CallRecord> getCallingInCityRecords() {
        return callingInCityRecords;
    }

    public void setCallingInCityRecords(ArrayList<CallRecord> callingInCityRecords) {
        this.callingInCityRecords = callingInCityRecords;
    }

    public ArrayList<CallRecord> getCallingInProvinceRecords() {
        return callingInProvinceRecords;
    }

    public void setCallingInProvinceRecords(ArrayList<CallRecord> callingInProvinceRecords) {
        this.callingInProvinceRecords = callingInProvinceRecords;
    }

    public ArrayList<CallRecord> getCallingInLandRecords() {
        return callingInLandRecords;
    }

    public void setCallingInLandRecords(ArrayList<CallRecord> callingInLandRecords) {
        this.callingInLandRecords = callingInLandRecords;
    }

    public ArrayList<CallRecord> getAnswerInCityRecords() {
        return answerInCityRecords;
    }

    public void setAnswerInCityRecords(ArrayList<CallRecord> answerInCityRecords) {
        this.answerInCityRecords = answerInCityRecords;
    }

    public ArrayList<CallRecord> getAnswerInProvinceRecords() {
        return answerInProvinceRecords;
    }

    public void setAnswerInProvinceRecords(ArrayList<CallRecord> answerInProvinceRecords) {
        this.answerInProvinceRecords = answerInProvinceRecords;
    }

    public ArrayList<CallRecord> getAnswerInLandRecords() {
        return answerInLandRecords;
    }

    public void setAnswerInLandRecords(ArrayList<CallRecord> answerInLandRecords) {
        this.answerInLandRecords = answerInLandRecords;
    }

    public ArrayList<MessageRecord> getSendMessageRecords() {
        return sendMessageRecords;
    }

    public void setSendMessageRecords(ArrayList<MessageRecord> sendMessageRecords) {
        this.sendMessageRecords = sendMessageRecords;
    }

    public ArrayList<MessageRecord> getReceiveMessageRecords() {
        return receiveMessageRecords;
    }

    public void setReceiveMessageRecords(ArrayList<MessageRecord> receiveMessageRecords) {
        this.receiveMessageRecords = receiveMessageRecords;
    }

}


class MobilePhoneMassageCharging extends ChargeMode {

    private double monthlyRent = 0;

    public MobilePhoneMassageCharging() {
        super();
        chargeRules.add(new MobilePhoneMessageRule());
    }

    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        for (ChargeRule rule : chargeRules) {
            sumCost += rule.calCost(userRecords);
        }
        return sumCost;
    }

    @Override
    public double getMonthlyRent() {
        return monthlyRent;
    }

}

class Inputdeal {

    public int check(String input) {
        if (input.matches("[u]-0791[0-9]{7,8}\\s[0]") || input.matches("[u]-1[0-9]{10}\\s[13]")) {
            return 1;
        } else if (input.matches("[m]-1[0-9]{10}\\s" + "1[0-9]{10}\\s" + "[0-9a-zA-Z\\s\\.,]+")) {
            return 2;
        }
        return 0;
    }

    public void writeUser(ArrayList<User> users, String input) {
        User usernew = new User();
        String[] inputs = input.split(" ");
        String num = inputs[0].substring(2);
        for (User i : users) {
            if (i.getNumber().equals(num)) {
                return;
            }
        }
        usernew.setNumber(num);
        int mode = Integer.parseInt(inputs[1]);
        if (mode == 3) {
            usernew.setChargeMode(new MobilePhoneMassageCharging());
        }
        users.add(usernew);
    }

    public void writeRecord(ArrayList<User> users, String input) {
        String[] inputs = input.split(" ");
        inputs[0] = inputs[0].substring(2);
        User callu = null, answeru = null;
        String out = inputs[0];
        String in = "";
        if (inputs.length == 6) {
            in = inputs[1];
        } else if (inputs.length == 7) {
            in = inputs[1];
        } else if (inputs.length == 8) {
            in = inputs[2];
        } else {
            in = inputs[1];
        }

        for (User i : users) {
            if (i.getNumber().equals(out)) {
                callu = i;
            }
            if (i.getNumber().equals(in)) {
                answeru = i;
            }
            if (callu != null && answeru != null) {
                break;
            }
        }

        if (input.charAt(0) == 'm') {
            MessageRecord messageRecord = new MessageRecord(input);
            if (callu != null) {
                callu.getUserRecords().addSendMessageRecords(messageRecord);
                ;
            }
            if (answeru != null) {
                callu.getUserRecords().addReceiveMessageRecords(messageRecord);
            }
        }

    }

}

abstract class CommunicationRecord {
    protected String callingNumber;
    protected String answerNumbe;

    public String getCallingNumber() {
        return callingNumber;
    }

    public void setCallingNumber(String callingNumber) {
        this.callingNumber = callingNumber;
    }

    public String getAnswerNumbe() {
        return answerNumbe;
    }

    public void setAnswerNumbe(String answerNumbe) {
        this.answerNumbe = answerNumbe;
    }

}

abstract class ChargeRule {

    abstract public double calCost(UserRecords userRecords);

}

class CallRecord extends CommunicationRecord {
    private Date startTime;
    private Date endTime;
    private String callingAddressAreaCode;
    private String answerAddressAreaCode;

    public String getCallType() {
        String type = "";
        if (callingAddressAreaCode.equals("0791")) {
            type = type.concat("1");
        } else if (callingAddressAreaCode.matches("^079[023456789]$") || callingAddressAreaCode.equals("0701")) {
            type = type.concat("2");
        } else {
            type = type.concat("3");
        }

        if (answerAddressAreaCode.equals("0791")) {
            type = type.concat("1");
        } else if (answerAddressAreaCode.matches("^079[023456789]$") || answerAddressAreaCode.equals("0701")) {
            type = type.concat("2");
        } else {
            type = type.concat("3");
        }

        return type;
    }

    public CallRecord(String[] inputs) {
        super();

        char type = inputs[0].charAt(0);

        String sd = null, st = null, ed = null, et = null;

        if (type == 't') {
            if (inputs.length == 6) {
                sd = inputs[2];
                st = inputs[3];
                ed = inputs[4];
                et = inputs[5];
                callingAddressAreaCode = inputs[0].substring(0, 4);
                answerAddressAreaCode = inputs[1].substring(0, 4);
            } else if (inputs.length == 7) {
                sd = inputs[3];
                st = inputs[4];
                ed = inputs[5];
                et = inputs[6];
                if (inputs[0].charAt(0) != '0') {
                    if (inputs[2].length() == 10) {
                        answerAddressAreaCode = inputs[2].substring(0, 3);
                    } else {
                        answerAddressAreaCode = inputs[2].substring(0, 4);
                    }
                    callingAddressAreaCode = inputs[1];
                } else {
                    if (inputs[0].length() == 10) {
                        callingAddressAreaCode = inputs[0].substring(0, 3);
                    } else {
                        callingAddressAreaCode = inputs[0].substring(0, 4);
                    }
                    answerAddressAreaCode = inputs[2];
                }
            } else if (inputs.length == 8) {
                sd = inputs[4];
                st = inputs[5];
                ed = inputs[6];
                et = inputs[7];
                callingAddressAreaCode = inputs[1];
                answerAddressAreaCode = inputs[3];
            }
        } else if (type == 'm') {

        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss", Locale.getDefault());
        try {
            startTime = simpleDateFormat.parse(sd + " " + st);
            endTime = simpleDateFormat.parse(ed + " " + et);
        } catch (ParseException e) {
        }

    }

    public CallRecord(Date startTime, Date endTime, String callingAddressAreaCode, String answerAddressAreaCode) {
        super();
        this.startTime = startTime;
        this.endTime = endTime;
        this.callingAddressAreaCode = callingAddressAreaCode;
        this.answerAddressAreaCode = answerAddressAreaCode;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public String getCallingAddressAreaCode() {
        return callingAddressAreaCode;
    }

    public void setCallingAddressAreaCode(String callingAddressAreaCode) {
        this.callingAddressAreaCode = callingAddressAreaCode;
    }

    public String getAnswerAddressAreaCode() {
        return answerAddressAreaCode;
    }

    public void setAnswerAddressAreaCode(String answerAddressAreaCode) {
        this.answerAddressAreaCode = answerAddressAreaCode;
    }
}

abstract class CallChargeRule extends ChargeRule {

}

class MobilePhoneMessageRule extends CallChargeRule {

    @Override
    public double calCost(UserRecords userRecords) {
        double sumCost = 0;
        int number = 0;
        for (MessageRecord m : userRecords.getSendMessageRecords()) {
            int length = m.getMessage().length();
            if (length <= 10) {
                number++;
            } else {
                number += length / 10;
                if (length % 10 != 0) {
                    number++;
                }
            }
        }
        if (number <= 3) {
            sumCost = number * 0.1;
        } else if (number <= 5) {
            sumCost = 0.3 + 0.2 * (number - 3);
        } else {
            sumCost = 0.7 + 0.3 * (number - 5);
        }
        return sumCost;
    }

}

class MessageRecord extends CommunicationRecord {

    private String message;

    public MessageRecord(String input) {
        super();
        this.message = input.substring(26);
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

class User {

    private UserRecords userRecords = new UserRecords();
    private double balance = 100;
    private ChargeMode chargeMode;
    private String number;

    public double calCost() {
        return chargeMode.calCost(userRecords);
    }

    public double calBalance() {
        return balance - chargeMode.getMonthlyRent() - chargeMode.calCost(userRecords);
    }

    public UserRecords getUserRecords() {
        return userRecords;
    }

    public void setUserRecords(UserRecords userRecords) {
        this.userRecords = userRecords;
    }

    public ChargeMode getChargeMode() {
        return chargeMode;
    }

    public void setChargeMode(ChargeMode chargeMode) {
        this.chargeMode = chargeMode;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

}
class Reduce{
    public double exchange(double a){
        String c=String.format("%."+2+"f",a);a= Double.parseDouble(c);
        return a;
    }
}

SourceMonitor生成的报表内容:
img
类图
img
代码分析总结:
本题只要将第六次作业的电话通信改成短信就行,而且只需要通过计算发送短信的长度来计费,比第六次作业还要简单。

第八次作业第二题

题目:
编写一个类Shop(商店)、内部类InnerCoupons(内部购物券)
编写一个类Shop(商店),该类中有一个成员内部类InnerCoupons(内部购物券),可以用于购买该商店的牛奶(假设每箱牛奶售价为50元)。要求如下:
(1)Shop类中有私有属性milkCount(牛奶的箱数,int类型)、公有的成员方法setMilkCount( )和getMilkCount( )分别用于设置和获取牛奶的箱数。
(2)成员内部类InnerCoupons,有公有属性value(面值,int类型),一个带参数的构造方法可以设定购物券的面值value,一个公有的成员方法buy( )要求输出使用了面值为多少的购物券进行支付,同时使商店牛奶的箱数减少value/50。
(3)Shop类中还有成员变量coupons50(面值为50元的内部购物券,类型为InnerCoupons)、coupons100(面值为100元的内部购物券,类型为InnerCoupons)。
(4)在Shop类的构造方法中,调用内部类InnerCoupons的带参数的构造方法分别创建上面的购物券coupons50、coupons100。

在测试类Main中,创建一个Shop类的对象myshop,从键盘输入一个整数(大于或等于3),将其设置为牛奶的箱数。假定有顾客分别使用了该商店面值为50的购物券、面值为100的购物券各消费一次,分别输出消费后商店剩下的牛奶箱数。
输入格式:
输入一个大于或等于3的整数。

输出格式:
使用了面值为50的购物券进行支付
牛奶还剩XX箱
使用了面值为100的购物券进行支付
牛奶还剩XX箱
试题分析
本题需要按照题目要求定义shop类,计算用50元券和100元券买完牛奶后店里剩余的牛奶箱数。
源代码展示:

查看代码

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Shop shop = new Shop(in.nextInt());
        shop.coupons50.buy();
        System.out.println("牛奶还剩" + shop.getMilkCount() + "箱");
        shop.coupons100.buy();
        System.out.println("牛奶还剩" + shop.getMilkCount() + "箱");
    }
}

class Shop {
    private int milkCount;
    InnerCoupons coupons50 = new InnerCoupons(50);
    InnerCoupons coupons100 = new InnerCoupons(100);
    public Shop(int milkCount) {
        super();
        this.milkCount = milkCount;
    }
    class InnerCoupons {
        public int value;
        public InnerCoupons(int value) {
            this.value = value;
        }
        public void buy() {
            System.out.println("使用了面值为" + value + "的购物券进行支付");
            milkCount = milkCount - value / 50;
        }
    }
    public int getMilkCount() {
        return milkCount;
    }
    public void setMilkCount(int milkCount) {
        this.milkCount = milkCount;
    }
}

SourceMonitor生成的报表内容:
img
类图
img
代码分析总结:
本题难度较低,不做分析

第八次作业第三题

题目:
动物发声模拟器(多态)
设计一个动物发生模拟器,用于模拟不同动物的叫声。比如狮吼、虎啸、狗旺旺、猫喵喵……。
定义抽象类Animal,包含两个抽象方法:获取动物类别getAnimalClass()、动物叫shout();
然后基于抽象类Animal定义狗类Dog、猫类Cat和山羊Goat,用getAnimalClass()方法返回不同的动物类别(比如猫,狗,山羊),用shout()方法分别输出不同的叫声(比如喵喵、汪汪、咩咩)。
最后编写AnimalShoutTest类测试,输出:
猫的叫声:喵喵
狗的叫声:汪汪
山羊的叫声:咩咩

其中,在AnimalShoutTestMain类中,用speak(Animal animal){}方法输出动物animal的叫声,在main()方法中调用speak()方法,分别输出猫、狗和山羊对象的叫声。

请在下面的【】处添加代码。

//动物发生模拟器. 请在下面的【】处添加代码。
public class AnimalShoutTest2 {
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()
【】

}

//定义抽象类Animal
【】class Animal{
【】
}
//基于Animal类,定义猫类Cat,并重写两个抽象方法
class Cat 【】{
【】
【】
}
//基于Animal类,定义狗类Dog,并重写两个抽象方法
class Dog 【】{
【】
【】
}
//基于Animal类,定义山羊类Goat,并重写两个抽象方法
class Goat 【】{
【】
【】
}
输入格式:

输出格式:
猫的叫声:喵喵
狗的叫声:汪汪
山羊的叫声:咩咩
试题分析
本题只要按照题目写出动物类和它的三个动物子类,子类中重写动物类型和动物叫声两个抽象方法
源代码展示:

查看代码

//动物发生模拟器.  请在下面的【】处添加代码。
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()
    private static void speak(Animal animal) {
        System.out.println(animal.getAnimalClass() + "的叫声:" + animal.shout());
    }
}
//定义抽象类Animal
abstract class Animal {
    abstract public String getAnimalClass();
    abstract public String shout();
}
//基于Animal类,定义猫类Cat,并重写两个抽象方法
class Cat extends Animal {
    @Override
    public String getAnimalClass() {
        return "猫";
    }
    @Override
    public String shout() {
        return "喵喵";
    }
}
//基于Animal类,定义狗类Dog,并重写两个抽象方法
class Dog extends Animal {
    @Override
    public String getAnimalClass() {
        return "狗";
    }
    @Override
    public String shout() {
        return "汪汪";
    }
}
//基于Animal类,定义山羊类Goat,并重写两个抽象方法
class Goat extends Animal {
    @Override
    public String getAnimalClass() {
        return "山羊";
    }
    @Override
    public String shout() {
        return "咩咩";
    }
}

SourceMonitor生成的报表内容:
img
类图
img
代码分析总结:
本题题目给出了主体代码,只要重写4个类就行,不做分析

3.踩坑心得:

写第六次作业第一题时忘记约分了,导致一直有几个点过不去,约分到两位小数后才通过。
img
代码太长就导致同一个代码提交后有的超时而有的通过了
img
img

4.改进建议

1.考虑到全面性,所以根据老师给出的类图写的众多类中有很多
函数其实并没有用到,如果因为运行超时而导致过不去的话可以试试删掉一些没有用到的部分。
2.判断用户开户地址时用了大量的if else,可以考虑换成switch。

5.总结

1.作业总结:电信计费的这三次作业基本上老师已经将主体放在给出的类图里了,我们只需要按照类图就能写出大部分,通过这三次作业我也学到了对类更细致的构造方法。
2.本阶段综合总结:在这三次作业期间,我学会了javafx的基本编程方法和接口的编程方法,但真正运用时总是不怎么熟练,接口的编程总是迷糊,不知道怎么接,用javaFx写图形界面时对图片的调用和移动时老是感觉很迷茫,还需要多加研究和查阅资料。
3.对课堂的建议:希望老师在讲述Java的一些设计方面和编程方面时能详细一些,有时候明明听得很懵但还是很快的略过了,容易导致之后的内容越来越难跟上进度。

标签:inputs,return,String,ArrayList,博客,void,pta,public,第三次
From: https://www.cnblogs.com/wmshyk/p/16967691.html

相关文章

  • 第三次博客
    目录第三次博客电信计费一、前言二、设计与分析1.第六次大作业电信计费系列1-座机计费思路总结改进建议2.第七次大作业电信计费系列2-手机+座机计费思路分析改进建议3.第......
  • 【12.3-12.9】博客精彩回顾
    一、优秀文章推荐1.​​LAMP平台部署及应用​​2.​​数据结构与算法__03--二叉树前序线索化与前序线索化遍历(Java语言版)​​3.​​Spring框架介绍和使用​​4.​​数据挖......
  • 第三次Blog
    一、前言  在最后三次作业主要是围绕电信计费系统的作业。从一开始的座机计费,再到手机+座机计费,最后到短信计费。至于其它的题目,C~K的班级、阅读程序,按照题目需求修改程......
  • Centos7.2 启用iptables
    一、防火墙iptables简洁介绍iptables和firewalld都是工作在用户空间、用来定义规则的工具,本身不是防火墙,他们定义的规则,可以让内核空间当中的netfilter读取,并且实现防......
  • iptables检查与删除策略
      [root@controller1~]#iptables-tnat-LPREROUTING--line-numbers#带序号查看指定表、链策略;小写字母对应表,大写字母对应链;-t指定表,-L指定链,--line-number......
  • 论文解读(PCL)《Probabilistic Contrastive Learning for Domain Adaptation》
    论文信息论文标题:ProbabilisticContrastiveLearningforDomainAdaptation论文作者:JunjieLi,YixinZhang,ZileiWang,KeyuTu论文来源:aRxiv2022论文地址:download......
  • 使用chatGPT写一篇关于在线客服系统的博客-在线客服系统:快速解决客户问题的利器
    不想写介绍文案了,让AI帮我写一篇 在线客服系统:快速解决客户问题的利器在线客服系统是一种软件系统,它能够提供即时的在线客服服务。客户可以通过网页、移动应用或其他渠......
  • 【bug】博客园小bug
    (1)新建随笔(2)win+→分屏(3)点击全屏(4)最大化窗口(5)就变成这样了(6)点击全屏即可还原......
  • 博客设置
    /*导航博客园隐藏*/#navListli:first-child{display:none;}/*隐藏广告*/#under_post_card1{display:none;}#under_post_card2{display:none;......
  • 博客主题 Lite 配置说明
    主题说明Hello,Everyone!这是一个博客园主题皮肤:Lite,基于上一个博客皮肤的又一次改造。目前基础功能和基础API开发完成。说说我开发的意图:博客园每打开一个连接,比如随笔......