上海市计算机等级考试二级Java程序调试与程序填空Word文档格式.docx

上传人:b****6 文档编号:21625748 上传时间:2023-01-31 格式:DOCX 页数:17 大小:81.31KB
下载 相关 举报
上海市计算机等级考试二级Java程序调试与程序填空Word文档格式.docx_第1页
第1页 / 共17页
上海市计算机等级考试二级Java程序调试与程序填空Word文档格式.docx_第2页
第2页 / 共17页
上海市计算机等级考试二级Java程序调试与程序填空Word文档格式.docx_第3页
第3页 / 共17页
上海市计算机等级考试二级Java程序调试与程序填空Word文档格式.docx_第4页
第4页 / 共17页
上海市计算机等级考试二级Java程序调试与程序填空Word文档格式.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

上海市计算机等级考试二级Java程序调试与程序填空Word文档格式.docx

《上海市计算机等级考试二级Java程序调试与程序填空Word文档格式.docx》由会员分享,可在线阅读,更多相关《上海市计算机等级考试二级Java程序调试与程序填空Word文档格式.docx(17页珍藏版)》请在冰豆网上搜索。

上海市计算机等级考试二级Java程序调试与程序填空Word文档格式.docx

g。

drawImage(image,0,0,this);

【解析】该题要求掌握如何在applet中显示图像文件的编程能力,具体可参照参考教材第十章.

3。

编写removeRandChar()方法,该方法有两个参数,类型分别是String和int,第二个参数表示从第一个参数指定的字符串中删除字符的个数.删除哪个字符由随机数决定。

如果第二个参数比第一个参数指定的字符串长度大,则该方法返回空字符串。

removeRandChar()方法必须包括调用removeSingChar()方法,该方法定义在test4_1应用程序中。

例如执行完整的test4_1应用程序两次后,产生如下的输出:

C:

\>

javatest4_1

Remove3randomcharactersfromINTERESTING:

INEETING

Remove6randomcharactersfromINTERESTING:

EESTN

ITERETIN

IEESG

下面是不包括removeRandChar()方法的test4_1应用程序:

publicclasstest4_1{

publicstaticvoidmain(String[]args){

Stringword1=removeRandChar("

INTERESTING”,3);

System。

out。

println(”Remove3randomcharactersfromINTERESTING:

"

+word1);

word1=removeRandChar(”INTERESTING”,6);

out.println(”Remove6randomcharactersfromINTERESTING:

”+word1);

/**

Thismethodremovesthecharacteratposition:

indexNum,fromtheString:

str,

andreturnstheresultingString。

*/

privatestaticStringremoveSingChar(Stringstr,intindexNum){

returnstr。

substring(0,indexNum)+str。

substring(indexNum+1);

}

……

privatestaticStringremoveRandChar(Stringstr,inthowMany){

if(str。

length()<

=howMany)

return"

”;

intrandNo;

for(intj=0;

j〈howMany;

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){

testMark=test;

examMark=exam;

publicvoidsetExamMark(intexam){

println(name+"

\’sexammarkchangedto"

+examMark);

publicintgetTestMark(){

returntestMark;

publicvoiddisplayInfo(){

System.out.println(name+”got”+testMark+"

in

thetestand"

+examMark+"

intheexam”);

publicvoidcompareTo(Studentother){

if(examMark〉other。

examMark)

println(name+”didbetterthan"

+other。

name);

else

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{

Studentstudent1;

Studentstudent2;

student1=newStudent(”张楠"

,70,85);

student2=newStudent("

李浩"

80,90);

student1。

displayInfo();

student2。

setExamMark(40);

compareTo(student1);

【解析】该题考核的重点是面向对象程序设计的基本应用:

对象的声明与创建,以及根据需求调用实例方法。

5.编写change()方法,该方法有一个参数,类型为int,通过方法,计算并输出由给定参数(元)的人民币兑换成一元、两元、五元的所有方案。

例如当用户输入10,执行Test4_1应用程序后,产生如下的输出:

importjava.io.*;

publicclassTest4_1{

publicstaticvoidmain(Stringargs[])throwsIOException{

intmoney;

Stringstr;

BufferedReaderbuf;

buf=newBufferedReader(newInputStreamReader(System。

in));

System。

print(”Inputaninteger:

”);

str=buf。

readLine();

money=Integer.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)

println("

5:

"

+r5+"

+”2:

”+r2+"

”+”1:

”+r1);

【解析】这是一个考核循环逻辑的编程题.

6.编写一个以字符串数组为参数的uniqueStrings()方法。

该方法将打印数组中所有的字符串.若数组中有相同的字符串,则相同的字符串只打印一次。

例如:

执行下列语句

String[]words={”one"

”two"

two”,”three”,”three"

three”,"

one”};

uniqueStrings(words);

将输出如下:

one

two

three

privatestaticvoiduniqueStrings(String[]words){

for(inti=0;

i<

words.length;

i++){

booleanunique=true;

for(intj=0;

j〈i;

j++){

if(words[i]。

equals(words[j]))

unique=false;

if(unique)

println(words[i]);

【解析】注意形参是数组的编程,以及字符串的比较要使用equals方法

7.现有一段30米长的木材,每天锯去其中的一半,编程序计算并输出多少天后,木材的长度开始小于0。

05米。

publicstaticvoidmain(Stringargs[]){

intd=0;

floatm=30;

while(m〉=0.05){

m=m/2;

d++;

out.print(d+”:

”);

//可选

System.out。

println(m);

//可选

print(”Youneed”+d+”days”);

【解析】该题的循环只能使用while和dowhile语句

8.完成下面的程序:

编写带两个参数,返回值为字符串的方法randomLetters(Stringword,inthowMany)。

该方法实现从字符串中随机获取所需数量的字符,并按获取的次序以大写字符串返回。

第一个参数代表给定的字符串,第二个参数确定字符个数。

(假定调用方法时字符串参数不为空,整型参数不为负)

执行完整的程序后:

publicclassTest4_2{

publicstaticvoidmain(String[]args){

println(randomLetters("

LOVELY”,3));

out.println(randomLetters(”LOVELY”,3));

System.out。

println(randomLetters(”LOVELY"

,4));

zhang”,1));

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;

i〈howMany;

i++){

random=(int)(Math。

random()*word。

length());

c=word。

charAt(random);

outWord=outWord+c;

returnoutWord;

9.完成下面的程序:

编写一个参数为二维整数数组、返回值为整数的方法countOdds。

该方法计算二维数组中奇数的个数并返回.下面的例子是一个数组的定义并调用该方法及输出对应的结果。

inttwoArray[][]={{5,7,8,22,47},{42,75,58,21,36}};

out.println(”Odds:

+countOdds(twoArray));

以上程序的结果是:

Odds:

5

privatestaticintcountOdds(int[][]theInts){

intnumOdds=0;

for(inti=0;

i<

theInts.length;

i++){

for(intj=0;

j〈theInts[i]。

length;

if(theInts[i][j]%2!

=0)

numOdds++;

returnnumOdds;

【解析】要注意两维数组中不同维调用length的差别

10.在下图所示的小应用程序用户界面上有两个标签(Label)、一个列表框(Choice)和一个文本区域(TextArea)。

程序实现以下功能:

一旦用户在列表框中(单击鼠标)选择了某书名选项,该书名将被立即显示在文本区域中。

请完成该程序的init()方法以实现程序的功能。

 

awt。

*;

event.*;

importjava.applet.*;

publicclassMyChoiceextendsApplet{

String[]bookstore={”Java程序设计”,"

C++程序设计"

,”网络基础"

”计算机应用基础”,"

VisualBasic程序设计"

"

动画设计基础”};

Labella1=newLabel(”请选择你要的书籍:

”);

Labella2=newLabel("

你的选择是:

TextAreat=newTextArea(5,30);

Choicec=newChoice();

publicvoidinit(){

……//所需要编写的程序块

classCLimplementsItemListener{

publicvoiditemStateChanged(ItemEvente){t。

append(c。

getSelectedItem()+”\n"

);

t。

setEditable(false);

(该语句任选)

for(inti=0;

i〈6;

i++)

c。

addItem(bookstore[i]);

add(la1);

add(c);

add(la2);

add(t);

c.addItemListener(newCL());

【解析】该题的重点是认清applet中的init方法的基本作用:

对相关内容进行初始化。

该题是对组件进行初始化:

添加组件,设置侦听能力

11。

从“C:

\素材库”中取出StringStrip。

java文件,在该文件基础上进行补充,完成其中方法staticStringstrip(Strings,charc)的编写。

该方法的功能是:

将指定的字母从给定的字符串中所有出现的地方移去,其中参数s是给定的字符串,参数c是指定的字母。

要求用循环语句实现。

下图中给出了一个从给定字符串“ABCADaAADFaRAGA”中移去‘A’字符后为“BCDaDFaRG”的演示结果)。

程序完成后将程序编辑及运行所需要的所有文件(包括源程序文件、字节码文件)存放到C:

\KS目录下。

staticStringstrip(Strings,charc){

intn=s。

length();

Stringa=”"

;

inti=0;

while(i〈n){

charsc=s。

charAt(i);

if(sc==c)i++;

else{a=a+sc;

i++;

};

returna;

【解析】从2008年开始Java等级考试采用了上机形式。

因此参加考试的同学需要从指定的文件夹中取出指定的文件进行编程。

希望同学打开文件后马上存放到另一个指定文件夹如本题要求的C:

\KS文件夹中,以免你的修改影响到原始的文件,有利于你需要时还可以取用最原始的文件.原始文件中已存放了一些文件编程所需要的非考核重点的程序段,内容有多,有少,便于程序调试.

12。

从“C:

\素材库”中取出Division.java文件,在该文件基础上进行补充,完成简单除法计算器(只做整数除法,结果也取整数)的小应用程序编写。

运行结果见图1,显示结果的文本域设置为不可编辑。

当除数为0时,能抛出ArithmeticException异常,并在捕获异常处对异常进行处理,异常处理要求在屏幕上显示“除数不能为0,请重新输入"

,结果见图2.

程序完成后将程序编辑及运行所需要的所有文件(包括源程序文件、字节码文件及html文件)存放到C:

图1

图2

*;

importjava.awt.event。

publicclassDivisionextendsAppletimplementsActionListener{

Labell1,l2,E;

TextFieldinput1,input2,output1;

inta,b,c;

Panelp=newPanel();

publicvoidinit(){

setLayout(newBorderLayout());

input1=newTextField(5);

l1=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);

add(input2);

add(l2);

p.add(output1);

add("

North”,p);

add(”Center”,E);

input2。

addActionListener(this);

output1。

publicvoidactionPerformed(ActionEvente){

a=Integer.parseInt(input1.getText());

b=Integer。

parseInt(input2。

getText());

setText(””);

try{

c=a/b;

output1.setText(Integer。

toString(c));

catch(ArithmeticExceptione1){

StringTx=”除数不能为0,请重新输入”;

E.setText(Tx);

output1.setText(”"

}

【解析】以上参考程序中非加粗的内容就是原始文件所提供的。

该题的知识点有两部分:

组件的设置,即界面的设计,以及事件的编程

13.从”C:

\素材库”中取出Test5.java文件,在该文件基础上进行补充,完成所需内容的编写。

该applet程序中,窗口区有一个”变色”按钮,程序初始状态见下图1,在中心位置有一个半径为30的蓝色圆。

(14分)

当单击窗口的任意位置,在鼠标位置将显示一个圆,其颜色和大小随机而变,但半径不能超过150,见下图2;

(10分)

当单击”变色”按钮则圆的颜色也会随机变化.颜色有Color.red、Color.blue、Color.yellow。

以下图示是用Java开发包直接运行的显示效果,仅作参考。

可根据需要在”C:

\素材库”中选取Test5。

html文件.

(2分)

图1图2

event。

*;

importjava.applet。

App

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销 > 经济市场

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1