PTA题目集7~9总结性报告
前言
这三次题目集的题量相对于前面几次的题目集,题目量少了很多,题目集7只有两道题,而题目8和9都只有一道,尽管题目量少了许多,但是每一道题,都是一道大题,难度方面也比以往的题目要难上许多,算是对前面所学知识和所写题目的总结,所涉及包含的内容还是比较多的。其中题目集7中的两道题属于递进关系,有很大的关联,所以如果连第一道题7-1都没做好的话,那么想要完成题目7-2是很困难的。而后面的两道题目集,题目集8和题目集9都是进行设计一个ATM机,总的来说题目集9就是在题目集8上进行重载,修改部分代码,从而实现题目集9中所需要的ATM功能,这3次题目集总的来说,综合性比较高,考验个人对之前所学知识的理解。
设计与分析
1、题目集7中的7-1与7-2的递进式设计总结
(7-1)类图:

(7-2)与7-1相比,多了输入多个重复数据,与对输出格式的修改,并且在某些方面进行了代码重载,由7-1的类图我们可以了解,这依旧是一个继承问题,其中子类Circle类、Rectangle类、Triangle类和Trapezoid类 继承父类Shape;由类图可知,父类Shape为抽象类,所以子类对于父类当中的所有方法都需要进行重写,并且父类当中的方法数量较少且容易构造,代码如下:
abstract class Shape{
public String shapeName;
Shape(){
}
public Shape(String shapeName){
super();
this.shapeName=shapeName;
}
/*Shape(double radius){
this.radius=radius;
}
Shape(double a,double b,double c){
this.a=a;
this.b=b;
this.c=c;
}
Shape(double width,double length){
this.width=width;
this.length=length;
}
*/
public String getShapeName() {
return shapeName;
}
public void setShapeName(String shapeName) {
this.shapeName=shapeName;
}
public abstract double getArea();
public abstract boolean validate();
public abstract String toString();
}
代码都非常的简单,对其方法前面加上 abstract来实现抽象方法的构造,而且构造好的抽象方法不需要在抽象父类中去实现,但是却要在它继承的子类当中一定要实现。
其中子类的如Circle类,只需要按照类图当中的方式去构造方法即可,不需要花太多的功夫,其他类就按照Circle类模仿写就可以了,Circle类代码如下:
class Circle extends Shape{
double radius;
String shapeNmae;
Circle(){
}
Circle(String shapeName){
super(shapeName);
this.shapeNmae=shapeName;
}
Circle(double radius){
this.radius=radius;
}
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius=radius;
}
public double getArea() {
return Math.PI*radius*radius;
}
public boolean validate() {
if(radius<0)
return false;
else
return true;
}
public String toString() {
return this.getArea()+" ";
}
}
如上所示,由Circle类的代码可以看出其中的代码都是非常的简单,不需要花太多的功夫;
最主要的是在于对重复数据的存入与对所有数据的输出,需要在Main类中实现,话不多说先上代码,Main类代码如下:
public class Main {
//在Main类中定义一个静态Scanner对象,这样在其它类中如果想要使用该对象进行输入,则直接
//使用Main.input.next…即可(避免采坑)
public static Scanner input = new Scanner(System.in);
public static void main(String[] args){
ArrayList<Integer> list = new ArrayList<Integer>();
int num = input.nextInt();
while(num != 0){
if(num < 0 || num > 4){
System.out.println("Wrong Format");
System.exit(0);
}
list.add(num);
num = input.nextInt();
}
DealCardList dealCardList = new DealCardList(list);
if(!dealCardList.validate()){
System.out.println("Wrong Format");
System.exit(0);
}
dealCardList.showResult();
input.close();
}
}
Main类代码已经由实验报告书提供了,我们只需要根据它当中的代码来进行构造类和方法即可;由类图和Main中的代码可知,我们还需要创建两个类Card类和DealCardList类,其中Card类与Shape类息息相关,对于之后的输入有的非常重要的作用,而DealCardList类则是对数据的的处理,之后通过Main类调用,并将其输出,达到题目所要求的格式,其中需要用到ArrayList来遍历数据,代码如下:
class DealCardList{
ArrayList<Card> cardList=new ArrayList<Card>();
DecimalFormat df = new DecimalFormat("0.00");//对于输出的数据保留两位数
DealCardList(){
}
DealCardList(ArrayList<Integer>list){
for(int i=0;i<list.size();i++) {
if(list.get(i)==1) {//如果输入的是1 那么对输入的数据进行圆的面积运算 下面的方法等同
double r;
r=Main.input.nextDouble();
Circle circle=new Circle(r);
Card card = new Card(circle);
card.getShape().setShapeName("Circle");
cardList.add(card);
}
if(list.get(i)==2) {
double width;
double length;
width=Main.input.nextDouble();
length=Main.input.nextDouble();
Rectangle rectangle=new Rectangle(width,length);
Card card=new Card(rectangle);
card.getShape().setShapeName("Rectangle");
cardList.add(card);
}
if(list.get(i)==3) {
double a;
double b;
double c;
a=Main.input.nextDouble();
b=Main.input.nextDouble();
c=Main.input.nextDouble();
Triangle triangle=new Triangle(a,b,c);
Card card=new Card(triangle);
card.getShape().setShapeName("Triangle");
cardList.add(card);
}
if(list.get(i)==4) {
double topSide;
double bottomSide;
double height;
topSide=Main.input.nextDouble();
bottomSide=Main.input.nextDouble();
height=Main.input.nextDouble();
Traperoid traperoid=new Traperoid(topSide,bottomSide,height);
Card card=new Card(traperoid);
card.getShape().setShapeName("Trapezoid");
cardList.add(card);
}
}
}
public boolean validate() {//对所有输入的数据进行一个总的判断,是否符合
for(int i=0;i<cardList.size();i++)
{
if(!cardList.get(i).getShape().validate())
return false;
}
return true;
}
public void cardSort() {//对所有面积进行排序
for(int k=0;k<cardList.size();k++)
for(int i=k+1;i<cardList.size();i++)
{
if(cardList.get(k).getShape().getArea()<cardList.get(i).getShape().getArea())
Collections.swap(cardList, k, i);
}
}
public double getAllArea() {
double s=0;
for(int i=0;i<cardList.size();i++)
s=s+cardList.get(i).getShape().getArea();
return s;
}
public void showResult() {
System.out.println("The original list:");
for(int i=0;i<cardList.size();i++)
System.out.print(cardList.get(i).getShape().getShapeName()+":"+df.format(cardList.get(i).getShape().getArea())+" ");
System.out.println();
System.out.println("The sorted list:");
cardSort();
for(int i=0;i<cardList.size();i++)
System.out.print(cardList.get(i).getShape().getShapeName()+":"+df.format(cardList.get(i).getShape().getArea())+" ");
System.out.println();
System.out.println("Sum of area:"+df.format(getAllArea()));
}
}
2、题目集8和题目集9ATM机仿真题目的设计分析与总结
(1).首先 题目8和9都是设计ATM仿真机,来模仿ATM机器,但也只是模仿,只要求模仿其中的一小部分比较简单的内容;题目8的难度相较于题目9难度要降低许多,因为8的ATM仿真机只要求银行卡为借记卡(不能够透支)这就避免了一些计算上的问题;题目8要求实现的功能只有两个:1 基础数据的初始化 2 用户存款、取款以及查询功能;这些功能我都比较熟悉,因为之前在寒假的时候,进行课程设计,老师要求做的学生学籍管理系统都和这些差不多,不过这次是用java语言来写;
类图如下:

老师给我们的类图,只给了大致的类,其中的方法并没有细解释,这就需要我们自己去构造方法来实现ATM仿真机的功能;因为是自己写的,我很多并没有按照老师的类图来写,这也给题目9留下了祸患,因为写题目9的时候,我需要进行大改,不是按照老师的写,改着确实挺麻烦的;题目8我偷懒了,并没有花太多的时间去钻研,直接进行暴力输出
public boolean Checknum(String num) {
boolean Checknum=num.equals("6217000010041315709")||num.equals("6217000010041315715")||num.equals("6217000010041315718")||num.equals("6217000010051320007")||num.equals("6222081502001312389")||num.equals("6222081502001312390")||num.equals("6222081502001312399")||num.equals("6222081502051320786")||num.equals("6222081502001312400")||num.equals("6222081502051320785");
return Checknum;
}
public boolean Checkword(String password) {
boolean Checkword=password.equals("88888888");
return Checkword;
}
public boolean Checkatm(String anum) {
boolean Checkatm=anum.equals("01")||anum.equals("02")||anum.equals("03")||anum.equals("04")||anum.equals("05")||anum.equals("06");
return Checkatm;
}
public void Checkmoney(String num,double money) {
if(num.equals("6217000010041315709")||num.equals("6217000010041315715")) {
if(money>this.ygmoney1) {
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}
}
else if(num.equals("6217000010041315718")) {
if(money>this.ygmoney2) {
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}
}
else if(num.equals("6217000010051320007")) {
if(money>this.gjmoney) {
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}
}
else if(num.equals("6222081502001312389")) {
if(money>this.zwmoney1) {
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}
}
else if(num.equals("6222081502001312390")) {
if(money>this.zwmoney2) {
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}
}
else if(num.equals("6222081502001312399")||num.equals("6222081502001312400")) {
if(money>this.zwmoney3) {
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}
}
else if(num.equals("6222081502051320786")) {
if(money>this.wxmoney1) {
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}
}
else if(num.equals("6222081502051320785")) {
if(money>this.wxmoney2) {
System.out.println("Sorry,your account balance is insufficient.");
System.exit(0);
}
}
}
public void CheckBank(String num,String anum) {
if(num.equals("6217000010041315709") || num.equals("6217000010041315715") ||
num.equals("6217000010041315718")) {
if(anum.equals("05") || anum.equals("06")) {
System.out.println("Sorry,cross-bank withdrawal is not supported.");
System.exit(0);
}
}
if(num.equals("6217000010051320007")) {
if(anum.equals("05") || anum.equals("06")) {
System.out.println("Sorry,cross-bank withdrawal is not supported.");
System.exit(0);
}
}
if(num.equals("6222081502001312389") || num.equals("6222081502001312390") ||
num.equals("6222081502001312399") || num.equals("6222081502001312400")) {
if(anum.equals("01") || anum.equals("02") || anum.equals("03") || anum.equals("04")) {
System.out.println("Sorry,cross-bank withdrawal is not supported.");
System.exit(0);
}
}
if(num.equals("6222081502001312389") || num.equals("6222081502001312390") ||
num.equals("6222081502001312399") || num.equals("6222081502001312400") ||
num.equals("6222081502051320785") || num.equals("6222081502051320786")) {
if(anum.equals("01") || anum.equals("02") || anum.equals("03") || anum.equals("04")) {
System.out.println("Sorry,cross-bank withdrawal is not supported.");
System.exit(0);
}
}
}
这些都是与字符串进行比较来检查输入的银行卡卡号,姓名什么的是否符合,不符合就报错,退出运行;
踩坑心得
踩坑的细节都在上述的内容中解释过了,还是要注意输出格式的正确与否,每次我都用到了
DecimalFormat df = new DecimalFormat("0.00");
来进行数字的保留两位数输出;
改进建议
其实题目7中的题目二就是题目一的改进版,而题目9则是题目8的改进版,根据这两个来看就可以了

浙公网安备 33010602011771号