前三次pta

前三次pta

前言:这三次的实验还是有成长之处的,但难度也是质的飞越,我做第一次的pta是比较轻松的,但做到了第三次难度提升的非常的大,一开始无从下手,知道看了看别人的,外加自己思考慢慢的做了出来,所以说看到难题不要怕,要迎难而上。

题集一:

概述的说就是对基本功的考察,如建立scanner类对象,以及输入输出,对函数的调用,if-else的利用,do-while语句运用,题目的数量适中,但主要还是简单的原因,再多也可以写的下去。

题集二:

这次的题目比较少,只有三题,但可操作性还是比较可以的,注意考查的对字符串的理解,以及运用了提取字符和查找特定字符的函数,这些都是通过自学掌握的,题目还是比较基础的,适合用来练手。

题集三:

这次的题目是真的较为困难,对直线,点,电话费的判断理解,可谓十分透彻,对正则表达式运用需要比较熟练,通过它来判断输入的字符串的格式,需要定义数组存储数据等等。

设计与分析

 

7-1 电信计费系列3-短信计费
分数 50
作者 蔡轲
单位 南昌航空大学

实现一个简单的电信计费程序,针对手机的短信采用如下计费方式:
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。

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
import java.util.Date;

public class Main {
    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        ArrayList<User> us2 = new ArrayList<User>();
        DecimalFormat x1 = new DecimalFormat("###.0#");
        ArrayList<MessageRecord> messageRecords = new ArrayList<MessageRecord>();
        while(true)
        {
            String[] arry = null;
            String n=in.nextLine();

            if(n.equals("end")) {
                break;}         
                    arry=n.split("-| ");
                    if(arry[0].equals("u")){    
                            cellphonemessage  lip = new cellphonemessage ();//手机
                            boolean decide = true;
                            for(User user : us2) 
                            {
                                if(user.getNumber().equals(arry[1]))
                                    decide=false;
                            }
                            if(decide==true) {
                                User u = new User(lip,arry[1]);
                                us2.add(u);
                            }
                        }
                    if(arry[0].equals("m")) {
                       if(n.matches("m-1[0-9]{10}\\s1[0-9]{10}\\s([0-9]|\\w|\\s|,|\\.)+")) {//手机手机
                            MessageRecord messageRecord =new MessageRecord(); 
                            String message=n.substring(26);
                            messageRecord.setMessage(message);
                            messageRecord.setCallingNumber(arry[1]);
                            messageRecord.setAnswerNumber(arry[2]);
                            messageRecords.add(messageRecord);
                            
                        }
                    }
                                        
                }                                               

        Collections.sort(us2,new Comparator<User>() {
            public int compare(User c1,User c2){
                double x = Double.parseDouble(c1.getNumber());
                double y = Double.parseDouble(c2.getNumber());
                return (int) (x-y);
            }
            });                
        for(int i=0; i<us2.size(); i++) {
            UserRecords userRecords = new UserRecords();//用户记录
            String a=us2.get(i).getNumber();
            for(int j=0;j<messageRecords.size();j++) {
                MessageRecord b=messageRecords.get(j);
                if(a.equals(b.getCallingNumber())) {
                    userRecords.addsendmessage(b);
                }
            }
            us2.get(i).setUserRecords(userRecords);
        }
        
