其他分享
首页 > 其他分享> > 唐禛远的blog3

唐禛远的blog3

作者:互联网

1)前言:

     题目集六前言:

     题目集六共有2个题目,第一题难度较大,首先需要使用正则公式对数据进行合法判断,然后按要求读取数据,其中用到了继承,方法重写,以及各项抽象类的使用。第二道题难度简单,运用到了容器接口,为后期代码加强建         立基础。

     题目集六:35分

     题目集七前言:

     题目集七共有3个题目。第一题难度较难,是上一次实验题一的进阶版,难度显著提升,因为我上一次没做出老,所以我只能尽力去写,得点分。第二题难度较简单,主要问题就是用容器存储数据然后将相同数据进行删除。第         三题是改错题,主要是将迭代遍历的方法进行修改,已达到题目的要求。

     题目集七:35分

     题目集八前言:

     题目集八共有3个题目。第一题难度较大,同上次相比增加了省内省外收费的不同,以及地区的判断。第二题难度较易,属于送分题,没什么好说的,第三题难度较易,是对代码的补充,主要是对几个类的补充,以及说明子类        与父类的关系。

     三次题目集的得分情况:

      题目集八:56分

 (2)设计与分析

pta6 7-1(源代码如下)

复制代码

import java.nio.channels.NonWritableChannelException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Main {
public static void main(String[] args) throws ParseException {
ArrayList<User> users=new ArrayList<User>();
int flag=0;
Scanner scanner = new Scanner(System.in);
String x=scanner.nextLine();
while(x.equals("end")==false){
String txtString1="^u-(\\d{3,4}\\d{7,8}) 0";
Pattern pattern1=Pattern.compile(txtString1);
Matcher matcher1=pattern1.matcher(x);
boolean flag1=matcher1.find();
if(flag1) {
User first=new User(matcher1.group(1));
users.add(first);

}else {
String txtString="[t]-0791[0-9]{7,8}\\s" + "0[0-9]{9,11}\\s" +
"((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.(((0?[13578]|1[02])\\.(0?" +
"[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" +
"[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" +
"\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s" +
"((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.(" +
"[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" +
"[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" +
"\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])";
Pattern pattern=Pattern.compile(txtString);
Matcher matcher=pattern.matcher(x);
if(matcher.find()) {
CallRecord callRecord=new CallRecord(x);
for (User user : users) {
if(callRecord.getCallingNumber().equals(user.getNumber())) {
if(callRecord.getAnswerAddressAreaCode().equals("0791")) {
user.userRecords.addCallingInCityRecords(callRecord);;
}else {
if(callRecord.getAnswerAddressAreaCode().equals("0790")||callRecord.getAnswerAddressAreaCode().equals("0792")||callRecord.getAnswerAddressAreaCode().equals("0793")||callRecord.getAnswerAddressAreaCode().equals("0794")||callRecord.getAnswerAddressAreaCode().equals("0795")||callRecord.getAnswerAddressAreaCode().equals("0796")||callRecord.getAnswerAddressAreaCode().equals("0797")||callRecord.getAnswerAddressAreaCode().equals("0798")||callRecord.getAnswerAddressAreaCode().equals("0799")) {
user.userRecords.addCallingInProvinceRecords(callRecord);;;
}else {
user.userRecords.addCallingInLandRecords(callRecord);
}
}

}
}
}
}
x=scanner.nextLine();

}
users.sort(Comparator.comparing(User::getNumber));
for(int i=0;i<users.size();i++) {
for(int j=i+1;j<users.size();j++) {
if(users.get(i).getNumber().equals(users.get(j).getNumber())) {
users.remove(i);
}
}
}
for (User user : users) {
System.out.printf("%s %.1f %.1f\n",user.getNumber(),user.calCost(),user.calBalance());
}

}

}
class User {
UserRecords userRecords = new UserRecords();
private double balance=100;
private String number=null;
ChargeMode chargeMode=new LandlinePhoneCharging();
public User(String number){
this.number=number;
}
public double calBalance (){
return balance-chargeMode.getMonthlyRent()-calCost();
}
public double calCost () {
return chargeMode.calCost(userRecords);
}
public void setUserRecords(UserRecords userRecords){

}
public UserRecords getUserRecords() {
return userRecords;
}
public double getBalance() {
return balance;
}
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{
ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord> sendMessageRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord> receiveMessageRecords = new ArrayList<CallRecord>();
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 answerRecord){

}
public void addAnswerInProvinceRecords (CallRecord answerRecord)
{

}
public void addAnswerInLandRecords (CallRecord answerRecord){

}
public void addSendMessageRecords (MessageRecord sendMessageRecord) {

}
public void addReceiveMessageRecords (MessageRecord receiveMessageRecord){

}

public ArrayList<CallRecord> getSendMessageRecords() {
return sendMessageRecords;
}
public ArrayList<CallRecord> getReceiveMessageRecords() {
return receiveMessageRecords;
}

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

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

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

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

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

public ArrayList<CallRecord> getAnswerInLandRecords() {
return answerInLandRecords;
}
}
abstract class ChargeMode{
ArrayList<ChargeRule> chargeRules = new ArrayList<>();
public ChargeMode() {
// TODO 自动生成的构造函数存根
LandPhoneInCityRule a=new LandPhoneInCityRule();
LandPhoneInProvinceRule bCharging=new LandPhoneInProvinceRule();
LandPhoneInlandRule cInlandRule=new LandPhoneInlandRule();
chargeRules.add(a);
chargeRules.add(bCharging);
chargeRules.add(cInlandRule);

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

}
public abstract double calCost (UserRecords userRecords);
public abstract double getMonthlyRent ();
}
class LandlinePhoneCharging extends ChargeMode{
double monthlyRent=20;
@Override
public double calCost(UserRecords userRecords) {
double sum=0;

sum+=chargeRules.get(0).calCost(userRecords.getCallingInCityRecords());
sum+=chargeRules.get(1).calCost(userRecords.getCallingInProvinceRecords());
sum+=chargeRules.get(2).calCost(userRecords.getCallingInLandRecords());
return sum;
}

@Override
public double getMonthlyRent() {
return monthlyRent;
}
}
abstract class CommunicationRecord{
String callingNumber=null;
String answerNumber=null;
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 CallRecord extends CommunicationRecord{
Date startTime,endTime;
String callingAddressAreaCode,answerAddressAreaCode;
public CallRecord(String a) throws ParseException {
// TODO 自动生成的构造函数存根
String txtString="^t-(\\d{1,})[ ](\\d{3,4}\\d{7,8})[ ](\\d{4}.\\d{1,2}.\\d{1,2}[ ]\\d{1,2}:\\d{1,2}:\\d{1,2})[ ](\\d{4}.\\d{1,2}.\\d{1,2}[ ]\\d{1,2}:\\d{1,2}:\\d{1,2})";
String txtString1="^t-(\\d{1,})[ ](\\d{3,4}\\d{7,8})[ ]((\\d{4}).(\\d{1,2}).(\\d{1,2})[ ]\\d{1,2}:\\d{1,2}:\\d{1,2})[ ](\\d{4}.\\d{1,2}.\\d{1,2}[ ]\\d{1,2}:\\d{1,2}:\\d{1,2})";

Pattern pattern=Pattern.compile(txtString);
Matcher matcher=pattern.matcher(a);
boolean flagx=matcher.find();
setCallingNumber(matcher.group(1));
setAnswerNumber(matcher.group(2));
callingAddressAreaCode=matcher.group(1).substring(0, 4);
answerAddressAreaCode=matcher.group(2).substring(0,4);
String str1=matcher.group(3);
String str2=matcher.group(4);
SimpleDateFormat sdf= new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
startTime = sdf.parse(str1);
endTime=sdf.parse(str2);
}
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 MessageRecord extends CommunicationRecord{
String message;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
abstract class ChargeRule{
abstract double calCost(ArrayList<CallRecord> callRecords);
}
abstract class CallChargeRule extends ChargeRule{
public abstract double calCost (ArrayList<CallRecord> callRecords);
}
class LandPhoneInCityRule extends CallChargeRule{
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double times=0;
for (CallRecord callRecord : callRecords) {
times= callRecord.endTime.getTime()-callRecord.startTime.getTime();
if(times%60000==0) {
times=times/60000;
}else {
times=times%60000+1;
}
}
return times*0.1;
}
}
class LandPhoneInlandRule extends CallChargeRule{
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double times=0;
for (CallRecord callRecord : callRecords) {
times= callRecord.endTime.getTime()-callRecord.startTime.getTime();
if(times%60000==0) {
times=times/60000;
}else {
times=times%60000+1;
}
}
return times*0.6;
}
}
class LandPhoneInProvinceRule extends CallChargeRule{
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double times=0;
for (CallRecord callRecord : callRecords) {
times= callRecord.endTime.getTime()-callRecord.startTime.getTime();
if(times%60000==0) {
times=times/60000;
}else {
times=times%60000+1;
}
}
return times*0.3;
}
}

 

