阅读以下说明和Java代码,将应填入 (n) 处的语句或语句成分写在答题纸的对应栏内。
【说明】
某数据文件students.txt的内容为100名学生的学号和成绩,下面的程序将文件中的数据全部读入对象数组,按分数从高到低进行排序后选出排名前30%的学生。
【Java代码】
import java.io.*;
class Student {
private String sNO; //学号
private int Credit; //分数
public int getCredit( ){
return Credit;
}
public String toString() {
return "sNO = " + this.sNO + ", Credit = " + this.Credit;
}
Student(String sNO, int Credit){
(1) = sNO;
(2) = Credit;
}
}
public class SortStudent {
void sort(Student[ ] s) { //Sort the array s[ ] in descending order of
Credit
for (int i = 0; i < s.length-1; i++) {
for (int .j = i+1; j < s.length; j++) {
if (s[i]. (3) < s[j]. (4) ) {
Student tmp = s[i];
s[i] = s[j];
s[j] = tmp;
}
}
}
}
public static void main(String argv[ ]) {
Student[ ] testStudent = new Student[size];
try {
BufferedReader in = new BufferedReader(new FileReader
("students.txt"));
boolean done = false;
int i = 0;
while (!done) {
String s = in.readLine(); //每次读取一个学生的学号和成绩
if (s != null) {
String tmp[ ] = s.split(",");
testStudent[i++] = (5) (tmp[0], Integer.parseInt
(tmp[1]));
} else
done = true;
}
in.close();
(6) = new SortStudent();
ss.sort(testStudent);
System.out.println("top 30%:");
for (int j = 0; j < size * 0.3; j++)
System.out.println(testStudent[j]);
} catch (IOException e) {
System.out.println("io error!");
}catch (NumberFormatException e) {
System.out.println("not a number!");
}
}
(7) int size = 100; //学生总数
}
正确答案及解析
正确答案
解析
(1)this.sNO
(2)this.Credit
(3)getCredit()
(4)getCredit()
(5)new Student
(6)SortStudent ss
(7)static 或static final
首先分析程序的整体结构,本题中定义了两个类:Student和SortStudent,分别用于定义学生和进行排序。类Student的定义中,第一部分为private访问权限的成员,要对其进行访问,需要通过具有相应访问权限的成员函数。
Student类的构造器方法的参数和类中属性同名,需要用this标识符进行区分,因此前面俩空需要用this表明当前Student的属性。
SortStudent类中的成员函数sort和Student类的对象所构成的对象数组根据分数进行排序,而由于在Strdent类中学号和分数为private访问权限,不可从外部直接访问,所以要通过getCredit而得到。
Main函数中需要将学生信息读入,并根据学生信息创建对象并加入对象数组。空(5)为new Student。空(6)处根据其所在的语句右侧new SortStudent以及其后序语句ss.sort的方法调用,可知,需要声明SortStudent类的对象引用ss,所以空(6)处为SortStrdent ss。空(7)处是属性size的类型描述,size是作为作为数组大小的参数,其值不可变,因此是static 或者static final。
你可能感兴趣的试题
一台主机的IP地址为202.123.25.36,掩码为255.255.254.0。如果该主机需要在该网络进行直接广播,那么它应该使用的目的地址为( )
-
- A.202.123.25.0
- B.202.123.25.255
- C.202.123.24.0
- D.202.123.24.255
- 查看答案
在计算机系统的日常维护工作中,应当注意硬盘工作时不能__(2)__。另外,需要防范病毒,而__(3)__是不会被病毒感觉的。
-
- A.电子邮件
- B.硬盘
- C.U盘
- D.ROM
- 查看答案
有 4 个 IP 地址:201.117.15.254、201.117.17.01、201.117.24.5 和 201.117.29.3,如果子网掩码为 255.255.248.0,则这 4 个地址分别属于3个子网;其中属于同一个子网的是()
-
- A.201.117.15.254 和 201.117.17.01
- B.201.117.17.01 和 201.117.24.5
- C.201.117.15.254 和 201.117.29.3
- D.201.117.24.5 和 201.117.29.3
- 查看答案
在异步通信中,每个字符包含1位起始位、7位数据位、1位奇偶位和1位终止位,每秒钟传送200个字符,采用4相位调制,则码元速率为()。
-
- A.50波特
- B.500波特
- C.550波特
- D.1000波特
- 查看答案
在 Windows 中,运行( )命令得到下图所示结果。以下关于该结果的叙述中,错误的是( )。
Pinging 59.74.111.8 with 32 bytes of data:
Reply from 59.74.111.8: bytes=32 time=3ms TTL=60
Reply from 59.74.111.8: bytes=32 time=5ms TTL=60
Reply from 59.74.111.8: bytes=32 time=3ms TTL=60
Reply from 59.74.111.8: bytes=32 time=5ms TTL=60
Ping statistics for 59.74.111.8:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 3ms, Maximum = 5ms, Average = 4ms
-
- A.该命令使得本地主机向目标主机发送了 4 个数据包
- B.本地主机成功收到了目标主机返回的 4 个数据包
- C.本地主机与目标主机连接正常
- D.该命令用于查看目标主机的 IP 地址
- 查看答案