        for(User u : us2) {    
            double cost=Double.parseDouble(x1.format(u.calCost()));
            double balance=Double.parseDouble(x1.format(u.calBalance()));
            System.out.println(u.getNumber()+" "+cost+" "+balance);    
            
        }
        in.close();

}
}

    class CallRecord extends CommunicationRecord{
    private Date startTime;//开始时间
    private Date endTime;//结束时间
    String callnumber;
    String answernumber;
    private String callingAddressAreaCode;//拨号地点的区号
    private String 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 cellphonemessage extends ChargeMode{
    private double monthlyRent=0;//月租

    @Override
    public double calCost(UserRecords userRecords) {//话费
        double sum = 0;
        Sendmessage p=new Sendmessage();
        sum=sum+p.calCost(userRecords.getsendmessage());        
        return sum;
    }

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

}
abstract class   ChargeMode {
    private ArrayList<ChargeRule> chargeRules= new ArrayList<>();
    public ArrayList<ChargeRule> getChargeRule(){
        return chargeRules;
    }
    public  void setChargeRules(ArrayList<ChargeRule> chargeRules) {
        this.chargeRules= chargeRules;
    }
    public abstract double calCost(UserRecords userRecords);
    abstract double getMonthlyRent() ;

}
abstract class ChargeRule {

}
class CommunicationRecord {
    private String callingNumber;
    private String answerNumber;
    public String getCallingNumber() {
        return callingNumber;
    }
    public void setCallingNumber(String callingNumber) {
        this.callingNumber=callingNumber;
    }
    public String getAnswerNumber() {
        return answerNumber;
    }
    public void setAnswerNumber(String answerNumber) {
        this.answerNumber=answerNumber;
    }

}
abstract class MessageChargeRule extends ChargeRule {
    public abstract double calCost(ArrayList<MessageRecord>messageRecirds);}
class MessageRecord extends CommunicationRecord {
    String message;
    String getMessage() {
        return message;
    }
    void setMessage(String message) {
        this.message=message;
    }

}
     class Sendmessage extends  MessageChargeRule{
    
    @Override
    public double calCost(ArrayList<MessageRecord> messageRecord) {//发送短信
        double sum=0.0;
        int length;
        int i=0;
        for(MessageRecord messageRecord1:messageRecord) {
             length=messageRecord1.getMessage().length();
            if(length%10==0)i+=length/10;
            else {
                i+=(int)(length/10)+1;
        }
        }
        if(i>3)
            sum=sum+0.2*(i-3);
            if(i>5)
                sum=sum+0.3*(i-5);
            else
                sum=sum+0.1*i;
            return sum;
}
}
    
class User {
    UserRecords userRecords = new UserRecords();
    double balance = 100;
    ChargeMode chargeMode;
    String number;

    User(ChargeMode chargeMode,String number){
        this.chargeMode=chargeMode;
        this.number=number;
    }
    double calBalance() {
        double a = balance-(calCost()+chargeMode.getMonthlyRent());
        return a;
    }
    double calCost() {
        double s =chargeMode.calCost(userRecords);
        return s;
    }
    UserRecords getUserRecords() {
        return userRecords;    
    }
    void setUserRecords(UserRecords userRecords) {
        this.userRecords=userRecords;
    }
    double getBalance() {
        return balance;
    }
    ChargeMode getChargeMode() {
        return chargeMode;
    }
    void setChargeMode(ChargeMode chargeMode) {
        this.chargeMode = chargeMode;    
    }
    String getNumber() {
        return number;        
    }
    void setNumber(String number) {
        this.number = number;        
    }
}
class UserRecords {
    ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();//市内打电话
    ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();//省内打电话
    ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();//国内打电话
    ArrayList<CallRecord> ProvincecallingRecords = new ArrayList<CallRecord>();//省内打漫游
    ArrayList<CallRecord> LandcallingRecords = new ArrayList<CallRecord>();//国内打漫游
    ArrayList<MessageRecord> sendmessage = new ArrayList<MessageRecord>();//国内打漫游
    ArrayList<MessageRecord> receivemessage = new ArrayList<MessageRecord>();//国内打漫游
    ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();//市内接电话
    ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();//省内接电话
    ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();//国内接电话
    
    public ArrayList<CallRecord> getCallingInCityRecords() {
        return callingInCityRecords;
    }
    public void addCallingInCityRecords(CallRecord callRecord) {//添加市内打电话记录
        callingInCityRecords.add(callRecord);
    }
    
    public ArrayList<CallRecord> getCallingInProvinceRecords() {
        return callingInProvinceRecords;
    }
    public void addCallingInProvinceRecords(CallRecord callRecord) {//添加省内打电话记录
        callingInProvinceRecords.add(callRecord);
    }
    public ArrayList<MessageRecord>getsendmessage() {
        return sendmessage;
    }
    public void addsendmessage(MessageRecord messagerecords) {
        sendmessage.add(messagerecords);
    }
    public ArrayList<MessageRecord>getreceivemessage() {
        return receivemessage;
    }
    public void addreceivemessage(MessageRecord messagerecords) {
        receivemessage.add(messagerecords);
    }
    
    
    public ArrayList<CallRecord> getCallingInLandRecords() {
        return callingInLandRecords;
    }
    public void addCallingInLandRecords(CallRecord callRecord) {//添加国内打电话记录
        callingInLandRecords.add(callRecord);
    }
    
    public ArrayList<CallRecord> getAnswerInCityRecords() {
        return answerInCityRecords;
    }
    public void addAnswerInCityRecords(CallRecord answerRecord) {//添加市内接电话记录
        answerInCityRecords.add(answerRecord);
    }
    
    public ArrayList<CallRecord> getAnswerInProvinceRecords() {
        return answerInProvinceRecords;
    }
    public void addAnswerInProvinceRecords(CallRecord answerRecord) {//添加省内接电话记录
        answerInProvinceRecords.add(answerRecord);
    }
    
    public ArrayList<CallRecord> getAnswerInLandRecords() {
        return answerInLandRecords;
    }
    public void addAnswerInLandRecords(CallRecord answerRecord) {//添加国内接电话记录
        answerInLandRecords.add(answerRecord);
    }
    public void addProvincecallingRecords (CallRecord callRecord) {
        ProvincecallingRecords.add(callRecord);
    }
    public ArrayList<CallRecord> getProvincecallingRecords() {
        return ProvincecallingRecords;
    }
    public void addLandcallingRecords (CallRecord callRecord) {
        LandcallingRecords.add(callRecord);
    }
    public ArrayList<CallRecord> getLandcallingRecords() {
        return LandcallingRecords;
    }
    
    

}

这题综合考虑了许多,比如座机与座机,输入的格式判断,对没有开户的客户进行判断然后开户,然后最重要的是用户的数据储存,如果没有储存下来计算是没有办法进行的,对类的创建也非常重要,所幸老师提前帮我们对类以及分好了类,写起来才轻松无力。

7-1 电信计费系列1-座机计费
分数 80
作者 蔡轲
单位 南昌航空大学

实现一个简单的电信计费程序:
假设南昌市电信分公司针对市内座机用户采用的计费方式:
月租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,可根据理解自行调整:

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

public class Main {
    
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        ArrayList<User> us = new ArrayList<User>();
        DecimalFormat x1 = new DecimalFormat("###.0#");
        ArrayList<CallRecord> callRecords = new ArrayList<CallRecord>();
    
        
        while(true)
        {
            String[] arry = null;
            Date starttime=null;
            Date endtime=null;
            String n=in.nextLine();
            if(n.equals("end")) {
                break;}
            if(n.matches("u-079[\\d]{1}[\\d]{7,9} 0")||n.matches("u-0701[\\d]{7,9} 0")||n.matches("t-[\\d]{11,12}\\s[\\d]{11,12}\\s[\\d]{4}.[1-12].[1-31]\\s([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]\\s[\\d]{4}.[1-12].[1-31]\\s([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]"))
                {             
                    arry=n.split("-| ");                    
                    if(arry[0].equals("t")) {
                        CallRecord callRecord = new CallRecord();
                        String answerareaCode,callareaCode;//接听区号;拨打区号
                        callareaCode = arry[1].substring(0, 4);//赋值
                        answerareaCode = arry[2].substring(0, 4);//赋值
                        callRecord.setCallingAddressAreaCode(callareaCode);
                        callRecord.setAnswerAddressAreaCode(answerareaCode);
                        callRecord.setCallingNumber(arry[1]);
                        callRecord.setAnswerNumber(arry[2]);
                        SimpleDateFormat time = new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
                        try {
                            starttime = time.parse(arry[3]+" "+arry[4]);
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                        
                        try {
                            endtime = time.parse(arry[5]+" "+arry[6]);
                        } catch (ParseException e1) {
                            e1.printStackTrace();
                        }
                        callRecord.setStartTime(starttime);//开始时间
                        callRecord.setEndTime(endtime);//结束时间
                        callRecords.add(callRecord);//添加用户记录
                    }
                    
                    if(arry[0].equals("u")){
                        String areaCode;
                        CallRecord callRecord = new CallRecord();      
                        LandlinePhoneCharging lip = new LandlinePhoneCharging();//座机
                        boolean decide = true;
                        areaCode = arry[1].substring(0, 4);
                        callRecord.setCallingAddressAreaCode(areaCode);
                        for(User user : us) 
                        {
                            if(user.getNumber().equals(arry[1]))
                                decide=false;
                        }
                        if(decide==true) {
                            User u = new User(lip,arry[1]);
                            us.add(u);
                        }
                    }
                    
                    
                   
                }
        }
        for(int i=0;i<us.size();i++) {
            UserRecords u=new UserRecords();
            for(int j=0;j<callRecords.size();j++) {
                CallRecord a=callRecords.get(j);
                String callnumber=a.callnumber;
                if(us.get(i).number.equals(callnumber)) {
                    if(a.getAnswerAddressAreaCode().matches("0791"))u.addCallingInCityRecords(a);
                    else if(a.getAnswerAddressAreaCode().matches("0701"))u.addCallingInProvinceRecords(a);
                    else if(a.getAnswerAddressAreaCode().matches("079[0-9]"))u.addCallingInProvinceRecords(a);
                    else u.addCallingInLandRecords(a);
                }
                
            }
            us.get(i).setUserRecords(u);
        }
Collections.sort(us,new Comparator<User>() {
    @Override
    public int compare(User o1, User o2) {
        // TODO Auto-generated method stub
        double x=Double.parseDouble(o1.getNumber());
        double y=Double.parseDouble(o2.getNumber());
        return (int) (x-y);

    }
});
        for(User u : us) {
            double cost=Double.parseDouble(x1.format(u.calCost()));
            double balance=Double.parseDouble(x1.format(u.calBalance()));
            System.out.println(u.getNumber()+" "+cost+" "+balance);
        }
        in.close();
        
    }
    
}
abstract class CallChargeRule extends ChargeRule {
    public abstract double calCost(ArrayList<CallRecord>callRecords);

}
 class CallRecord extends CommunicationRecord{
    Date startTime;
    Date endTime;
    String callingAddressAreaCode;
    String answerAddressAreaCode;
    String callnumber;
    String answernumber;
    Date getStartTime() {
        return startTime;
    }
    void setStartTime(Date starttime2) {
        this.startTime=(Date) starttime2;
    }
    Date getEndTIme() {
        return endTime;
    }
    void setEndTime(Date endtime2) {
        this.endTime=(Date) endtime2;
    }
    String getCallingAddressAreaCode() {
        return callingAddressAreaCode;
    }
    void setCallingAddressAreaCode(String callingAddressAreaCode) {
        this.callingAddressAreaCode=callingAddressAreaCode;
    }
    String getAnswerAddressAreaCode() {
        return answerAddressAreaCode;
    }
    void setAnswerAddressAreaCode(String answerAddressAreaCode) {
        this.answerAddressAreaCode=answerAddressAreaCode;
    }
    public void setCallingNumber(String string) {
        this.callnumber=string;
        
    }
    public void setAnswerNumber(String string) {
        this.answernumber=string;
    }

    
    
}
abstract class   ChargeMode {
    private ArrayList<ChargeRule> chargeRules= new ArrayList<>();
    public ArrayList<ChargeRule> getChargeRule(){
        return chargeRules;
    }
    public  void setChargeRules(ArrayList<ChargeRule> chargeRules) {
        this.chargeRules= chargeRules;
    }
    public abstract double calCost(UserRecords userRecords);
    abstract double getMonthlyRent() ;

}
abstract class ChargeRule {

}
class CommunicationRecord {
    private String callingNumber;
    private String answerNumber;
    public String getCallingNumber() {
        return callingNumber;
    }
    public void setCallingNumber(String callingNumber) {
        this.callingNumber=callingNumber;
    }
    public String getAnswerNumber() {
        return answerNumber;
    }
    public void setAnswerNumber(String answerNumber) {
        this.answerNumber=answerNumber;
    }

}


class LandlinePhoneCharging extends ChargeMode   {
    double monthlyRent = 20;
    public double calCost(UserRecords userRecords) {
        double sum = 0.0;
        LandPhoneInlandRule p1 = new LandPhoneInlandRule();
        LandPhonelnCityRule p2 = new LandPhonelnCityRule();
        LandPhonelnProvinceRule p3 = new LandPhonelnProvinceRule();
        double sum1= sum+p1.calCost(userRecords.getCallingInLandRecords());
        double sum2=sum+p2.calCost(userRecords.getCallingInCityRecords());
        double sum3=sum+p3.calCost(userRecords.getCallingInProvinceRecords());
        return sum1+sum2+sum3;

        
    }
    public double getMonthlyRent() {
        return monthlyRent;
    }

}
class LandPhoneInlandRule extends CallChargeRule{

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
            double sum=0.0;
            for(int i=0;i<callRecords.size();i++)
            {
                double s = callRecords.get(i).getEndTIme().getTime()-callRecords.get(i).getStartTime().getTime();
                s=s/1000.0/60;//分钟
                if(s%1==0) {
                    sum+=s*0.6;
                }
                else {
                    double a=s%1;
                    s=s-a+1;
                    sum+=s*0.6;
                }
                
            }
            return sum;
        }

    }

 class LandPhonelnCityRule extends CallChargeRule {

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double sum=0;
        for(int i=0;i<callRecords.size();i++)
        {
            double s = callRecords.get(i).getEndTIme().getTime()-callRecords.get(i).getStartTime().getTime();
            
            s=s/1000.0/60;//分钟
            if(s%1==0) {
                sum+=s*0.1;
            }
            else {
                double a=s%1;
                s=s-a+1;
                sum+=s*0.1;
            }
        }
        return sum;
    }

}
class LandPhonelnProvinceRule extends CallChargeRule{