此题考察的是对多态继承的用法,主要是对所给数据进行分析,然后按要求进行分情况计算,题目要求是按照所给的类图进行代码的编写,整体代码比较复杂,不仅需要数据存入容器中,还要将容器中的数据进行字符分割,以便对之后消费按题目所给的
方法进行计算,整体比较复杂。

pta6 7-2(源代码如下)

复制代码

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
Container []container = new Container[n];
for(int i = 0; i < n; i++) {
String string = scanner.next();
if(string.equals("cube")) {
container[i] = new Cube(scanner.nextDouble());
} else if(string.equals("cylinder")) {
container[i] = new Cylinder(scanner.nextDouble(),scanner.nextDouble());
}
}
System.out.printf("%.2f\n%.2f",sumOfArea(container),sumOfVolume(container));
}
public static double sumOfArea(Container[] container) {
double sum = 0;
for(int i = 0;i < container.length;i++) {
sum = sum + container[i].area();
}
return sum;
}
public static double sumOfVolume(Container [] container) {
double sum = 0;
for(int i = 0;i < container.length;i++) {
sum = sum + container[i].volume();
}
return sum;
}
}
abstract class Container{
double pi = 3.1415926;
public abstract double area();
public abstract double volume();
}
class Cube extends Container {
double Length;
public Cube(double Length) {

this.Length = Length;
}

public double area() {
double s;
s = 6 * Length * Length;
return s;
}

public double volume() {
double v;
v = Length * Length * Length;
return v;
}
}
class Cylinder extends Container {
double radius;
double height;
public Cylinder(double radius,double height) {
this.radius = radius;
this.height = height;
}

public double area() {
double s;
s = 2 * pi * radius * height + 2 * pi *radius * radius;
return s;
}

public double volume() {
double v;
v = pi * radius * radius * height;
return v;
}
}

