对后几次实验与PTA电信计费题目及期末的总结
作者:互联网
前言:
在长达一个学期的JAVA课程学习下,在老师的指导下,我对JAVA认知得到了突破性的认知,对JAVA的掌握也得到了较大的提升。这个学期我们总共做了五次实验,八次PTA大作业还有一次关于JAVAFX的客房预订设计,这些作业都对在相应学习时期的我们的JAVA学习提供了帮助,不仅仅是对知识的巩固,更多的是对JAVA的实用场景的一个提前的体验,加强了我们对于JAVA编程的实际运用,并且让我们见识了实际中的问题在JAVA中如何解决,总的来说意义还是十分大的。现在我就对后几次实验及PTA中电信计费问题做出一个总结。
所学内容:
①:继承与多态(父类与子类)
②:抽象类的声明与使用
③:接口类的声明与使用
④:JAVA编程的各种方法(Hashset,正则表达式,Array List等)
⑤:JAVAFX
对PTA中电信计费的总结
实验目的:
实现一个简单的电信计费程序,针对手机的短信采用如下计费方式:
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。
实验的类图设计:
运行结果:
输入样例:
在这里给出一组输入。例如:
u-18907910010 3
m-18907910010 13305862264 aaaaaaaaaaaaaaaaaaaaaaa
end
输出样例:
在这里给出相应的输出。例如:
18907910010 0.3 99.7
### 输入样例1:
在这里给出一组输入。例如:
u-18907910010 3
m-18907910010 13305862264 aaaaaaaaaaaa
m-18907910010 13305862264 aaaaaaa.
m-18907910010 13305862264 bb,bbbb
end
输出样例1:
在这里给出相应的输出。例如:
18907910010 0.5 99.5
源代码如下:
import java.util.Date; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Scanner; import java.util.regex.Pattern; class User { UserRecords userRecords =new UserRecords(); double balance =100; int chargeMode; String number; public double calBalance() { return balance; } public double calCost() { return 0; } public UserRecords getUserRecords() { return userRecords; } public void setUserRecords(UserRecords userRecords) { this.userRecords=userRecords; } public double getBalance() { return balance; } public int getChargeMode() { return chargeMode; } public void setChargeMode(int 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<MessageRecord> sendMessageRecord =new ArrayList<MessageRecord>(); ArrayList<MessageRecord> receiveMessageRecord =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 answerRecord) { answerInCityRecords.add(answerRecord); } public void addAnswerInProvinceRecords(CallRecord answerRecord) { answerInProvinceRecords.add(answerRecord); } public void addSendMessageRecord(MessageRecord messageRecord) { sendMessageRecord.add(messageRecord); } public void addReceiveMessageRecord(MessageRecord messageRecord) { receiveMessageRecord.add(messageRecord); } 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; } public ArrayList<MessageRecord> getSendMessageRecord(){ return sendMessageRecord; } public ArrayList<MessageRecord> getReceiveMessageRecord(){ return receiveMessageRecord; } } class CallRecord { Date startTime; Date endTime; String callingAddressAreaCode; String answerAddressAreaCode; public Date getStartTime() { return startTime; } public Date getEndTime() { return endTime; } public void setStartTime(Date time) { startTime=time; } public void setEndTime(Date time) { startTime=time; } public String getCallingAddressAreaCode() { return callingAddressAreaCode; } public String getAnswerAddressAreaCode() { return answerAddressAreaCode; } public void setCallingAddressAreaCode(String code) { callingAddressAreaCode=code; } public void setAnswerAddressAreaCode(String code) { answerAddressAreaCode=code; } } abstract class ChargeMode { ArrayList<ChargeRule> chargeRules =new ArrayList<>(); public ArrayList<ChargeRule> getChargeRule() { return chargeRules; } public void setChargeRule(ArrayList<ChargeRule> chargeRules) { this.chargeRules=chargeRules; } public abstract double calCost(UserRecords userRecords) ; public abstract double getMonthlyRent(); } abstract class CommunicationRecord { String callingNumber; String answerNumber; public String getCallingNumber() { return callingNumber; } public String getAnswerNumber() { return answerNumber; } public void setCallingNumber(String number) { callingNumber=number; } public void setAnswerNumber(String number) { answerNumber=number; } } class MessageRecord extends CommunicationRecord{ String message; public String getMessage() { return message; } public void setMassage(String mess) { message=mess; } } abstract class CallChargeRule extends ChargeRule{ public double calCost(ArrayList<CallRecord> records) { return 0; } } abstract class ChargeRule { } public class Main { public static void main(String[] args){ // TODO Auto-generated method stub String input=" "; String data[]; String num[]=new String[2]; ArrayList<User> users=new ArrayList<User>(); int i=0; int e=0; int m=0; int a=0; double fee=0; String format1 = "[u]-[0-9]{11}\\s"+"3"; String format = "[m]-1[0-9]{10}\\s"+"1[0-9]{10}\\s"+"([0-9]|[a-z]|[A-Z]|,|\\.|\\s)*"; Scanner in =new Scanner(System.in); input=in.nextLine(); while(!Pattern.matches("end",input)) { data=input.split(" "); if(!Pattern.matches(format1,input)&&!Pattern.matches(format,input)){ input=in.nextLine(); continue; } num=data[0].split("-"); if(Pattern.matches(num[0],"u")) { User user=new User(); user.setNumber(num[1]); for(m=0;m<i;m++) { if(i!=0&&Pattern.matches(num[1], users.get(m).getNumber())) { e=1; break; } } if(e!=1){ users.add(user); i++; e=0; } } else{ for(m=0;m<i;m++) { if(Pattern.matches(num[1], users.get(m).getNumber())) { e=1; break; } } if(e==0||!Pattern.matches(format,input)) { input=in.nextLine(); continue; } e=0; a=input.substring(26,input.length()).length()/10; if(input.substring(26,input.length()).length()%10!=0) { a++; } users.get(m).setChargeMode(a+users.get(m).getChargeMode()); } input=in.nextLine(); } User user=new User(); for(int n=0;n<users.size()-1;n++) { for(int j=0;j<users.size()-1-n;j++) { if(Double.parseDouble(users.get(j).getNumber())>Double.parseDouble(users.get(j+1).getNumber())) { user=users.get(j); users.set(j, users.get(j+1)); users.set(j+1, user); } } } for(m=0;m<i;m++) { if(users.get(m).getChargeMode()<3) { fee=users.get(m).getChargeMode()*0.1; } else if(users.get(m).getChargeMode()<5) { fee=0.3+0.2*(users.get(m).getChargeMode()-3); } else { fee=0.7+0.3*(users.get(m).getChargeMode()-5); } System.out.println(String.format("%s %.1f %.1f", users.get(m).number,fee,users.get(m).balance-fee)); } } }
实验的缺陷与改进:
①:对于多态的使用并未熟练掌握,对各个类的返回有点小问题,在通过多次调试后了解到了原因并加以了改进
②:对各种的抽象类的用因并不了解,导致抽象类无法实现功能,在多次调试后也无果,导致最后的抽象类也只是个空壳,并未起到相应的作用
③:对面向对象的设计并未完善,导致出了交互感不是很好,输出不是十分可观,在经过多次的调试后改进了稍稍微一点点。
对后几次实验的总结:
农夫过河(JAVAFX的使用):
实验目的:
1、了解JavaFX的基本原理和基本组件;
2、能够熟练应用JavaFX组件进行界面设计;
3、掌握Java的事件驱动机制、lambda表达式;
4、掌握shape形状类的使用。
实验要求:
1、 根据实验步骤中提出的要求,使用eclipse等开发工具编写相应的Java程序;
2、 编译、运行自己所编写的Java程序;
3、 根据编译与运行过程中所获得的错误信息修改程序直至获得正确的结果;
记录实验中遇到的各类问题并以及解决办法
实验结果:
界面图:
胜利图:
失败图:
源代码:
package jxceshi; import javafx.application.Application; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.GridPane; import javafx.stage.Stage; import java.util.ArrayList; import java.util.HashSet; public class Main extends Application { GridPane pane = new GridPane(); Game user = new Game(); @Override public void start(Stage stage) { pane.setAlignment(Pos.CENTER); pane.setPadding(new Insets(5,2,2,2)); pane.setHgap(3.5); pane.setVgap(3.5); //加入图片效果 Image image = new Image("file:F:\\图片\\河.jpg"); ImageView imageView = new ImageView(); imageView.setImage(image); imageView.setFitHeight(410.5); imageView.setFitWidth(110); pane.add(imageView,1,0,1,4); //加入按键 Button bt = new Button("农夫独自过河"); bt.setMinSize(110,30); ButtonEvents handle = new ButtonEvents(1); bt.setOnAction(handle); pane.add(bt,0,4); bt = new Button("农夫带狼过河"); bt.setMinSize(110,30); handle = new ButtonEvents(2); bt.setOnAction(handle); pane.add(bt,1,4); bt = new Button("农夫带羊过河"); bt.setMinSize(110,30); handle = new ButtonEvents(3); bt.setOnAction(handle); pane.add(bt,0,5); bt = new Button("农夫带菜过河"); bt.setMinSize(110,30); handle = new ButtonEvents(4); bt.setOnAction(handle); pane.add(bt,1,5); /*bt = new Button("退出游戏"); bt.setMinSize(110,63.5); handle = new ButtonEvents(0); bt.setOnAction(handle); pane.add(bt,2,4,1,2);*/ pane=user.play(pane,-1); Scene scene = new Scene(pane); stage.setTitle("农夫过河"); stage.setScene(scene); stage.show(); } public static void main(String[] args) { Application.launch(args); } class ButtonEvents implements EventHandler<ActionEvent> { int flag; ButtonEvents(int i) { this.flag=i; } @Override public void handle(ActionEvent event) { if(flag==0) System.exit(0); else pane=user.play(pane,flag); } } } abstract class MaterialObject { String type; String place; boolean isExit; MaterialObject() { this.place = "Left"; isExit = true; } void diedOut() { this.isExit=false; } boolean isExit() { return this.isExit; } void showStatus(GridPane pane) { Image image; ImageView imageView; switch(this.type) { case "Farmer": if(this.place.equals("Right")) { image = new Image("file:F:\\空.jpg"); imageView = new ImageView(); setSize(imageView,image); pane.add(imageView,0,0); image = new Image("file:F:\\图片\\农夫.jpg"); imageView = new ImageView(); setSize(imageView,image); pane.add(imageView,2,0); } else { image = new Image("file:F:\\空.jpg"); imageView = new ImageView(); setSize(imageView,image); pane.add(imageView,2,0); image = new Image("file:F:\\图片\\农夫.jpg"); imageView = new ImageView(); setSize(imageView,image); pane.add(imageView,0,0); } break; case "Wolf": if(this.place.equals("Right")) { image = new Image("file:F:\\空.jpg"); imageView = new ImageView(); setSize(imageView,image); pane.add(imageView,0,2); image = new Image("file:F:\\图片\\狼.jpg"); imageView = new ImageView(); setSize(imageView,image); pane.add(imageView,2,2); } else { image = new Image("file:F:\\空.jpg"); imageView = new ImageView(); setSize(imageView,image); pane.add(imageView,2,2); image = new Image("file:F:\\图片\\狼.jpg"); imageView = new ImageView(); setSize(imageView,image); pane.add(imageView,0,2); } break; case "Sheep": if(this.place.equals("Right")) { image = new Image("file:F:\\空.jpg"); imageView = new ImageView(); setSize(imageView,image); pane.add(imageView,0,1); image = new Image("file:F:\\图片\\羊.jpg"); imageView = new ImageView(); setSize(imageView,image); pane.add(imageView,2,1); } else { image = new Image("file:F:\\空.jpg"); imageView = new ImageView(); setSize(imageView,image); pane.add(imageView,2,1); image = new Image("file:F:\\图片\\羊.jpg"); imageView = new ImageView(); setSize(imageView,image); pane.add(imageView,0,1); } break; case "Cabbage": if(this.place.equals("Right")) { image = new Image("file:F:\\空.jpg"); imageView = new ImageView(); setSize(imageView,image); pane.add(imageView,0,3); image = new Image("file:F:\\图片\\白菜.jpg"); imageView = new ImageView(); setSize(imageView,image); pane.add(imageView,2,3); } else { image = new Image("file:F:\\空.jpg"); imageView = new ImageView(); setSize(imageView,image); pane.add(imageView,2,3); image = new Image("file:F:\\图片\\白菜.jpg"); imageView = new ImageView(); setSize(imageView,image); pane.add(imageView,0,3); } break; } } void setSize(ImageView imageView,Image image) { imageView.setImage(image); imageView.setFitHeight(100); imageView.setFitWidth(110); } } class Animal extends MaterialObject { HashSet<String> recipe = new HashSet<String>(); boolean eat(MaterialObject object,MaterialObject farmer) { if(canBeEat(object,farmer)) { object.diedOut(); return true; } else return false; } void addedToRecipe(MaterialObject object) { this.recipe.add(object.type); } boolean isFood(MaterialObject object) { if(recipe.contains(object.type)) return true; else return false; } boolean canBeEat(MaterialObject object,MaterialObject farmer) { if(isFood(object)&&object.place.equals(this.place)&&!farmer.place.equals(this.place)) return true; else return false; } } class Person extends MaterialObject { } class Plant extends MaterialObject { } abstract class AbstractTransport { String place; int capacity; AbstractTransport() { this.place="Left"; this.capacity=2; } ArrayList<MaterialObject> goodses = new ArrayList<MaterialObject>(); void moveTo() { if(this.place.equals("Left")) { this.place=""; this.place="Right"; } else { this.place=""; this.place="Left"; } } } class Boat extends AbstractTransport { void crossRiver() { int i; for(i=0;i<goodses.size();i++) { if(goodses.get(i).place.equals("Left")) { goodses.get(i).place=""; goodses.get(i).place="Right"; } else { goodses.get(i).place=""; goodses.get(i).place="Left"; } } moveTo(); } void board(MaterialObject object) { goodses.add(object); capacity--; } void disembark() { goodses.clear();; capacity=2; } } abstract class AbstractGame { GameOverRule gameOverRule=new GameOverRule(); GameSuccessRule gameSuccessRule=new GameSuccessRule(); GameData gameData=new GameData(); abstract GridPane play(GridPane pane,int choice); } class Game extends AbstractGame { GameUI UI = new GameUI(); CrossRiverRule rule = new CrossRiverRule(); int over=0; GridPane play(GridPane pane,int choice) { switch(choice) { case 1: gameData.boat.board(gameData.farmer); gameData.boat.crossRiver(); gameData.boat.disembark(); break; case 2: if(rule.hasCross(gameData.wolf, gameData.boat)) { gameData.boat.board(gameData.farmer); gameData.boat.board(gameData.wolf); gameData.boat.crossRiver(); gameData.boat.disembark(); } break; case 3: if(rule.hasCross(gameData.sheep, gameData.boat)) { gameData.boat.board(gameData.farmer); gameData.boat.board(gameData.sheep); gameData.boat.crossRiver(); gameData.boat.disembark(); } break; case 4: if(rule.hasCross(gameData.cabbage, gameData.boat)) { gameData.boat.board(gameData.farmer); gameData.boat.board(gameData.cabbage); gameData.boat.crossRiver(); gameData.boat.disembark(); } break; case 0: over=1; break; } pane=UI.showStatus(gameData,pane); Image image; ImageView imageView; if(over==1) { image = new Image("file:F:\\图片\\输.jpg"); imageView = new ImageView(); imageView.setImage(image); imageView.setFitHeight(410.5); imageView.setFitWidth(337); pane.add(imageView,0,0,3,4); } if(gameOverRule.judge(gameData)) { pane.getChildren().clear(); if(gameSuccessRule.judge(gameData)) image = new Image("file:F:\\图片\\胜利.jpg"); else image = new Image("file:F:\\图片\\输.jpg"); imageView = new ImageView(); imageView.setImage(image); imageView.setFitHeight(417.5); imageView.setFitWidth(337); pane.add(imageView,0,0,3,4); } return pane; } } class GameUI { GridPane showStatus(GameData gameData,GridPane pane) { gameData.farmer.showStatus(pane); gameData.wolf.showStatus(pane); gameData.sheep.showStatus(pane); gameData.cabbage.showStatus(pane); return pane; } } class GameData { MaterialObject farmer = new Person(); Animal wolf = new Animal(); Animal sheep = new Animal(); MaterialObject cabbage = new Plant(); Boat boat = new Boat(); public GameData() { farmer.type="Farmer"; wolf.type="Wolf"; sheep.type="Sheep"; cabbage.type="Cabbage"; wolf.addedToRecipe(sheep); sheep.addedToRecipe(cabbage); } } abstract class AbstractRule { abstract boolean judge(GameData gameData); } class CrossRiverRule extends AbstractRule { boolean hasCross(MaterialObject object,Boat boat) { if(!object.place.equals(boat.place)) return false; else return true; } @Override boolean judge(GameData gameData) { if(gameData.farmer.place.equals("Right")&&gameData.wolf.place.equals("Right")&&gameData.sheep.place.equals("Right")&&gameData.cabbage.place.equals("Right")) return true; else return false; } } class ObjectExitRule extends AbstractRule { @Override boolean judge(GameData gameData) { if(gameData.wolf.eat(gameData.sheep, gameData.farmer)||gameData.sheep.eat(gameData.cabbage, gameData.farmer)) return false; else return true; } } class GameOverRule extends AbstractRule { GameSuccessRule win = new GameSuccessRule(); ObjectExitRule live = new ObjectExitRule(); @Override boolean judge(GameData gameData) { if(win.judge(gameData)||!live.judge(gameData)) return true; else return false; } } class GameSuccessRule extends AbstractRule { CrossRiverRule cross = new CrossRiverRule(); ObjectExitRule live = new ObjectExitRule(); @Override boolean judge(GameData gameData) { if (cross.judge(gameData) && live.judge(gameData)) return true; else return false; } }
总结:
这个学期的JAVA编程学期收获颇多,面向对面的设计能力得到了巨大的提升,这次的学期对以后大二大三的的学习肯定能提供巨大的帮助,但在某些方面还需改进,如多态、抽象类、接口方面还需要进一步的深入学习,总的概括如下:
1:学到了什么:
①:可以根据需求分析来解决问题。
②:对面向对象的能力得到了提升。
③:能通过类间关系来降低代码的耦合性。
④:抽象类与堕胎方法的使用。
⑤:对书上后几个章节得到了学习,了解到了 各种在实际的JAVA编程方法(如JAVAFX、正则表达式,各种方法(如:Hashset、Array list等等))
2:需要进一步改进的地方:
①:自主学习的能力,由于这门课程采用的是线上线下相结合的方式,通过老师指定
的方式或其他方式自学是很重要的。
②:编码时的调试能力,在pta作业的完成中,错误无可避免,但是要学会自己通过调
试来发现问题所在并解决它。
③:对简便的追求要给与肯定,越复杂的题目越要追求简单化,从而获得更加好的交互体验,让程序更加 有可用性。
建议与意见:
无。
标签:对后,image,PTA,ArrayList,pane,imageView,计费,new,public 来源: https://www.cnblogs.com/iuuuu/p/16389253.html