    @Override
    public double calCost(ArrayList<CallRecord> callRecords) {
        double sum=0;
        for(int i=0;i<callRecords.size();i++)
        {
            double s = callRecords.get(i).getEndTIme().getTime()-callRecords.get(i).getStartTime().getTime();
            s=s/1000.0/60;//分钟
            if(s%1==0) {
                sum+=s*0.3;
            }
            else {
                double a=s%1;
                s=s-a+1;
                sum+=s*0.3;
            }
        }
        return sum;

}
}





class MessageRecord extends CommunicationRecord {
    String message;
    String getMessage() {
        return message;
    }
    void setMessage(String message) {
        this.message=message;
    }

}
class User {
    UserRecords userRecords = new UserRecords();
    double balance = 100;
    ChargeMode chargeMode;
    String number;

    User(ChargeMode chargeMode,String number){
        this.chargeMode=chargeMode;
        this.number=number;
    }
    double calBalance() {
        double a = balance-(calCost()+chargeMode.getMonthlyRent());
        return a;
    }
    double calCost() {
        double s =chargeMode.calCost(userRecords);
        return s;
    }
    UserRecords getUserRecords() {
        return userRecords;    
    }
    void setUserRecords(UserRecords userRecords) {
        this.userRecords=userRecords;
    }
    double getBalance() {
        return balance;
    }
    ChargeMode getChargeMode() {
        return chargeMode;
    }
    void setChargeMode(ChargeMode chargeMode) {
        this.chargeMode = chargeMode;    
    }
    String getNumber() {
        return number;        
    }
    void setNumber(String number) {
        this.number = number;        
    }
}
class UserRecords {
    ArrayList<CallRecord>callinglnCityRecords = new ArrayList<CallRecord>();
    ArrayList<CallRecord>callinglnProvinceRecords = new ArrayList<CallRecord>();
    ArrayList<CallRecord>callinglnLandRecords = new ArrayList<CallRecord>();
    ArrayList<CallRecord>answerlnCityRecords = new ArrayList<CallRecord>();
    ArrayList<CallRecord>answerlnProvinceRecords = new ArrayList<CallRecord>();
    ArrayList<CallRecord>answerlnLandRecords = new ArrayList<CallRecord>();
    ArrayList<MessageRecord>sendMessageRecords = new ArrayList<MessageRecord>();
    ArrayList<MessageRecord>receiveMessageRecords = new ArrayList<MessageRecord>();
    public void addCallingInCityRecords (CallRecord callRecord) {
        callinglnCityRecords.add(callRecord);
    }
    public void addCallingInProvinceRecords (CallRecord callRecord) {
        callinglnProvinceRecords.add(callRecord);
    }
    public void addCallingInLandRecords (CallRecord callRecord) {
        callinglnLandRecords.add(callRecord);
    }
    public void addAnswerInCityRecords (CallRecord answerRecord) {
        answerlnCityRecords.add(answerRecord);
    }
    public void addAnswerInProvinceRecords (CallRecord answerRecord) {
        answerlnProvinceRecords.add(answerRecord);
    }
    public void addAnswerInLandRecords (CallRecord answerRecord) {
        answerlnLandRecords.add(answerRecord);
    }