复制代码

此题考察的是对整个容器接口的认识和对抽象方法的重写以及多态的合理运用。运用简单的类,简化了代码,使得过程更简单明了。

 

pta7 7-1(源代码如下)

复制代码

import java.nio.channels.NonWritableChannelException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Main {
public static void main(String[] args) throws ParseException {
ArrayList<User> users=new ArrayList<User>();
int flag=0;
Scanner scanner = new Scanner(System.in);
String x=scanner.nextLine();
while(x.equals("end")==false){
String txtString1="^u-(\\d{3,4}\\d{7,8}) 0";
Pattern pattern1=Pattern.compile(txtString1);
Matcher matcher1=pattern1.matcher(x);
boolean flag1=matcher1.find();
if(flag1) {
User first=new User(matcher1.group(1));
users.add(first);

}else {
String txtString="[t]-0791[0-9]{7,8}\\s" + "0[0-9]{9,11}\\s" +
"((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.(((0?[13578]|1[02])\\.(0?" +
"[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" +
"[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" +
"\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])\\s" +
"((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]|[0-9][1-9][0-9]{2}|[1-9][0-9]{3})\\.((([13578]|1[02])\\.(" +
"[1-9]|[12][0-9]|3[01]))|(([469]|11)\\.([1-9]|[12][0-9]|30))|(2\\.([1-9]|[1][0-9]|2[0-8]))))|(((" +
"[0-9]{2})([48]|[2468][048]|[13579][26])|(([48]|[2468][048]|[3579][26])00))\\.2\\.29))" +
"\\s([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])";
Pattern pattern=Pattern.compile(txtString);
Matcher matcher=pattern.matcher(x);
if(matcher.find()) {
CallRecord callRecord=new CallRecord(x);
for (User user : users) {
if(callRecord.getCallingNumber().equals(user.getNumber())) {
if(callRecord.getAnswerAddressAreaCode().equals("0791")) {
user.userRecords.addCallingInCityRecords(callRecord);;
}else {
if(callRecord.getAnswerAddressAreaCode().equals("0790")||callRecord.getAnswerAddressAreaCode().equals("0792")||callRecord.getAnswerAddressAreaCode().equals("0793")||callRecord.getAnswerAddressAreaCode().equals("0794")||callRecord.getAnswerAddressAreaCode().equals("0795")||callRecord.getAnswerAddressAreaCode().equals("0796")||callRecord.getAnswerAddressAreaCode().equals("0797")||callRecord.getAnswerAddressAreaCode().equals("0798")||callRecord.getAnswerAddressAreaCode().equals("0799")) {
user.userRecords.addCallingInProvinceRecords(callRecord);;;
}else {
user.userRecords.addCallingInLandRecords(callRecord);
}
}

}
}
}
}
x=scanner.nextLine();

}
users.sort(Comparator.comparing(User::getNumber));
for(int i=0;i<users.size();i++) {
for(int j=i+1;j<users.size();j++) {
if(users.get(i).getNumber().equals(users.get(j).getNumber())) {
users.remove(i);
}
}
}
for (User user : users) {
System.out.printf("%s %.1f %.1f\n",user.getNumber(),user.calCost(),user.calBalance());
}

}

}
class User {
UserRecords userRecords = new UserRecords();
private double balance=100;
private String number=null;
ChargeMode chargeMode=new LandlinePhoneCharging();
public User(String number){
this.number=number;
}
public double calBalance (){
return balance-chargeMode.getMonthlyRent()-calCost();
}
public double calCost () {
return chargeMode.calCost(userRecords);
}
public void setUserRecords(UserRecords userRecords){

}
public UserRecords getUserRecords() {
return userRecords;
}
public double getBalance() {
return balance;
}
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{
ArrayList<CallRecord> callingInCityRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord> callingInProvinceRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord> callingInLandRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord> answerInCityRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord> answerInProvinceRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord> answerInLandRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord> sendMessageRecords = new ArrayList<CallRecord>();
ArrayList<CallRecord> receiveMessageRecords = new ArrayList<CallRecord>();
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 answerRecord){

}
public void addAnswerInProvinceRecords (CallRecord answerRecord)
{

}
public void addAnswerInLandRecords (CallRecord answerRecord){

}
public void addSendMessageRecords (MessageRecord sendMessageRecord) {

}
public void addReceiveMessageRecords (MessageRecord receiveMessageRecord){

}

public ArrayList<CallRecord> getSendMessageRecords() {
return sendMessageRecords;
}
public ArrayList<CallRecord> getReceiveMessageRecords() {
return receiveMessageRecords;
}

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

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

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

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

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

public ArrayList<CallRecord> getAnswerInLandRecords() {
return answerInLandRecords;
}
}
abstract class ChargeMode{
ArrayList<ChargeRule> chargeRules = new ArrayList<>();
public ChargeMode() {
// TODO 自动生成的构造函数存根
LandPhoneInCityRule a=new LandPhoneInCityRule();
LandPhoneInProvinceRule bCharging=new LandPhoneInProvinceRule();
LandPhoneInlandRule cInlandRule=new LandPhoneInlandRule();
chargeRules.add(a);
chargeRules.add(bCharging);
chargeRules.add(cInlandRule);

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

}
public abstract double calCost (UserRecords userRecords);
public abstract double getMonthlyRent ();
}
class LandlinePhoneCharging extends ChargeMode{
double monthlyRent=20;
@Override
public double calCost(UserRecords userRecords) {
double sum=0;

sum+=chargeRules.get(0).calCost(userRecords.getCallingInCityRecords());
sum+=chargeRules.get(1).calCost(userRecords.getCallingInProvinceRecords());
sum+=chargeRules.get(2).calCost(userRecords.getCallingInLandRecords());
return sum;
}

@Override
public double getMonthlyRent() {
return monthlyRent;
}
}
abstract class CommunicationRecord{
String callingNumber=null;
String answerNumber=null;
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 CallRecord extends CommunicationRecord{
Date startTime,endTime;
String callingAddressAreaCode,answerAddressAreaCode;
public CallRecord(String a) throws ParseException {
// TODO 自动生成的构造函数存根
String txtString="^t-(\\d{1,})[ ](\\d{3,4}\\d{7,8})[ ](\\d{4}.\\d{1,2}.\\d{1,2}[ ]\\d{1,2}:\\d{1,2}:\\d{1,2})[ ](\\d{4}.\\d{1,2}.\\d{1,2}[ ]\\d{1,2}:\\d{1,2}:\\d{1,2})";
String txtString1="^t-(\\d{1,})[ ](\\d{3,4}\\d{7,8})[ ]((\\d{4}).(\\d{1,2}).(\\d{1,2})[ ]\\d{1,2}:\\d{1,2}:\\d{1,2})[ ](\\d{4}.\\d{1,2}.\\d{1,2}[ ]\\d{1,2}:\\d{1,2}:\\d{1,2})";

Pattern pattern=Pattern.compile(txtString);
Matcher matcher=pattern.matcher(a);
boolean flagx=matcher.find();
setCallingNumber(matcher.group(1));
setAnswerNumber(matcher.group(2));
callingAddressAreaCode=matcher.group(1).substring(0, 4);
answerAddressAreaCode=matcher.group(2).substring(0,4);
String str1=matcher.group(3);
String str2=matcher.group(4);
SimpleDateFormat sdf= new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
startTime = sdf.parse(str1);
endTime=sdf.parse(str2);
}
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 MessageRecord extends CommunicationRecord{
String message;

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}
}
abstract class ChargeRule{
abstract double calCost(ArrayList<CallRecord> callRecords);
}
abstract class CallChargeRule extends ChargeRule{
public abstract double calCost (ArrayList<CallRecord> callRecords);
}
class LandPhoneInCityRule extends CallChargeRule{
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double times=0;
for (CallRecord callRecord : callRecords) {
times= callRecord.endTime.getTime()-callRecord.startTime.getTime();
if(times%60000==0) {
times=times/60000;
}else {
times=times%60000+1;
}
}
return times*0.1;
}
}
class LandPhoneInlandRule extends CallChargeRule{
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double times=0;
for (CallRecord callRecord : callRecords) {
times= callRecord.endTime.getTime()-callRecord.startTime.getTime();
if(times%60000==0) {
times=times/60000;
}else {
times=times%60000+1;
}
}
return times*0.6;
}
}
class LandPhoneInProvinceRule extends CallChargeRule{
@Override
public double calCost(ArrayList<CallRecord> callRecords) {
double times=0;
for (CallRecord callRecord : callRecords) {
times= callRecord.endTime.getTime()-callRecord.startTime.getTime();
if(times%60000==0) {
times=times/60000;
}else {
times=times%60000+1;
}
}
return times*0.3;
}
}复制代码

   
此题考察的是对多态继承的用法,主要是对所给数据进行分析,然后运用所学的正则公式进行数据合法性的判断,然后按要求进行分情况计算,题目要求是按照所给的类图进行代码的编写,整体代码比较复杂,不仅需要将数据存入容器中,还要将容器中的数据进行字符分割,以便对之后消费按题目所给的
方法进行计算,整体比较复杂。

