




已阅读5页,还剩9页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
2004年上海市高等学校计算机等级考试试卷二级 (Java程序设计)(本试卷答卷时间为120分钟)试题一(28分,每小题4分)解答下列各小题,把解答写在答卷纸的对应栏内。(1) 选出合法的事件监听者接口A. MouseMotionListener B. WindowListener C. DialogListener D. PaintListener (2) 写出语句int e = Integer.parseInt( + 3 + 3);System.out.println(e - 2.5);执行后的输出结果。(3) HTML文件中有如下语句codebaseclasses 和codehiapplet.HiApplet.class,则HiApplet.class文件应位于:A. HTML文件所在目录/classes B. HTML文件所在目录C. HTML文件所在目录/classes/hiappletD. 根目录(4) 小应用程序的生命期可分为4个阶段: 、启动、停止、 。(5) 选出下列代码的输出结果: class test public static void main(String args) int i = 0,1; try i2 = i0 + i1; catch(ArrayIndexOutOfBoundsException e1) System.out.println(1); catch(Exception e2) System.out.println(2); finally System.out.println(3); System.out.println(4); A1 B2 C3 D4(6) 假设下列代码已经正确编译:public class HelloWorld_App public static void main( String args ) System.out.println(My name is + args1);若在DOS命令窗口中输入以下命令:C: java HelloWorld_App Michael Thomas请写出程序的输出结果。(7) 下列程序运行后,将显示什么?请选择正确的答案。public class example int i = 0; public static void main(String args) int i = 1; change_i(i); System.out.println(i0); public static void change_i(int i) int j = 2; i = j; A. 0 B. 1 C. 2 D. 4 试题二(12分, 每小题6分)阅读下列程序, 把程序的输出结果写在答卷纸的对应栏内。(1)【程序2.1】public class test2_1 public static void main(String args) Thing t1 = new Item(food);System.out.println(t1);Thing t2 = new Item(television);System.out.println(t2);t2.setValue(10);System.out.println(t1);public class Item private String name;private static int value = 0;public Thing(String theName) name = theName;value = value + theName.length();public void setValue(int newValue) value = newValue;public String toString() return value + + name + s;(2)【程序2.2】右图是程序运行开始的界面,写出当第一次单击按钮时,在DOS命令窗口中输出的内容。import java.awt.*;import java.applet.*;import java.awt.event.*;public class Test2_2 extends Applet implements ActionListener Label lOne, lTwo;Button bDoIt;int total = 0;int difference = 0;public void init() lOne = new Label(John);lTwo = new Label(Mary);bDoIt = new Button(Do It!);add(lOne);add(lTwo);add(bDoIt);bDoIt.addActionListener(this);public void actionPerformed(ActionEvent e) int regular = 100;int bonus = 10;System.out.println(pickOne(regular, bonus);addThem(100, 10);System.out.println(total);subtractThem(100, 10);System.out.println(difference);swapLabels(lOne, lTwo);System.out.println(lOne.getText() + + lTwo.getText();public void swapLabels(Label one, Label two) String temp = one.getText();one.setText(two.getText();two.setText(temp);public int pickOne(int i, int j) if (i java test3_1 The sum is: 75【程序3.1】#1 public class test3_1#2 /* A program to add two numbers together */#3 public static void pain(String args) #4 int a = 23;#5 int b = 52;#6 int c = sum(a, b);#7 System.out.println(The sum is: + c);#8 #9 public int sum(int a, int b) #10 return a + b;#11 #12 (2) 下面的小应用程序用于显示meth. jpeg图像文件。 【程序3.2】#1import java.awt.*;#2import java.applet.Applet;#3#4public class LoadImg extends applet #5 private Image img;#6 public void init () #7 img = new Image( getDocumentBase(), meth.jpeg );#8 #9 public void paint( Graphics g ) #10 g.drawImage( img, 0, 0, this );#11 g.drawImage(img, 0, 40, getWidth(), getHeight() 40, this)#12 #13 试题四 (18分, 每小题6分)按指定的要求编写程序段, 把解答写在答卷纸的对应栏内。(1) 编写removeRandChar()方法,该方法有两个参数,类型分别是String和int,第二个参数表示从第一个参数指定的字符串中删除字符的个数。删除哪个字符由随机数决定。如果第二个参数比第一个参数指定的字符串长度大,则该方法返回空字符串。removeRandChar()方法必须包括调用removeSingChar()方法,该方法定义在test4_1应用程序中。例如执行完整的test4_1应用程序两次后,产生如下的输出:C: java test4_1Remove 3 random characters from INTERESTING: INEETINGRemove 6 random characters from INTERESTING: EESTNC: java test4_1Remove 3 random characters from INTERESTING: ITERETINRemove 6 random characters from INTERESTING: IEESG下面是不包括removeRandChar()方法的test4_1应用程序: public class test4_1public static void main(String args) String word1 = removeRandChar(INTERESTING, 3);System.out.println(Remove 3 random characters from INTERESTING: + word1);word1 = removeRandChar(INTERESTING, 6);System.out.println(Remove 6 random characters from INTERESTING: + word1);/*This method removes the character at position: indexNum, from the String: str,and returns the resulting String.*/private static String removeSingChar(String str, int indexNum) return str.substring(0,indexNum) + str.substring(indexNum+1);(2) 编写一个以二维整数数组为参数的addToArrary()方法。该方法返回一个新的二维整数数组。由该方法返回的数组中的每一个元素按以下方法改变:数组的最后一行最后一个元素加1,该行倒数第二个元素加2,倒数第三个元素加3,当最后一行的元素处理完毕后,接着处理倒数第二行的最后一个元素,以此类推,数组中每个元素按此方法增量,直到第一行第一个元素。假设数组至少有一行。例如:如果参数数组如下: +12 +11+9 4256377+598899+4+3+2+1然后通过该方法返回的数组将是:161315151114131412111110public class test4_2 public static void main(String args) int numbers1 = 4, 2, 5, 6,3, 7, 7, 9,8, 8, 9, 9;System.out.println(Array1:);printIntA(numbers1);int numbers2 = addToArray(numbers1);System.out.println(Array2:);printIntA(numbers2);private static void printIntA(int nums) System.out.println();for(int i=0; inums.length; i+) for(int j=0; j other.examMark)System.out.println(name + did better than + );elseSystem.out.println(name + did worse than + );要求完成test4_3的编程,它将创建两个Student对象并调用相应的方法,产生的输出如下所示:C: java test4_3张楠 got 70 in the test and 85 in the exam李浩 got 80 in the test and 90 in the exam李浩s exam mark changed to 40李浩 did worse than 张楠注意不能使用任何System.out.print() 或 System.out.println()语句,产生的输出只需简单地调用所创建的Student对象的对应方法。public class test4_3 public static void main(String args) Student student1; Student student2;试题五(15分)阅读下列问题描述和相应的程序,把应填入其中 (n) 处的内容写在答卷纸的对应栏内。【问题描述】完成test5的源程序。Test5有两个按钮和一个文本框。当Frame第一次出现时如图5.1所示。开始时Test5显示水平方向5个正方形,每个正方形大小为10个象素。文本框显示5。当用户单击“CHANGE”按钮时,正方形系列以相反定位显示,即:如果当前是以水平方向显示,则用户单击“CHANGE”按钮后,正方形系列以垂直方向显示;如果正方形系列当前以垂直方向显示,则用户单击“CHANGE”按钮后,它们以水平方向显示。如图5.2是用户单击“CHANGE”按钮后产生的效果。无论何时,只要用户单击“NEW”,一个随机数量(1至6之间)的正方形系列将按水平或垂直各50%概率显示。图5.3所示是用户某次单击“NEW”按钮的效果。 图5.1 图5.2 图5.3【程序5】import java.awt.*;import java.awt.event.*;public class test5 extends Frame implements ActionListenerprivate static final int MAX_NUMBER_BOXES = 6;private Button newB, changeB;private TextField numBoxesT;private int numBoxes; /the number of boxes currently displayedprivate boolean isHorizontal; /the orientation of the boxespublic test5(String title, int x, int y, int width, int height) setBounds(x, y, width, height); setTitle(title);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)dispose(); System.exit(0););setLayout(new FlowLayout();newB = new Button(NEW);newB.addActionListener(this);add(newB); changeB = new Button(CHANGE); (1) add(changeB);numBoxesT = new TextField(4);numBoxesT.setText(5);add(numBoxesT); (2) numBoxes = 5;show();public void actionPerformed(ActionEvent e) if (e.getSource() = newB) (3) if (Math.random() 0.5)isHorizontal = true;elseisHorizontal = false;numBoxesT.setText(+numBoxes);else if (e.getSource() = changeB) (4) repaint();public void paint(Graphics g) final int SIZE = 10; /size of each box./used for horizontal orientation:final int START_HOR_X = 100; / x value left most squarefinal int HOR_Y = 100; / y value all horizontal squares/used for vertical orientationfinal int START_VERT_Y = 100; / y value highest squarefinal int VERT_X = 100; / x value all vertical squaresif (isHorizontal)for (int i=0; inumBoxes; i+) (5) else for (int i=0; inumBoxes; i+)g.drawRect(VERT_X, START_VERT_Y+SIZE*i, SIZE, SIZE);试题六(15分)阅读下列问题描述和相应的程序,把应填入其中 (n) 处的内容写在答卷纸的对应栏内。【问题描述】当窗口第一次打开时,如图6.1所示,程序窗口中只有一个“CLEAR”按钮。每当单击鼠标,将从窗口的中心位置(100,100)到鼠标位置画一条线,如图6.2所示。 单击鼠标时的位置应存放在一个Point对象数组中。这个数组的大小由常量private final int MAX_LINES = 10;定义。一旦数组放满数据,则不能再画线。换言之,一旦窗口中画了MAX_LINES条线,则再单击鼠标窗口将不会发生任何变化。当单击“CLEAR”按钮,窗口中内容应被清除,恢复初始状态。一旦窗口被清理,用户就能够继续画线。下面的类用于创建窗口:public class test6 public static void main(String args)ManyLines lines = new ManyLines(drawing lines,100, 100, 200, 200); 图 6.1 图 6.2【程序6】import java.awt.*;import java.awt.event.*;public class ManyLines extends Frame implements (1) private Button bCle
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年考试心理辅导师资格考试试卷及答案
- 2025年环境科学实验技能测评卷及答案
- 2025年公共卫生人群健康考试试题及答案
- 2025年公共交通管理考试试题及答案的技巧
- 分数的运算性质及应用题解析:小学数学教案
- 音乐理论作曲与创作技巧测试卷
- 完形填空练习题与答案解析
- 自然的力量环保话题作文(15篇)
- 餐饮服务员考试卷
- 公交之星活动方案
- DF6205电能量采集装置用户手册-2
- 培训课件 -华为铁三角工作法完全解密
- 2024年新版《公文写作与处理》近年考试题库(含答案)
- 水利安全生产风险管控六项机制题库
- 《鲁迅的简介》课件
- 韩国《寄生虫》电影鉴赏解读
- 重症肺炎并呼吸衰竭个案护理查房
- 吸烟有害健康课件高一上学期行为习惯养成教育主题班会
- 中医各家学说(湖南中医药大学)智慧树知到课后章节答案2023年下湖南中医药大学
- 油气井现代产量递减分析方法及应用讲座
- 基于PLC的全自动洗衣机控制系统设计毕业论文
评论
0/150
提交评论