    public void addSendMessageRecords (MessageRecord sendMessageRecord) {
        sendMessageRecords.add(sendMessageRecord);
    }
    public void addReceiveMessageRecords (MessageRecord receiveMessageRecord) {
        receiveMessageRecords.add(receiveMessageRecord);
    }
    public ArrayList<MessageRecord> getSendMessageRecords (){
        return this.sendMessageRecords;
    }
    public ArrayList<MessageRecord> getReceiveMessageRecords (){
        return this.receiveMessageRecords;
    }
    public ArrayList<CallRecord> getCallingInCityRecords (){
        return this.callinglnCityRecords;
    }
    public ArrayList<CallRecord> getCallingInProvinceRecords (){
        return this.callinglnProvinceRecords;
    }
    public ArrayList<CallRecord> getCallingInLandRecords (){
        return this.callinglnLandRecords;
    }
    public ArrayList<CallRecord> getAnswerInCityRecords (){
        return this.answerlnCityRecords;
    }
    public ArrayList<CallRecord> getAnswerInProvinceRecords (){
        return this.callinglnProvinceRecords;
    }
    public ArrayList<CallRecord> getAnswerInLandRecords (){
        return this.callinglnLandRecords;
    }


}

手机对手机用户对打,座机对手机,座机对座机,种类挺多的,但思路比较清晰,和上一次比差不多,增加几个类,仿照座机就行了