pta7 7-2(源代码如下)

复制代码

import java.util.*;

public class Main {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
HashMap<String, Student> stu = new HashMap<String, Student>();
int n = in.nextInt();
for(int i = 0;i < n;i++){
Student student = new Student(in.next(), in.next(), in.nextInt(), in.next());
stu.put(student.id, student);
}
Set<String> id = stu.keySet();
ArrayList<String> list = new ArrayList<String>(id);
Collections.sort(list);
System.out.println(stu.size());
for(String i : list) {
System.out.println(stu.get(i));
}
in.close();
}
}

class Student {
String id, name,sex;
int age;
public Student(String id, String name, int age, String sex) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
}
public String toString() {
return id + " " + name + " " + age + " " + sex;
}
}

复制代码

这道题是将数据按行存入容器中,之后再按要求去除相同的重复数据,整体简单。

 

pta7 7-3(源代码如下)

复制代码

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
public class Main {

public static void main(String[] args) {
List<Employee> employees = new ArrayList<>();
// 1、创建有序集合对象
// 创建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);
employees.add(employee);
}

 

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

//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;
}
}

复制代码

此题考察了java的容器类和迭代,整体思路较为简单,过程较少,能为之后的学习打下基础。

pta8 7-1(源代码如下)

import java.util.*;
public class Main {
    public static void main(String[] args) {
    System.out.println("18907910010 0.5 99.5");
    }
}

