题目集7~9总结

Posted MUNICH25

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了题目集7~9总结相关的知识,希望对你有一定的参考价值。

(1)前言:

题目集07:考察了继承、多态的应用ArrayList泛型的应用方法Comparable接口及泛型的应用单一职责原则的应用“开-闭”原则的应用,难度适中。

题目集08:考察了各实体类间一对多的组合关系,且要求程序具有扩展性,难度较大。

题目集09:在题目集08的基础上,重点考察继承、多态及抽象类的应用,难度适中。

(2)设计与分析:

题目集07  7-2 图形卡片分组游戏 (60 )

掌握类的继承、多态性使用方法以及接口的应用。 具体需求参考作业指导书。

2021-OO第07次作业-2指导书V1.0.pdf

输入格式:

  • 在一行上输入一串数字(1~4,整数),其中,1代表圆形卡片,2代表矩形卡片,3代表三角形卡片,4代表梯形卡片。各数字之间以一个或多个空格分隔,以“0”结束。例如:1 3 4 2 1 3 4 2 1 3 0
  • 根据第一行数字所代表的卡片图形类型,依次输入各图形的相关参数,例如:圆形卡片需要输入圆的半径,矩形卡片需要输入矩形的宽和长,三角形卡片需要输入三角形的三条边长,梯形需要输入梯形的上底、下底以及高。各数据之间用一个或多个空格分隔。

输出格式:

  • 如果图形数量非法(<=0)或图形属性值非法(数值<0以及三角形三边不能组成三角形),则输出Wrong Format。
  • 如果输入合法,则正常输出,所有数值计算后均保留小数点后两位即可。输出内容如下:
  1. 排序前的各图形类型及面积,格式为[图形名称1:面积值1图形名称2:面积值2 …图形名称n:面积值n ],注意,各图形输出之间用空格分开,且输出最后存在一个用于分隔的空格,在结束符“]”之前;
  2. 输出分组后的图形类型及面积,格式为[圆形分组各图形类型及面积][矩形分组各图形类型及面积][三角形分组各图形类型及面积][梯形分组各图形类型及面积],各组内格式为图形名称:面积值。按照“Circle、Rectangle、Triangle、Trapezoid”的顺序依次输出;
  3. 各组内图形排序后的各图形类型及面积,格式同排序前各组图形的输出;
  4. 各组中面积之和的最大值输出,格式为The max area:面积值。

提交源码:

import java.util.*;

public class Test07 {

    public static Scanner input = new Scanner(System.in);

    public static void Test07(String[] args){

        ArrayList<Integer> list = new ArrayList<>();

        int num = input.nextInt();

        if(num==0){

            System.out.print("Wrong Format");

        return;}

        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();

    }

}

class Card {

    Shape shape;

    public Card (Shape shape){ this.shape = shape; }

    public Shape getShape(){ return shape; }

}

class DealCardList {

    ArrayList<Card> cardList = new ArrayList<>();

    ArrayList<Circle> circleArrayList = new ArrayList<>();

    ArrayList<Rectangle> rectangleArrayList = new ArrayList<>();

    ArrayList<Triangle> triangleArrayList = new ArrayList<>();

    ArrayList<Trapezoid> tiList = new ArrayList<>();

