Java程序设计(2021春)——第四章接口与多态课后题(选择题+编程题)答案与详解



目录



第四章选择题

Tip:选择题部分我只针对部分我出错的或我认为有价值的题目在此整理。

4.0 导学



4.1接口



4.2.1-4.2.2-类型转换



4.2.3-方法的查找

T2

题面

已知

import java.io.*;

class Person {
public static void print() {
System.out.print("Person");
}
}

class Employee extends Person {
public void print() {
System.out.print("Employee");
}
}

class Manager extends Employee {
public void print() {
System.out.print("Manager");
}
}

public class Test {
public static void main(String[] args) {
Manager man = new Manager();
Employee emp1 = new Employee();
Employee emp2 = (Employee) man;
Person person = (Person) man;
emp2.print();
System.out.print("#");
person.print();
}

}


对于以上代码,其输出结果是

A Employee#Person

B Manager#Person

C Employee#Manager

D Manager#Manager

答案

D

详解

​man​​所指向的对象是​​Manager()​​类型的,​​emp2​​为经由强转的​​man​​的引用,




3.5 泛型

T1

题面

Java泛型机制的优点有

A 可以使代码编写变得简单

B 比较安全

C 消除对Object类的强制类型转换=

D 使得代码运行性能增强

答案

A B C

详解



第四章编程题

T1 字符串数字验证

题面

验证一个给定的字符串是否为数字。是则输出1,不是则输出0 一些例子:

"0"=> true

" 0.1 "=> true

"abc"=> false

"1 a"=> false

"2e10"=>true


样例输入:

​2e10​

样例输出:

​1​

思考和详解

本题就是一个模拟题,只要知道什么样的字符串符合题目的要求,直接模拟即可,不过笔者思考了一会,并没有把所有的情况确定出来,以下这份代码只拿到了\(70\)分,若有读者知道笔者疏漏在哪欢迎指出。

具体代码

import java.util.Scanner;

class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str;
boolean isDot = false;
boolean isE = false;
boolean isC = false;
boolean prt = false;
str = in.next();
int len = str.length();
for (int i = 0; i < len; i++) {
char c = str.charAt(i);
if (c >= '0' && c <= '9') {
isC = true;
continue;
} else if (c == '.') {
if (isDot == true || isE == true) {
System.out.println(0);
prt = true;
break;
} else {
isDot = true;
}
} else if (c == 'e') {
if (isE == true || i == 0) {
System.out.println(0);
prt = true;
break;
} else {
isE = true;
}
} else if (c == 'f' && i == len - 1) {
continue;
} else {
System.out.println(0);
prt = true;
break;
}
}
if (!prt) {
System.out.println(1);
}
}
}


T2 数组跳跃

题面

给定一个长度为N的非负的整数数组a[N],初始位置是数组的第一个位置,数组中的每一个数代表你至多能跳多远,如a[i]=3代表可以从a[i]调到a[i+3],判断你能否成功跳到数组的最后一个位置。输入为两行,第一行为数组的长度N,第二行为N个数,输出为0表示失败,1表示成功。

样例输入:

5
2 3 1 1 4


样例输出:

1


样例输入2:

5

3 2 1 0 4


输出:

0


样例解释

如输出所示

思考和详解

本题需要注意​​a[i]​​代指的时可以至多跳到​​a[i+a[i]]​​的位置,而非只能。主干思路就是一个普通的带记忆化的\(BFS\)。

具体代码

import java.util.Scanner;

public class arr {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n + 5];
int[] flag = new int[n + 105];
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
flag[i] = 0;
}
int[] queue = new int[n + 105];
int rear = -1;
int head = 0;
queue[++rear] = 0;
flag[0] = 1;
while (rear >= head) {
int p = queue[head++];
if (p == n - 1) {
System.out.println(1);
System.exit(0);
}
for (int i = 1; i <= arr[p]; i++) {
if (p + i < n && flag[p + i] == 0) {
queue[++rear] = p + i;
flag[p + i] = 1;
}
}
}
System.out.println(0);
}
}


T3跳跃数组2

题面

给定一个长度为N的非负的整数数组a[N],初始位置是数组的第一个位置,数组中的每一个数代表你至多能跳多远,如a[i]=3代表可以从a[i]调到a[i+3],你的目标是以最小的跳跃次数到达终点。输入为两行,第一行为数组的长度N,第二行为N个数,输出为跳跃次数。

样例输入

5
2 3 1 1 4


样例输出

2 


样例解释

\(2->3->4\),两次跳跃

思考与详解

本题和\(T2\)很像,但是有以下两点不同:

  1. 要求输出最小的跳跃次数,并且没有说明如果不能跳到终点怎么办,即,暗示所有给定输入都有解。
  2. 要求输出最小的跳跃次数。

思路和T2差不多,只需要将​​flag​​数组存储内容变成到该位置的最小部属即可,感觉有点像​​dp​​,但是笔者算法比较菜就不卖弄了。

代码如下:

import java.util.Scanner;

public class squareNumber {
public static int n, ct = 0;
public static int[] arr;
public static int ans;

public static void findSquareNumber() {
for (int i = 1; i <= n; i++) {
if (i * i <= n) {
arr[ct++] = i * i;
}
}
}

public static void func(int nowNum, int cnt, int flag) {
if (nowNum > n)
return;
else if (nowNum == n) {
ans = ans < cnt ? ans : cnt;
return;
} else {
for (int i = ct - 1; i >= flag; i--) {
func(nowNum + arr[i], cnt + 1, i);
}
}
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
n = in.nextInt();
in.close();
arr = new int[n + 100];
findSquareNumber();
ans = n + 100;
func(0, 0, 0);

System.out.println(ans);
}
}


百度了一下发现和leetcode 279是一样的题目,考察的知识点是动态规划

这份官方题解给出了不错的思路,我就不再赘述,直接在下面给出本题的完整代码。

具体代码

import java.util.Scanner;

public class arr2 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n + 5];
int[] flag = new int[n + 105];
for (int i = 0; i < n; i++) {
arr[i] = in.nextInt();
flag[i] = 0;
}
int[] queue = new int[n + 105];
int rear = -1;
int head = 0;
queue[++rear] = 0;
flag[0] = 0;
while (rear >= head) {
int p = queue[head++];
if (p == n - 1) {
System.out.println(flag[p]);
System.exit(0);
}
for (int i = 1; i <= arr[p]; i++) {
if (p + i < n && flag[p + i] == 0) {
queue[++rear] = p + i;
flag[p + i] = flag[p] + 1;
}
}
}
System.out.println(0);
}
}