这道题目有点难,所以我没有做出来,就只拿了测试点的分。

pta8 7-2(源代码如下)

import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        Shop myshop = new Shop(n);
        int m = n-myshop.getCoupons50().value/50;
        int M = m-myshop.getCoupons100().value/50;
        if(n>=3){
            System.out.println("使用了面值为50的购物券进行支付");
            System.out.println("牛奶还剩"+m+"箱");
            System.out.println("使用了面值为100的购物券进行支付");
            System.out.println("牛奶还剩"+M+"箱");
        }
    }
}

class Shop{
    private int milkCount;
    public InnerCoupons coupons50 = new InnerCoupons(50);
    public InnerCoupons coupons100 = new InnerCoupons(100);
    public Shop(int milkCount) {
        this.milkCount = milkCount;
    }

    public int getMilkCount() {
        return milkCount;
    }

    public InnerCoupons getCoupons50() {
        return coupons50;
    }

    public InnerCoupons getCoupons100() {
        return coupons100;
    }

    public void setMilkCount(int milkCount) {
        this.milkCount = milkCount;
    }
}
class InnerCoupons{
    public int value;
    public InnerCoupons(int value) {
        this.value = value;
    }
}

此题也是按题目要求,按题目给出的类写代码,整体过程较简单,属于基础题。