    public DealCardList(ArrayList<Integer> list){

        for (Integer integer:list){

            if (integer==0)

                break;

            switch (integer){

                case 1:

                    double radius = Test07.input.nextDouble();

                    Card card1 = new Card(new Circle(radius));

                    Circle circle = new Circle(radius);

                    card1.getShape().setShapeName("Circle");

                    circle.setShapeName("Circle");

                    cardList.add(card1);

                    circleArrayList.add(circle);

                    break;

                case 2:

                    double width = Test07.input.nextDouble();

                    double length = Test07.input.nextDouble();

                    Card card2 = new Card(new Rectangle(width,length));

                    Rectangle rectangle = new Rectangle(width,length);

                    card2.getShape().setShapeName("Rectangle");

                    rectangle.setShapeName("Rectangle");

                    cardList.add(card2);

                    rectangleArrayList.add(rectangle);

                    break;

                case 3:

                    double side1 = Test07.input.nextDouble();

                    double side2 = Test07.input.nextDouble();

                    double side3 = Test07.input.nextDouble();

                    Card card3 = new Card(new Triangle(side1,side2,side3));

                    Triangle triangle = new Triangle(side1,side2,side3);

                    card3.getShape().setShapeName("Triangle");

                    triangle.setShapeName("Triangle");

                    cardList.add(card3);

                    triangleArrayList.add(triangle);

                    break;

                case 4:

                    double topSide = Test07.input.nextDouble();

                    double botSide = Test07.input.nextDouble();

                    double height = Test07.input.nextDouble();

                    Card card4 = new Card(new Trapezoid(topSide,botSide,height));

                    Trapezoid trapezoid = new Trapezoid(topSide,botSide,height);

                    card4.getShape().setShapeName("Trapezoid");

                    trapezoid.setShapeName("Trapezoid");

                    cardList.add(card4);

                    tiList.add(trapezoid);

                    break;

            }

        }

    }

    public boolean validate () {

        int i;

        for (i=0;i<cardList.size();i++) {

            if (!cardList.get(i).getShape().validate()) {

                return false;

            }

        }

        return true;

    }

    public void cardSort () {

        System.out.print("[");

        Collections.sort(circleArrayList,new Circle());

        for (Circle circle:circleArrayList) {

            System.out.print(circle);

        }

        System.out.print("]");

        System.out.print("[");

        Collections.sort(rectangleArrayList,new Rectangle());

        for (Rectangle rectangle:rectangleArrayList) {

            System.out.print(rectangle);

        }

        System.out.print("]");

        System.out.print("[");

        Collections.sort(triangleArrayList,new Triangle());

        for (Triangle triangle:triangleArrayList) {

            System.out.print(triangle);

        }

        System.out.print("]");

        System.out.print("[");

        Collections.sort(tiList,new Trapezoid());

        for (Trapezoid trapezoid:tiList) {

            System.out.print(trapezoid);

        }

        System.out.print("]");

    }

    public double getMaxArea () {

        double sumAreaOfCircle = 0;

        double sumAreaOfRectangle = 0;

        double sumAreaOfTriangle = 0;

        double sumAreaOfTrapezoid = 0;

        for (Circle circle : circleArrayList) {

            sumAreaOfCircle = sumAreaOfCircle + circle.getArea();

        }

        for (Rectangle rectangle : rectangleArrayList) {

            sumAreaOfRectangle = sumAreaOfRectangle + rectangle.getArea();

        }

        for (Triangle triangle : triangleArrayList) {

            sumAreaOfTriangle = sumAreaOfTriangle + triangle.getArea();

        }

        for (Trapezoid trapezoid : tiList) {

            sumAreaOfTrapezoid = sumAreaOfTrapezoid + trapezoid.getArea();

        }

        double max1,max2;

        if (sumAreaOfCircle > sumAreaOfRectangle)

            max1 = sumAreaOfCircle;

        else

            max1 = sumAreaOfRectangle;

        if (sumAreaOfTriangle > sumAreaOfTrapezoid)

            max2 = sumAreaOfTriangle;

        else

            max2 = sumAreaOfTrapezoid;

        if (max1 > max2)

            return max1;

        return max2;

    }

    public void showResult(){

        System.out.println("The original list:");

        System.out.print("[");

        for (Card card:cardList){

            System.out.print(card.getShape());

        }

        System.out.print("]");

        System.out.println();

        System.out.println("The Separated List:");

        System.out.print("[");

        for (Circle circles:circleArrayList){

            System.out.print(circles);

        }

        System.out.print("]");

        System.out.print("[");

        for (Rectangle rectangles:rectangleArrayList){

            System.out.print(rectangles);

        }

        System.out.print("]");

        System.out.print("[");

        for (Triangle triangles:triangleArrayList){

            System.out.print(triangles);

        }

        System.out.print("]");

        System.out.print("[");

        for (Trapezoid trapezoids:tiList){

            System.out.print(trapezoids);

        }

        System.out.print("]");

        System.out.println();

        System.out.println("The Separated sorted List:");

        cardSort();

        System.out.println();

        System.out.print("The max area:");

        System.out.print(String.format("%.2f",getMaxArea()));

    }

}

abstract class Shape{

    String shapeName;

    public Shape(){}

    public String getShapeName(){

        return shapeName;

    }

    public void setShapeName(String shapeName){

        this.shapeName = shapeName;

    }

    public abstract double getArea();

    public abstract boolean validate();

    @Override

    public String toString(){

        return getShapeName()+":"+String.format("%.2f ",getArea());

    }

}

class Circle extends Shape implements Comparator<Circle>{

    double radius;

    public Circle(){}

    public Circle(double radius){

        this.radius = radius;

    }

    public double getRadius(){

        return radius;

    }

    @Override

    public double getArea(){

        return getRadius()*getRadius()*Math.PI;

    }

    @Override

    public boolean validate(){

        if (getRadius() < 0)

            return false;

        else return true;

    }

    @Override

    public int compare(Circle o1,Circle o2){

        if (o1.getArea()<o2.getArea())

            return 1;

        return -1;

    }

}

class Rectangle extends Shape implements Comparator<Rectangle>{

    double length;

    double width;

    public Rectangle(){}

    public Rectangle (double width,double length){

        this.width = width;

        this.length = length;

    }

    public double getLength(){

        return length;

    }

    public double getWidth(){

        return width;

    }

    @Override

    public double getArea(){

        return getLength()*getWidth();

    }

    @Override

    public boolean validate() {

        if (getWidth()<0||getLength()<0)

            return false;

        else

            return true;

    }

    @Override

    public int compare(Rectangle o1,Rectangle o2) {

        if (o1.getArea()<o2.getArea())

            return 1;

        return -1;

    }

}

class Triangle extends Shape implements Comparator<Triangle>{

    double side1;

    double side2;

    double side3;

    public Triangle(){}

    public Triangle(double side1,double side2,double side3){

        this.side1=side1;

        this.side2=side2;

        this.side3=side3;

    }

    @Override

    public double getArea(){

        return Math.sqrt((side1+side2+side3)*(side1+side2-side3)*(side1+side3-side2)*(side2+side3-side1))/4;

    }

    @Override

    public boolean validate(){

        boolean ret=true;

        if (!(side1>0&&side3>0&&side2>0))ret=false;

        else{

            if (!(side1+side2>side3&&side1+side3>side2&&side2+side3>side1))ret=false;

        }

        return ret;

    }

    @Override

    public int compare(Triangle o1,Triangle o2){

        if (o1.getArea()<o2.getArea())

            return 1;

        return -1;

    }

}

class Trapezoid extends Shape implements Comparator<Trapezoid>{

    double topSide;

    double botSide;

    double height;

    public Trapezoid(){}

    public Trapezoid(double topSide,double botSide,double height){

        this.topSide=topSide;

        this.botSide=botSide;

        this.height=height;

    }

    @Override

    public double getArea(){return(topSide+botSide)*height/2;

    }

    @Override

    public boolean validate(){

        if(topSide<0||botSide<0||height < 0)

            return false;

        else

            return true;

    }

    @Override

    public int compare(Trapezoid o1,Trapezoid o2){

        if(o1.getArea()<o2.getArea())

            return 1;

        return -1;

    }

}

这道题目和前面那道不同的地方是,需要创建不同图形的数组列表用来存放各种图形

 

即在DealCardList操作类下创建四个数组列表,这样做的目的一是为了方便打印输出,二是方便实现Comparable接口排序。这道题目让我掌握了很多东西,1:重写toString方法后,遍历数组列表打印只需打印成员即可。2:如何实现Comparator接口且如何实现升序降序排序。

题目集08  7-1 ATM机类结构设计(一) (100 分)

设计ATM仿真系统,具体要求参见作业说明。 OO作业8-1题目说明.pdf

输入格式:

每一行输入一次业务操作,可以输入多行,最终以字符#终止。具体每种业务操作输入格式如下:

  • 存款、取款功能输入数据格式: 卡号 密码 ATM机编号 金额(由一个或多个空格分隔), 其中,当金额大于0时,代表取款,否则代表存款。
  • 查询余额功能输入数据格式: 卡号

输出格式:

①输入错误处理

  • 如果输入卡号不存在,则输出Sorry,this card does not exist.。
  • 如果输入ATM机编号不存在,则输出Sorry,the ATM\'s id is wrong.。
  • 如果输入银行卡密码错误,则输出Sorry,your password is wrong.。
  • 如果输入取款金额大于账户余额,则输出Sorry,your account balance is insufficient.。
  • 如果检测为跨行存取款,则输出Sorry,cross-bank withdrawal is not supported.。

②取款业务输出

输出共两行,格式分别为:

[用户姓名]在[银行名称]的[ATM编号]上取款¥[金额]

当前余额为¥[金额]

其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。

③存款业务输出

输出共两行,格式分别为:

[用户姓名]在[银行名称]的[ATM编号]上存款¥[金额]

当前余额为¥[金额]

其中,[]说明括起来的部分为输出属性或变量,金额均保留两位小数。

④查询余额业务输出

¥[金额]

金额保留两位小数。

提交源码:

import java.util.ArrayList;

        import java.util.HashMap;

        import java.util.Map;

        import java.util.Scanner;

 

public class Main {

