[说明]
某数据文件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; //学生总数
(7)处应填()。
参考答案:static或static final
解析:
本题考查Java语言程序设计的能力,涉及类、对象、方法、访问控制、对象数组的定义和相关操作,以及对象数组的排序算法。要求考生根据给出的案例和执行过程说明,认真阅读理清程序思路,然后完成题目。
首先分析程序的整体结构。本题中定义了两个类:Student和SortStudent,分别用于定义学生类和进行排序。类Student的定义中,属性sNO和Credit均定义为private访问权限,要对其进行访问,需要通过具有相应访问权限的方法。方法getCredit和toString均为public。Java中的方法参数如果与类定义中的属性同名,则需通过this关键字进行区分。类SortStudent中定义了方法sort,其功能是对参数为Student对象数组根据分数进行排序。考察main方法。理清程序整体过程:创建对象数组,读取学生文件,将学生的学号和成绩读入,创建Student对象并加入对象数组,将对象数组排序,然后输出top30%的学生信息。
Student类的构造器方法的参数和类中属性同名,需要用this标识符进行区分,因此空(1)和(2)需要用this表明是当前Student的属性。
SortStudent类中定义的方法sort对Student类的对象所构成的对象数组根据分数进行排序,而由于在Student类中学号和分数为private访问权限,不可从外部直接访问,所以需要通过getCredit而得到。因此,空(3)和空(4)处均为getCredit函数调用获得分数。
main函数中需要创建Student类型的对象数组,将学生信息读入,并根据学生信息创建对象并加入对象数组。由于使用SortStudent类中的属性size作为数组大小的参数,因此其值不可变,即空(7)处为static或者static final。空(5)处是创建对象数组中的Student类型的对象元素,Java中对象的创建方式为采用new关键字,因此空(5)处为new Student。空(6)处根据其所在语句右侧new SortStudent以及其后续语句ss.sort的方法调用,可知,需要声明SortStudent类的对象引用ss,所以空(6)处为SortStudent ss。