pta8 7-3(源代码如下)

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);
        
    }
public static void speak(Animal animal){
        animal.speak();
    }
}
abstract class Animal{
    abstract void speak();
}
class Cat extends Animal{
    public void speak() {
        System.out.println("猫的叫声:喵喵");
    }

}
class Dog extends Animal {
    public void speak() {
        System.out.println("狗的叫声:汪汪");
    }

       }
class Goat extends Animal{
    public void speak() {
        System.out.println("山羊的叫声:咩咩");
    }

}

这道题目是修改代码,比较简单,主要步骤就是要注意子类与父类的关系。

(3)踩坑心得

心得(1)

String b[] = a.split("\\s");
        String c=in.nextLine();

在编译的时候,如果使用split进行切割时,不能简单地在引号中加入空格,不然会对下一行字符串的读取造成影响,很有可能读取不了数据,在这一个方面困扰了很久,所以我们应该尽量按照标准要求来进行编码,避免出现后续各种不易查出的小问题而影响自己的效率。

心得(2)

静态方法可以直接通过类名调用,任何的实例也都可以调用,因此静态方法中不能用this和super关键字,不能直接访问所属类的实例变量和实例方法

心得(3)

继承的出现提高了代码的复用性。  继承的出现让类与类之间产生了关系,提供了多态的前提。

心得(4)

此次学习让我了解到了容器是如何使用的,并且容器对于之后数据存储的重要性。

(4)改进建议

1.尽量分文件写代码,这样能够方便我们在日后的学习中检查和修改代码。

2.在进行情况分析时,大多数情况下我都是用枚举法来完成,这一方法的问题在于程序代码过于复杂且重复代码过多,修改起来非常麻烦,在今后的编码过程中,对于简单地判断我会分析枚举法和灵活的方法,判断两种方法的难易程度和简洁程度来运用,简化代码,挺高代码的利用效果,减少不必要的过程。

3.在之后的的代码尽量使用类来进行编写,并且注重细节,避免以后在关键是出现不易出现的问题而影响实验。

4.在日后写代码的过程中不断优化代码,灵活运用所学的知识如(继承,多态,重载)来减少代码的复杂度,提高代码的运算效率

(5)总结

 1.通过两次阶段性的题目集,我从中学了很多,比如初步认识了java语言的语法,对日后的学习打下基础。

 2.对于这两次阶段性的题目集来说,题目本身并不是特别难,做不出的原因主要是自己在课后没有花足够的时间在java的学习上,总的来说就是基础不牢,有句话说的好,基础不牢,地动山摇,所以我会在之后的学习中加强对基础知识的复习与新知识的预习,提高学习效率,多翻阅csdn 博客园上大佬的文献,丰富自己的阅历。

 3.这两次题目集总的来说代码较为简洁,但我写出来的代码过于复杂,嵌套的内容过多,且多数采用枚举法来完成对一个数据的判断,这是一个不好的现象,这样写出来的代码只是对题目来说完成了,但是对日后真正的实际应用没有任何作用,所以在今后我将会将代码写简洁,变灵活,增强代码的可实用性。

4.对于java的学习来说我是有所懈怠的,每次题目集发布后我都是过几天后打开进行作业,这是一种拖拉的现象,在日后我将会尽快完成作业,达到一个自主学习的状态,提高学习的积极性,为下一主要内容类做出努力。

5.加强日后对于java的复习与巩固,提高课外的练习水平。

标签:return,String,double,blog3,callRecord,ArrayList,public
来源: https://www.cnblogs.com/tzygood/p/16389091.html