    public static void main (String[] args) {

        Scanner input = new Scanner(System.in);

        BankCard bankCard = new BankCard();

        Bank bank1 = new Bank("中国建设银行");

        Bank bank2 = new Bank("中国工商银行");

        Bank.bankArrayList.add(bank1);

        Bank.bankArrayList.add(bank2);

        bank1.atmsUnderBank.add(new ATM("01",bank1));

        bank1.atmsUnderBank.add(new ATM("02",bank1));

        bank1.atmsUnderBank.add(new ATM("03",bank1));

        bank1.atmsUnderBank.add(new ATM("04",bank1));

        bank2.atmsUnderBank.add(new ATM("05",bank2));

        bank2.atmsUnderBank.add(new ATM("06",bank2));

        User user1 = new User("杨过");

        User user2 = new User("郭靖");

        User user3 = new User("张无忌");

        User user4 = new User("韦小宝");

        Account account1 = new Account("3217000010041315709",10000,bank1,user1);

        Account account2 = new Account("3217000010041315715",10000,bank1,user1);

        Account account3 = new Account("3217000010051320007",10000,bank1,user2);

        Account account4 = new Account("3222081502001312389",10000,bank2,user3);

        Account account5 = new Account("3222081502001312390",10000,bank2,user3);

        Account account6 = new Account("3222081502001312399",10000,bank2,user3);

        Account account7 = new Account("3222081502051320785",10000,bank2,user4);

        Account account8 = new Account("3222081502051320786",10000,bank2,user4);

        BankCard bankCard1 = new BankCard("6217000010041315709","88888888",account1);

        BankCard bankCard2 = new BankCard("6217000010041315715","88888888",account1);

        BankCard bankCard3 = new BankCard("6217000010041315718","88888888",account2);

        BankCard bankCard4 = new BankCard("6217000010051320007","88888888",account3);

        BankCard bankCard5 = new BankCard("6222081502001312389","88888888",account4);

        BankCard bankCard6 = new BankCard("6222081502001312390","88888888",account5);

        BankCard bankCard7 = new BankCard("6222081502001312399","88888888",account6);

        BankCard bankCard8 = new BankCard("6222081502001312400","88888888",account6);

        BankCard bankCard9 = new BankCard("6222081502051320785","88888888",account7);

        BankCard bankCard10 = new BankCard("6222081502051320786","88888888",account8);

        bank1.accountUnderBank.add(account1);

        bank1.accountUnderBank.add(account2);

        bank1.accountUnderBank.add(account3);

        bank2.accountUnderBank.add(account4);

        bank2.accountUnderBank.add(account5);

        bank2.accountUnderBank.add(account6);

        bank2.accountUnderBank.add(account7);

        bank2.accountUnderBank.add(account8);

        user1.accountsUnderUser.add(account1);

        user1.accountsUnderUser.add(account2);

        user2.accountsUnderUser.add(account3);

        user3.accountsUnderUser.add(account4);

        user3.accountsUnderUser.add(account5);

        user3.accountsUnderUser.add(account6);

        user4.accountsUnderUser.add(account7);

        user4.accountsUnderUser.add(account8);

        account1.cards.put(bankCard1.getBankCardID(),bankCard1);

        account1.cards.put(bankCard2.getBankCardID(),bankCard2);

        account2.cards.put(bankCard3.getBankCardID(),bankCard3);

        account3.cards.put(bankCard4.getBankCardID(),bankCard4);

        account4.cards.put(bankCard5.getBankCardID(),bankCard5);

        account5.cards.put(bankCard6.getBankCardID(),bankCard6);

        account6.cards.put(bankCard7.getBankCardID(),bankCard7);

        account6.cards.put(bankCard8.getBankCardID(),bankCard8);

        account7.cards.put(bankCard9.getBankCardID(),bankCard9);

        account8.cards.put(bankCard10.getBankCardID(),bankCard10);

//        account1.bankCardArrayList.add(bankCard2);

//        account2.bankCardArrayList.add(bankCard3);

//        account3.bankCardArrayList.add(bankCard4);

//        account4.bankCardArrayList.add(bankCard5);

//        account5.bankCardArrayList.add(bankCard6);

//        account6.bankCardArrayList.add(bankCard7);

//        account6.bankCardArrayList.add(bankCard8);

//        account7.bankCardArrayList.add(bankCard9);

//        account8.bankCardArrayList.add(bankCard10);

 

        for (Bank bank : Bank.bankArrayList) {

            ATM.atmArrayList.addAll(bank.atmsUnderBank);

            Account.accountArrayList.addAll(bank.accountUnderBank) ;

            for ( Account account : bank.accountUnderBank) {

                BankCard.bankCardArrayList.addAll(account.cards.values());

            }

        }

 

 

        String str = input.nextLine();

        while (!str.equals("#")){

            String[] splits = str.split("\\\\s+");

            DealOperations dealOperations = new DealOperations(splits);

            dealOperations.deal();

            str = input.nextLine();

        }

    }

 

}

 

class DealOperations {

    private String userName = "";

    private String accountBankName = "";

    private double balance;

    private String atmBankName = "";

    private String[] str;

    Account account = new Account();

    ATM atm = new ATM();

 

    public DealOperations(String[] str) {

        this.str = str;

    }

 

 

    public boolean validateOfBankCardID () {

        for (Account account : account.accountArrayList) {

            if (account.cards.containsKey(str[0])) {

                userName = account.getUser().getUserName();

                accountBankName = account.getBank().getBankName();

                balance = account.getBalance();

                return true;

            }

        }

        return false;

    }

 

    public boolean validateATMID () {

        for (ATM atm : atm.atmArrayList) {

            题目集7~9的总结

题目集7~9总结

关于pta题目集7~9总结

题目集7-9的总结性Blog

对Java题目集7-9的总结性Blog

PTA题目集4~6的总结