• 2023年11月15号期中测验编程题(Java)


    本篇还有3篇相关文章->

    2023年11月15号期中测验判断题(Java)-CSDN博客

    2023年11月15号期中测验选择题(Java)-CSDN博客

    2023年11月15号期中测验主观题(Java)-CSDN博客

    7-1 学生类定义

    定义一个Student类,含类成员变量:
    String name、String gender、int score、boolean award,所有的变量为私有(private)。

    1.编写有参构造函数:
    对name、gender、score、award赋初值;
    2. 重写(覆盖)toString方法:
    按照格式:类名 [name=, gender=,score=, award=]输出;
    3.对每个成员变量生成setter/getter方法;
    4.main方法中创建对象并输出。

    输入格式:

    输入1行学生的信息:姓名、性别、成绩、获奖情况。

    输出格式:

    通过对象调用toString方法输出学生的姓名、性别、成绩、获奖情况信息。

    输入样例:

    Rose female 96 true

    输出样例:

    Student [name=Rose, gender=female, score=96, award=true]

    代码长度限制                                                                                                                  16 KB

    时间限制                                                                                                                       400 ms

    内存限制                                                                                                                         64 MB

     解释:

    本题主要考察对Object类(超类)的toString方法的重写,较为基础,按要求格式重写返回的字符串格式后处理输入输出即可,细节处看代码。

    AC代码:

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner sc = new Scanner(System.in);
    5. String name = sc.next(), gender = sc.next();
    6. int score = sc.nextInt();
    7. boolean award = sc.nextBoolean();
    8. Student s = new Student(name, gender, score, award);
    9. System.out.println(s.toString());
    10. }
    11. }
    12. class Student {
    13. private String name;
    14. private String gender;
    15. private int score;
    16. private boolean award;
    17. public Student(String name, String gender, int score, boolean award) {
    18. this.name = name;
    19. this.gender = gender;
    20. this.score = score;
    21. this.award = award;
    22. }
    23. @Override
    24. public String toString() {
    25. return "Student [" +
    26. "name=" + name +
    27. ", gender=" + gender +
    28. ", score=" + score +
    29. ", award=" + award +
    30. ']';
    31. }
    32. }


    7-2 多态方式实例化子类对象并调用shout方法打印输出信息

    声明一个抽象Animal类,此类中定义抽象方法shout();

    声明Dog类、Cat类,均继承自Animal类,并重写了shout()方法;

    运用多态方式实例化子类对象并调用shout()方法打印输出信息;

    具体输出要求请看测试说明。

    输入格式:

    输出格式:

    多态方式实例化子类对象打印动物的叫声

    输入样例:

    在这里给出一组输入。例如:

    输出样例:

    在这里给出相应的输出。例如:

    小狗汪汪叫
    小猫喵喵叫

    代码长度限制                                                                                                                  16 KB

    时间限制                                                                                                                       400 ms

    内存限制                                                                                                                         64 MB

    解释:

    本题考察抽象类及类的继承知识点,建立继承关系,重写父类方法后处理输出即可,细节处看代码。

    较为基础,甚至可以直接面向结果编程(当然为了学新知识点不建议这样做)。。。

    AC代码: 

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Dog d = new Dog();
    5. Cat c = new Cat();
    6. d.shout();
    7. c.shout();
    8. }
    9. }
    10. abstract class Animal {
    11. abstract void shout();
    12. }
    13. class Dog extends Animal {
    14. void shout() {
    15. System.out.println("小狗汪汪叫");
    16. }
    17. }
    18. class Cat extends Animal {
    19. void shout() {
    20. System.out.println("小猫喵喵叫");
    21. }
    22. }

    7-3 定义抽象类Person、派生类Student和类Teacher 

    设计抽象类Person,派生出具体类:学生类Student和教师类Teacher,创建若干不同类对象后并在主方法中测试。
    数据成员定义:
    Person [ID,姓名,生日]
    Student [专业,成绩]
    Teacher [职称,工资]
    带参构造方法分别为:
    Person(int id,String name, int bir)
    Student(int id,String name, int bir, String major,double score)
    Teacher(int id,String name, int bir, String title, double salary)
    toString方法(Eclipse自动生成)

    输入格式:

    第一行整数n表示有n个对象,每个对象占2行,第一行为数字0(表示学生)或1(表示教师),第二行为生成对象的参数。

    输出格式:

    按行输出具体对象的信息。

    输入样例:

    在这里给出一组输入。例如:

    5
    0
    10101 Peter 20010121 Computer 87.5
    1
    20110014 Crystal 19900103 AI 7000
    0
    10102 Rose 20010715 E-Commerce 90
    1
    20120019 David 19781218 Prof 12000
    0
    10103 Semon 20000405 Computer 88

    输出样例:

    在这里给出相应的输出。例如:

    Student [id=10101, name=Peter, bir=20010121, major=Computer, score=87.5]
    Teacher [id=20110014, name=Crystal, bir=19900103, title=AI, salary=7000.0]
    Student [id=10102, name=Rose, bir=20010715, major=E-Commerce, score=90.0]
    Teacher [id=20120019, name=David, bir=19781218, title=Prof, salary=12000.0]
    Student [id=10103, name=Semon, bir=20000405, major=Computer, score=88.0]

    代码长度限制                                                                                                                  16 KB

    时间限制                                                                                                                       400 ms

    内存限制                                                                                                                         64 MB

    解释:

    本题就是本篇前两题的结合版本,建立继承关系、重写对应的成员变量和toString方法,再处理输入输出即可,细节处看代码。

    AC代码:

    1. import java.util.Scanner;
    2. public class Main {
    3. public static void main(String[] args) {
    4. Scanner sc = new Scanner(System.in);
    5. int n = sc.nextInt();
    6. for (int i = 0; i < n; i++) {
    7. int teac_or_stu = sc.nextInt();
    8. if (teac_or_stu == 0) {
    9. String id = sc.next(), name = sc.next(), birth = sc.next(), major = sc.next();
    10. double score = sc.nextDouble();
    11. Student s = new Student(id, name, birth, major, score);
    12. System.out.println(s.toString());
    13. } else {
    14. String id = sc.next(), name = sc.next(), birth = sc.next(), title = sc.next();
    15. double salary = sc.nextDouble();
    16. Teacher t = new Teacher(id, name, birth, title, salary);
    17. System.out.println(t.toString());
    18. }
    19. }
    20. }
    21. }
    22. abstract class Person {
    23. String id, name, birth;
    24. }
    25. class Student extends Person {
    26. String major;
    27. double score;
    28. Student(String id, String name, String birth, String major, double score) {
    29. this.id = id;
    30. this.name = name;
    31. this.birth = birth;
    32. this.major = major;
    33. this.score = score;
    34. }
    35. @Override
    36. public String toString() {
    37. return "Student [" +
    38. "id=" + id +
    39. ", name=" + name +
    40. ", bir=" + birth +
    41. ", major=" + major +
    42. ", score=" + score +
    43. ']';
    44. }
    45. }
    46. class Teacher extends Person {
    47. String title;
    48. double salary;
    49. Teacher(String id, String name, String birth, String title, double salary) {
    50. this.id = id;
    51. this.name = name;
    52. this.birth = birth;
    53. this.title = title;
    54. this.salary = salary;
    55. }
    56. @Override
    57. public String toString() {
    58. return "Teacher [" +
    59. "id=" + id +
    60. ", name=" + name +
    61. ", bir=" + birth +
    62. ", title=" + title +
    63. ", salary=" + salary +
    64. ']';
    65. }
    66. }

    7-4 USB接口的定义

    定义一个USB接口,并通过Mouse和U盘类实现它,具体要求是:

    1.接口名字为USB,里面包括两个抽象方法:

    void work();描述可以工作

    void stop(); 描述停止工作

    2.完成类Mouse,实现接口USB,实现两个方法:

    work方法输出“我点点点”;

    stop方法输出 “我不能点了”;

    3.完成类UPan,实现接口USB,实现两个方法:

    work方法输出“我存存存”;

    stop方法输出 “我走了”;

    4测试类Main中,main方法中

    定义接口变量usb1 ,存放鼠标对象,然后调用work和stop方法

    定义接口数组usbs,包含两个元素,第0个元素存放一个Upan对象,第1个元素存放Mouse对象,循环数组,对每一个元素都调用work和stop方法。

    输入格式:

    输出格式:

    输出方法调用的结果

    输入样例:

    在这里给出一组输入。例如:

    输出样例:

    在这里给出相应的输出。例如:

    我点点点
    我不能点了
    我存存存
    我走了
    我点点点
    我不能点了

    代码长度限制                                                                                                                  16 KB

    时间限制                                                                                                                       400 ms

    内存限制                                                                                                                         64 MB

    解释:

    本题考察类对接口的实现,建立实现关系后在子类中实现对应的方法即可,需要注意的是接口中的方法默认由public abstract修饰,因此子类实现的方法也必须用public修饰。

    另外在测试类Main的main方法中用接口定义数组可能是初学者的陌生点,但本质和创建基本数据类型的数组没有太大区别,细节处看代码。

    AC代码:

    1. public class Main {
    2. public static void main(String[] args) {
    3. Mouse usb1 = new Mouse();
    4. usb1.work();
    5. usb1.stop();
    6. USB[] usbs = new USB[2];
    7. Upan u = new Upan();
    8. usbs[0] = u;
    9. Mouse m = new Mouse();
    10. usbs[1] = m;
    11. for (int i = 0; i < 2; i++) {
    12. usbs[i].work();
    13. usbs[i].stop();
    14. }
    15. }
    16. }
    17. interface USB {
    18. void work();
    19. void stop();
    20. }
    21. class Mouse implements USB {
    22. public void work() {
    23. System.out.println("我点点点");
    24. }
    25. public void stop() {
    26. System.out.println("我不能点了");
    27. }
    28. }
    29. class Upan implements USB {
    30. public void work() {
    31. System.out.println("我存存存");
    32. }
    33. public void stop() {
    34. System.out.println("我走了");
    35. }
    36. }

  • 相关阅读:
    python中的类型提示(定义函数时加入箭头->)
    【牛客刷题-SQL】SQL1 查询所有列
    后端秋招面经
    Python Selenium 超星点击操作处理
    电脑删除的文件如何找回?
    项目经理必看!用一句话说明你当过项目经理
    预测杭州五一黄金周的旅游出行人数
    数据库的下一个变革方向——云原生数据库
    【C++ 学习】库文件和头文件编写
    私有云:架构图
  • 原文地址:https://blog.csdn.net/liKeQing1027520/article/details/134500711