7-2 多态测试
分数 20
作者 董卫萍
单位 绍兴文理学院元培学院

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

  1. 定义接口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[]);
    其中两个静态方法分别计算返回容器数组中所有对象的面积之和、周长之和;
  2. 定义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) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        int n =in.nextInt();
        Container c[]=new Container[n];
        String name = null;
        for(int i=0;i<n;i++){
            name=in.next();
            if(name.equals("cube")){
                c[i]=new Cube(in.nextDouble());
            }
            else if(name.equals("cylinder")) {
                c[i]=new Cylinder(in.nextDouble(), in.nextDouble());
            }
            
        }
        System.out.println(String.format("%.2f", Container.sumofArea(c)));
        System.out.println(String.format("%.2f", Container.sumofVolume(c)));
                
    }

}
interface Container {
    public static final double pi=3.1415926;
    public abstract double area();
    public abstract double volume();
    
    static double sumofArea(Container c[]) {
        double sum =0 ;
        for(int i=0;i<c.length;i++) {
            sum=sum+c[i].area();    
        }
        return sum;    
        
    }
    static double sumofVolume(Container c[]) {
        double sum=0;
        for(int i=0;i<c.length;i++) {
            sum=sum+c[i].volume();            
        }
        return sum;
    }
}
class Cube implements Container {
    double a;
    Cube(double a){
        
        this.a=a;
        
    }

