win.add(newButton(labels[i]));
win.pack();
win.setVisible(true);
}
}
【解析】该题目的知识点在于图形用户界面设计中的布局设计,只能采用GridLayout的方
式,不能采用默认的FlowLayout布局方式
2.设计一个applet,加载和显示图象“student.gif”。
图像文件与
包含applet的HTM文件在同一个目录下。
参考程序:
importjava.awt.*;
importjava.applet.*;
publicclassMypictureextendsjava.applet.Applet{
Imageimage;
publicvoidinit(){
image=getlmage(getDocumentBase(),"Student.gif");}
publicvoidpaint(Graphicsg){
g.drawlmage(image,O,O,this);
}
}
【解析】该题要求掌握如何在applet中显示图像文件的编程能力,具体可参照参考教材第十
早。
3.编写removeRandChar()方法,该方法有两个参数,类型分别是String和int,第二个参数表示从第一个参数指定的字符串中删除字符的个数。
删除哪个字符由随机数决定。
如果第二个参数比第一个参数指定的字符串长度大,则该方法返回空字符串。
removeRandChar()方法必须包括调用removesingChar()方法,该方法定义在test4_1应用程序中。
例如执行完整的test4_1应用程序两次后,产生如下的输出:
C:
\>javatest4_1
Remove3randomcharactersfromINTERESTING:
INEETING
Remove6randomcharactersfromINTERESTING:
EESTN
C:
\>javatest4_1
Remove3randomcharactersfromINTERESTING:
ITERETIN
Remove6randomcharactersfromINTERESTING:
IEESG
下面是不包括removeRandChar()方法的test4_1应用程序:
publicclasstest4_1{
publicstaticvoidmain(String[]args){
Stringword1=removeRandChar("INTERESTING",3);
System.out.println("Remove3randomcharactersfromINTERESTING:
"+
word1);
word1=removeRandChar("INTERESTING",6);
System.out.println("Remove6randomcharactersfromINTERESTING:
"+
word1);
}
Thismethodremovesthecharacteratposition:
indexNum,fromtheString:
str,
andreturnstheresultingString.
*/
privatestaticStringremoveSingChar(Stringstr,intindexNum){
returnstr.substring(O,indexNum)+str.substring(indexNum+1);
}
}
参考程序:
页:
2
privatestaticStringremoveRandChar(Stringstr,inthowMany){if(str.length()<=howMany)
return"";
intrandNo;
for(intj=0;jvhowMany;j++){
randNo=(int)(Math.random()*str.length());
str=removeSingChar(str,randNo);
}
returnstr;
}
【解析】该题要求掌握编写方法和调用方法的能力,有返回值的不同编写形式。
4•仔细阅读下面student类的定义,该类的定义可以存放学生的姓名
(name、测验分数(testMark)和考试分数(examMark。
publicclassStudent{
privateStringname;
privateinttestMark;
privateintexamMark;
publicStudent(StringtheName){
name=theName;
}
publicStudent(StringtheName,inttest,intexam){
name=theName;
testMark=test;examMark=exam;
}
publicvoidsetExamMark(intexam){examMark=exam;
System.out.println(name+"\'sexammarkchangedto"+examMark);
}
publicintgetTestMark(){
returntestMark;
}
publicvoiddisplaylnfo(){
System.out.println(name+"got"+testMark+"in
thetestand"+examMark+"intheexam");
}
publicvoidcompareTo(Studentother){
if(examMark>other.examMark)
System.out.println(name+"didbetterthan"+other.name);
else
System.out.println(name+"didworsethan"+other.name);
}
}
要求完成test4_3的编程,它将创建两个Student对象并调用相应的方法,产生的输出如下所示:
C:
\>javatest4_3
张楠got70inthetestand85intheexam
李浩got80inthetestand90intheexam
李浩'sexammarkchangedto40
李浩didworsethan张楠
注意不能使用任何System.out.print()或System.out.println()语句,产生的输出只需简单地调用所创建的Student对象的对应方法。
publicclasstest4_3{
publicstaticvoidmain(String[]args){
Studentstudent1;
Studentstudent2;
}
}
参考程序:
页:
5
student1=newStudent(”张楠",70,85);
student2=newStudent("李浩",80,90);
student1.displaylnfo();
student2.displaylnfo();
student2.setExamMark(40);
studepareTo(student1);
对象的声明与创建,以及根据需
【解析】该题考核的重点是面向对象程序设计的基本应用:
求调用实例方法。
5.编写change()方法,该方法有一个参数,类型为int,通过方法,计算并输出由给定参数(元)的人民币兑换成一元、两元、五元的所有方案。
例如当用户输入10,执行Test4_1应用程序后,产生如下的输出:
importjava.io.*;
publicclassTest4_1{
publicstaticvoidmain(Stringargs[])throwsIOException{intmoney;
Stringstr;
BufferedReaderbuf;
buf=newBufferedReader(newInputStreamReader(System.in));
System.out.print("lnputaninteger:
");str=buf.readLine();
money=lnteger.parseInt(str);
if(money>=5&&money<=100)
change(money);
}publicstaticvoidchange(intnum)
{
}
}
参考程序:
intr1,r2,r5;
for(r5=0;r5<=num/5;r5++)
for(r2=0;r2<=num/2;r2++)
{r1=num-r5*5-r2*2;
if(r1>=0)
System.out.println("5:
"+r5+""+"2:
"+r2+""+"1:
"+r1);
【解析】这是一个考核循环逻辑的编程题。
6.编写一个以字符串数组为参数的uniqueStrings()方法。
该方法将打印数组中所有的字符串。
若数组中有相同的字符串,则相同的字符串只打印一次。
例如:
执行下列语句
String[]words={"one","two","two","three","three","three","one"};uniqueStrings(words);
将输出如下:
onetwothree
参考程序:
privatestaticvoiduniqueStrings(String[]words){
for(inti=0;ibooleanunique=true;
for(intj=0;j
if(words[i].equals(words[j]))
unique=false;
}
if(unique)
System.out.println(words[i]);
}
}
【解析】注意形参是数组的编程,以及字符串的比较要使用equals方法
7.现有一段30米长的木材,每天锯去其中的一半,编程序计算并输出多少天后,木材的长度开始小于0.05米。
参考程序:
publicclassTest4_1{
publicstaticvoidmain(Stringargs[]){
intd=0;
floatm=30;
while(m>=0.05){
m=m/2;
d++;
System.out.print(d+":
");//可选
System.out.println(m);//可选
}
System.out.print("Youneed"+d+"days");
}
}
【解析】该题的循环只能使用while和dowhile语句
&完成下面的程序:
编写带两个参数,返回值为字符串的方法
randomLetters(Stringword,inthowMany)。
该方法实现从字符串
中随机获取所需数量的字符,并按获取的次序以大写字符串返回。
第
一个参数代表给定的字符串,第二个参数确定字符个数。
(假定调用
方法时字符串参数不为空,整型参数不为负)
执行完整的程序后:
publicclassTest4_2{
publicstaticvoidmain(String[]args){
System.out.println(randomLetters("LOVELY",3));
System.out.println(randomLetters("LOVELY",3));
System.out.println(randomLetters("LOVELY",4));
System.out.println(randomLetters("zhang",1));
System.out.println(randomLetters("zhang",2));
}
privatestaticStringrandomLetters(Stringword,inthowMany){
其中一种输出如下:
LYO
VLO
OOLE
Z
GH
参考程序:
privatestaticStringrandomLetters(Stringword,inthowMany){StringoutWord="";
charc;
intrandom;
word=word.toUpperCase();
for(inti=0;irandom=(int)(Math.random()*word」ength());
c=word.charAt(random);
outWord=outWord+c;
}
returnoutWord;
}
9.完成下面的程序:
编写一个参数为二维整数数组、返回值为整数的方法countOdds。
该方法计算二维数组中奇数的个数并返回。
下面的例子是一个数组的定义并调用该方法及输出对应的结果。
inttwoArray[][]={{5,7,8,22,47},{42,75,58,21,36}};
System.out.println("Odds:
"+countOdds(twoArray));
以上程序的结果是:
Odds:
5
参考程序:
privatestaticintcountOdds(int[][]theInts){
intnumOdds=0;
for(inti=0;ifor(intj=0;jif(thelnts[i][j]%2!
=0)numOdds++;
}
}
returnnumOdds;
}
【解析】要注意两维数组中不同维调用length的差别
10.在下图所示的小应用程序用户界面上有两个标签(Label)、一个
列表框(Choice)和一个文本区域(TextArea)。
程序实现以下功能:
一旦用户在列表框中(单击鼠标)选择了某书名选项,该书名将被立即显示在文本区域中。
请完成该程序的init()方法以实现程序的功
importjava.awt.*;
importjava.awt.event.*;
importjava.applet.*;
publicclassMyChoiceextendsApplet{
String[]bookstore={"Java程序设计",”C++程序设计","网络基础",
"计算机应用基础",”VisualBasic程序设计","动画设计基础
"};
Labella仁newLabel("请选择你要的书籍:
");
LabelIa2=newLabel("你的选择是:
");
TextAreat=newTextArea(5,30);
Choicec=newChoice();
publicvoidinit(){
所需要编写的程序块
……//
classCLimplementsItemListener{
publicvoiditemStateChanged(ltemEvente){t.append(c.getSelectedltem()+"\n");}
}
}
参考程序:
(该语句任选)
t.setEditable(false);
for(inti=0;i<6;i++)
c.addltem(bookstore[i]);add(la1);
add(c);
add(la2);
add(t);
c.addltemListener(newCL());
【解析】该题的重点是认清applet中的init方法的基本作用:
对相关内容进行初始化。
该题是对组件进行初始化:
添加组件,设置侦听能力
11.从“素材库”中取出StringStrip.java文件,在该文件基础上进行补充,完成其中方法staticStringstrip(Strings,charc)的编写。
该方法的功能是:
将指定的字母从给定的字符串中所有出现的地方移去,其中参数s是给定的字符串,参数c是指定的字母。
要求用循环语句实现。
下图中给出了一个从给定字符串“ABCADaAADFaRAG中移
去‘A字符后为“BCDaDFaRG的演示结果)。
程序完成后将程序编辑及运行所需要的所有文件(包括源程序文件、字节码文件)存放到C:
\KS目录下。
参考程序:
staticStringstrip(Strings,charc){
intn=s.length();
Stringa="";
inti=0;
while(icharsc=s.charAt(i);
if(sc==c)i++;
else{a=a+sc;i++;};
}
returna;
}
【解析】从2008年开始Java等级考试采用了上机形式。
因此参加考试的同学需要从指定的文件夹中取出指定的文件进行编程。
希望同学打开文件后马上存放到另一个指定文件夹如本题要求的C:
\KS文件夹中,以免你的修改影响到原始的文件,有利于你需要时还可以取用最原始的文件。
原始文件中已存放了一些文件编程所需要的非考核重点的程序段,内容有多,有
少,便于程序调试。
12.从“素材库”中取出Division.java文件,在该文件基础上进行补充,完成简单除法计算器(只做整数除法,结果也取整数)的小应用程序编写。
运行结果见图1,显示结果的文本域设置为不可编辑。
当除数为0时,能抛出ArithmeticException异常,并在捕获异常处对异常进行处理,异常处理要求在屏幕上显示“除数不能为0,请重新输入”,结果见图2。
程序完成后将程序编辑及运行所需要的所有文件(包括源程序文件、字节码文件及html文件)存放到C:
\KS目录下。
图1
图2
参考程序:
importjava.applet.*;
importjava.awt.*;
importjava.awt.event.*;
publicclassDivisionextendsAppletimplementsActionListener{
Labell1,l2,E;
TextFieldinput1,input2,output1;
inta,b,c;
Panelp=newPanel();
publicvoidinit(){
setLayout(newBorderLayout());
input1=newTextField(5);
I1=newLabel("十");
input2=newTextField(5);
l2=newLabel("=");
output1=newTextField(9);
E=newLabel();
E.setAlignment(Label.CENTER);
E.setForeground(Color.red);
p.add(input1);
p.add(l1);
p.add(input2);
p.add(l2);
p.add(output";
add("North",p);
add("Center",E);
input2.addActionListener(this);
output1.setEditable(false);
}
publicvoidactionPerformed(ActionEvente){
a=lnteger.parseInt(input1.getText());
b=lnteger.parseInt(input2.getText());
E.setText(””);
try{
c=a/b;
output1.setText(Integer.toString(c));
}
catch(ArithmeticExceptione1){
StringTx="除数不能为0,请重新输入";
E.setText(Tx);
output1.setText("");
}
}
}
【解析】以上参考程序中非加粗的内容就是原始文件所提供的。
该题的知识点有两部分:
组
件的设置,即界面的设计,以及事件的编程
13.从素材库"中取出Test5.java文件,在该文件基础上进行补充,完成所需内容的编写。
该applet程序中,窗口区有一个"变色"按钮,程序初始状态见下图1,在中心位置有一个半径为30的蓝色圆。
(14分)
当单击窗口的任意位置,在鼠标位置将显示一个圆,其颜色和大小随机而变,但半径不能超过150,见下图2;(10分)
当单击”变色”按钮则圆的颜色也会随机变化。
颜色有Color.red、
Color.blue、Color.yellow。
(10分)
以下图示是用Java开发包直接运行的显示效果,仅作参考。
可根据需要在素材库"中选取Test5.html文件。
程序完成后将程序编辑及运行所需要的所有文件(包括源程序文件、字节码文件)存放到C: