1.png2.png3.png4.png5.png6.jpg

一、2019年

1. 第一题

  1. Test.java
  2. Test.classSinger.class
  3. e:\test01\src\ctgu\edu\cn

    2. 第二题

    1. import java.util.*;
    2. public class MessageParser{
    3. public double getSum(String message){
    4. String regex = "[^0123456789.]+";
    5. message = message.replaceAll(regex,"#");
    6. StringTokenizer fenxi = new StringTokenizer(message,"#");
    7. double sum = 0;
    8. while(fenxi.hasMoreTokens()){
    9. String item = fenxi.nextToken();
    10. double price += Double.valueOf(item);
    11. sum = sum + price;
    12. }
    13. return sum;
    14. }
    15. }

    3. 第三题

    ```java public class Test{ public static void main(String[] args){
    1. Screen screen = new Screen(false);
    2. Machine machine = new Machine();
    3. try{
    4. machine.checkScreen(screen);
    5. System.out.println("合格屏");
    6. }catch(DefectiveScreenException e){
    7. e.toString();
    8. }
    } }

class DefectiveScreenException extends Exception{ @Override public void toString(){ System.out.println(“缺陷屏”); } }

class Screen{ public boolean isDefective;

  1. public Screen(){
  2. }
  3. public Screen(boolean isDefective){
  4. this.isDefective = isDefective;
  5. }

}

class Machine{ public void checkScreen(Screen screen) throws DefectiveScreenException{ if(screen.isDefective()){ throw new DefectiveScreenException(); } } }

  1. <a name="V1Xce"></a>
  2. ## 4. 第四题
  3. 多线程不考,暂时跳过
  4. <a name="GeMNH"></a>
  5. ## 5. 第五题
  6. ```java
  7. public class TestEmployee{
  8. public static void main(String[] args){
  9. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
  10. Employee e1 = new Employee(1,"yxr","男",format.parse("2021-12-12"));
  11. Employee e2 = new Employee(2,"zyl","女",format.parse("2021-12-13"));
  12. System.out.println(e1);
  13. System.out.println(e2);
  14. }
  15. }
  16. class Employee{
  17. private int no;
  18. private String name;
  19. private String sex;
  20. private Date entryDate;
  21. public Employee(){
  22. }
  23. public Employee(int no,String name,String sex,Date entryDate){
  24. this.no = no;
  25. this.name = name;
  26. this.sex = sex;
  27. this.entryDate = entryDate;
  28. }
  29. public void setNo(int no){
  30. this.no = no;
  31. }
  32. public int getNo(){
  33. return this.no;
  34. }
  35. public void setName(String name){
  36. this.name = name;
  37. }
  38. public String getName(){
  39. return this.name;
  40. }
  41. public void setSex(String sex){
  42. this.sex = sex;
  43. }
  44. public String getSex(){
  45. return this.sex;
  46. }
  47. public void setEntryDate(Date entryDate){
  48. this.entryDate = entryDate;
  49. }
  50. public Date getEntryDate(){
  51. return this.entryDate;
  52. }
  53. @Override
  54. public String toString(){
  55. return "姓名:" + name + "入职时间:" + entryDate;
  56. }
  57. }

6. 第六题

  1. public class FrameTest{
  2. public static void main(String[] args){
  3. new MyFrame();
  4. }
  5. }
  6. class MyFrame extends JFrame{
  7. public MyFrame(){
  8. Container container = getContentPane();
  9. JButton button = new JButton("读取");
  10. button.addActionListener(new ButtonClickController());
  11. container.add(button);
  12. setVisible(true);
  13. setBounds(300,300,400,400);
  14. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  15. }
  16. }
  17. class ButtonClickController implements ActionListener{
  18. private ArrayList<String> strings;
  19. private int clickSum;
  20. public ButtonClickController(){
  21. strings = new ArrayList<>();
  22. clickSum = 0;
  23. FileReader fileReader = null;
  24. BufferedReader bufferdReader = null;
  25. try{
  26. fileReader = new FileReader("test.txt");
  27. bufferdReader = new BufferedReader(fileReader);
  28. String str = null;
  29. while((str = bufferdReader.readLine()) != null){
  30. strings.add(str);
  31. }
  32. }catch(IOException e){
  33. e.printStackTrace();
  34. }finally{
  35. if(bufferdReader != null){
  36. try{
  37. bufferdReader.close();
  38. }catch(IOException e){
  39. e.printStackTrace();
  40. }
  41. }
  42. }
  43. }
  44. @Override
  45. public void actionPerformed(ActionEvent e){
  46. if(clickSum >= strings.size()){
  47. throw new RuntimeException("已到达文件末尾!");
  48. }else{
  49. System.out.println(strings.get(clickSum));
  50. clickSum ++;
  51. }
  52. }
  53. }

7. 第七题

  1. public class TestShape{
  2. public static void main(String[] args){
  3. ArrayList<Shape> list = new ArrayList<>();
  4. Circle c1 = new Circle(20.0);
  5. Circle c2 = new Circle(5.2);
  6. Triangle t1 = new Triangle(10,20);
  7. Triangle t2 = new Triangle(5,3);
  8. list.add(c1);
  9. list.add(c2);
  10. list.add(t1);
  11. list.add(t2);
  12. Collections.sort(list);
  13. list.forEach(e -> System.out.println(e.getArea()));
  14. }
  15. }
  16. interface Shape extends Comparable<Shape>{
  17. double getArea();
  18. @Override
  19. int compareTo(Shape o);
  20. }
  21. class Circle implements Shape{
  22. private double radius;
  23. public Circle(){
  24. }
  25. public Circle(double radius){
  26. this.radius = radius;
  27. }
  28. @Override
  29. public double getArea(){
  30. return Math.PI * radius * radius;
  31. }
  32. @Override
  33. public int compareTo(Shape o){
  34. Double d1 = getArea();
  35. Double d2 = o.getArea();
  36. return d1.compareTo(d2);
  37. }
  38. }
  39. class Triangle implements Shape{
  40. private double length;
  41. private double width;
  42. public Triangle(){
  43. }
  44. public Triangle(double length,double width){
  45. this.length = length;
  46. this.width = width;
  47. }
  48. @Override
  49. public double getArea(){
  50. return length*width;
  51. }
  52. @Override
  53. public int compareTo(Shape o){
  54. Double d1 = getArea();
  55. Double d2 = o.getArea();
  56. return d1.compareTo(d2);
  57. }
  58. }

8. 第八题

TestGUI.java

  1. public class TestGUI{
  2. public static void main(String[] args){
  3. javax.swing.SwingUtilities.invokeLater(() -> {
  4. ComputeFrame win = new ComputeFrame();
  5. win.setTitle("TestGUI");
  6. win.setBounds(100,100,420,260);
  7. });
  8. }
  9. }

Circle.java

  1. public class Circle{
  2. private double radius;
  3. public Circle(double radius){
  4. this.radius = radius;
  5. }
  6. public Circle(){
  7. }
  8. public double getArea(){
  9. return Math.PI * radius * radius;
  10. }
  11. public void setRadius(double radius){
  12. this.radius = radius;
  13. }
  14. }

ComputeFrame.java

  1. public class ComputeFrame extends JFrame implements ActionListener{
  2. Circle circle;
  3. JTextField textRadius;
  4. JTextArea showArea;
  5. JButton computeButton;
  6. ComputeFrame(){
  7. init();
  8. setVisible(true);
  9. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  10. }
  11. void init(){
  12. circle = new Circle();
  13. textRadius = new JTextField(5);
  14. showArea = new JTextArea();
  15. computeButton = new JButton("计算面积");
  16. JPanel pNorth = new JPanel();
  17. pNorth.add(new JLabel("请输入半径:"));
  18. pNorth.add(textRadius);
  19. pNorth.add(computeButton);
  20. computeButton.addActionListener(this);
  21. add(pNorth,BorderLayout.NORTH);
  22. add(new JScrollPane(showArea),BorderLayout.CENTER);
  23. }
  24. public void actionPerformed(ActionEvent e){
  25. try{
  26. double radius = Double.parseDouble(textRadius.getText());
  27. circle.setRadius(radius);
  28. double area = circle.getArea();
  29. showArea.append("半径为:" + radius + "的圆形面积为:" + area + "\n");
  30. }catch(Exception e){
  31. showArea.append("\n" + e + "\n");
  32. }
  33. }
  34. }