    @Override
    public double area() {
        return a*a*6;
        // TODO Auto-generated method stub
        


    }

    @Override
    public double volume() {
        // TODO Auto-generated method stub
        return a*a*a;
    }


    

}
    class Cylinder implements Container{
    double R;
    double h;
    Cylinder(double R,double h){
        
        this.R=R;
        this.h=h;
        
    }
    @Override
    public double area() {
        // TODO Auto-generated method stub
        return 2*(pi*R*R)+(2*pi*R*h);
    }
    @Override
    public double volume() {
        // TODO Auto-generated method stub
        return pi*R*R*h;
    }

}

容器老师已经讲过了,此题也比较基础,随便写一下就可以,注意会熟练运用arrylist

7-2 sdut-Collection-sort--C~K的班级(II)
分数 10
作者 周雪芹
单位 山东理工大学

经过不懈的努力,C~K终于当上了班主任。

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

输入格式:

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

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

输出格式:

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

接下来的n行,输出每一个同学的信息,输出按照学号从小到大的顺序。

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;

public class Main {
    String name;
    String num;
    int nl;
    char sex;
          Main(String num,String name,int nl,char sex) {
        this.name=name;
        this.num=num;
        this.nl=nl;
        this.sex=sex;}
        public String toString() {
            return num + " " + name + " " + nl + " " + sex;
        }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
Scanner in=new Scanner(System.in);
HashMap<String, Main> o = new HashMap<String, Main>();
int n=in.nextInt();
for(int i=0;i<n;i++) {
    Main s=new Main(in.next(),in.next(),in.nextInt(),in.next().charAt(0));
    o.put(s.num, s);}
Set<String> c=o.keySet();
ArrayList<String>list=new ArrayList<String>(c);
Collections.sort(list);
System.out.println(list.size());
for(String i:list) {
        System.out.println(o.get(i));}
        in.close();
}


        
    }
    


    

    

老师上课讲过了HashMap,仿照之前上课讲过的就可以了,属于基础题。

作者 肖斌
单位 西南石油大学
功能需求:
      使用集合存储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
 

输出样例:

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

import java.util.ArrayList;
import java.util.Collection;
// 1、导入相关包
import java.util.Iterator;
import java.util.Scanner;

//定义员工类
  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;
    }
}

//主函数
  class Main {

    public static void Main(String[] args) {
                // 1、创建有序集合对象
                Collection<Employee> p = new ArrayList<Employee>();
      // 创建3个员工元素对象\
                Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 3; i++) {
            String employeeName = sc.next();
            int employeeAge = sc.nextInt();        
            Employee employee = new Employee(employeeName, employeeAge);
            p.add(employee);
        }            
                                                
                // 2、创建迭代器遍历集合
                Iterator<Employee> it=p.iterator();                
                //3、遍历
                while (it.hasNext()) {                    
                    //4、集合中对象未知,向下转型                    
                    Employee e =  it.next();                    
                    System.out.println(e.getName() + "---" + e.getAge());
                }
                
    }
    

}

按照题目意思解题即可。

总结:

 

    每一次花在pta的时间是非常多的,但有多少时间是真正用到实处的,所以还得加强的Java的学习,让自己更加了解这门语言,从而熟练这门语言,而不是一问三不知,见到pta就迷就呆了就无从下手。

 

2.通过三次作业的反复思考,从编辑到编译再到调试,我对Java这门课程有了较为全面的认识,尤其对面向对象编程思想有了一些感觉。首先,Java它特点很鲜明,类,对象,方法,就这些东西,具体功能实现过程中大量的思路和方法都是和C语言一样的,这就给我们学习Java提供了不少的便利。学习和做题的过程中会遇到很多的问题,这个时候就很考验我们软件专业学生的自我思考和学习的能力,现在大一还在慢慢适应,相信以后对这种模式会越来越顺手。课程的话建议换一个教师,这样老师可以用话筒而不是扩音器,教室也不需要这么大,穿戴鞋套很不方便,毕竟我们都自带电脑,不是去机房上课。

 

 

posted @ 2022-06-09 18:37  眼睛大的像铜铃  阅读(176)  评论(0)    收藏  举报