上海市计算机等级考试二级(Java)-程序调试与程序填空.doc

上传人:j****9 文档编号:2133133 上传时间:2019-04-30 格式:DOC 页数:14 大小:122KB
下载 相关 举报
上海市计算机等级考试二级(Java)-程序调试与程序填空.doc_第1页
第1页 / 共14页
上海市计算机等级考试二级(Java)-程序调试与程序填空.doc_第2页
第2页 / 共14页
上海市计算机等级考试二级(Java)-程序调试与程序填空.doc_第3页
第3页 / 共14页
上海市计算机等级考试二级(Java)-程序调试与程序填空.doc_第4页
第4页 / 共14页
上海市计算机等级考试二级(Java)-程序调试与程序填空.doc_第5页
第5页 / 共14页
点击查看更多>>
资源描述

1、编程题按指定的要求编写程序段,1编写一个程序,把六个按钮分别标识为A至F ,并排列成一行。参考程序:import java.awt.*;public class MyClass public static void main(String args) String labels = “A“,“B“,“C“,“D“,“E“,“F“;Window win = new Frame();win.setLayout(new GridLayout(1,6);for(int i=0;i java test4_1Remove 3 random characters from INTERESTING: INEE

2、TINGRemove 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(“

3、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 return

4、s the resulting String.*/private static String removeSingChar(String str, int indexNum) return str.substring(0,indexNum) + str.substring(indexNum+1);参考程序:页:2private static String removeRandChar(String str, int howMany)if (str.length() other.examMark)System.out.println(name + “ did better than “ + ot

5、her.name);elseSystem.out.println(name + “ did worse than “ + other.name);要求完成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.

6、out.println()语句,产生的输出只需简单地调用所创建的Student对象的对应方法。public class test4_3 public static void main(String args) Student student1; Student student2;参考程序:页:5student1 = new Student(“张楠“, 70, 85);student2 = new Student(“李浩“, 80, 90);student1.displayInfo();student2.displayInfo();student2.setExamMark(40);pareTo(

7、student1);【解析】该题考核的重点是面向对象程序设计的基本应用:对象的声明与创建,以及根据需求调用实例方法。5编写change( ) 方法,该方法有一个参数,类型为int,通过方法,计算并输出由给定参数(元)的人民币兑换成一元、两元、五元的所有方案。例如当用户输入10,执行Test4_1应用程序后,产生如下的输出:import java.io.*;public class Test4_1public static void main(String args) throws IOException int money;String str;BufferedReader buf;buf=n

8、ew BufferedReader(new InputStreamReader(System.in);System.out.print(“Input an integer:“);str=buf.readLine(); money=Integer.parseInt(str); if (money=5 【解析】这是一个考核循环逻辑的编程题。6编写一个以字符串数组为参数的uniqueStrings()方法。该方法将打印数组中所有的字符串。若数组中有相同的字符串,则相同的字符串只打印一次。例如:执行下列语句 String words = “one“,“two“,“two“,“three“,“three

9、“,“three“,“one“;uniqueStrings(words);将输出如下:onetwothree参考程序:private static void uniqueStrings(String words) for (int i = 0; i =0.05) m=m/2;d+;System.out.print(d+“: “); /可选System.out.println(m); /可选System.out.print(“You need “+d+“ days“); 【解析】该题的循环只能使用 while 和 do while 语句8完成下面的程序:编写带两个参数,返回值为字符串的方法ran

10、domLetters(String word, int howMany)。该方法实现从字符串中随机获取所需数量的字符,并按获取的次序以大写字符串返回。第一个参数代表给定的字符串,第二个参数确定字符个数。 (假定调用方法时字符串参数不为空,整型参数不为负)执行完整的程序后:public class Test4_2 public static void main(String args) System.out.println(randomLetters(“LOVELY“,3);System.out.println(randomLetters(“LOVELY“,3);System.out.print

11、ln(randomLetters(“LOVELY“,4);System.out.println(randomLetters(“zhang“,1);System.out.println(randomLetters(“zhang“,2);private static String randomLetters ( String word, int howMany )其中一种输出如下:LYOVLOOOLEZGH参考程序:private static String randomLetters ( String word, int howMany )String outWord = “;char c;in

12、t random;word = word.toUpperCase();for( int i=0; ihowMany; i+ ) random = (int)( Math.random()*word.length() );c = word.charAt( random );outWord = outWord + c;return outWord;9完成下面的程序:编写一个参数为二维整数数组、返回值为整数的方法countOdds。该方法计算二维数组中奇数的个数并返回。下面的例子是一个数组的定义并调用该方法及输出对应的结果。int twoArray = 5, 7, 8, 22, 47, 42, 75

13、, 58, 21, 36; System.out.println(“Odds: “+countOdds(twoArray);以上程序的结果是:Odds: 5 参考程序:private static int countOdds(int theInts)int numOdds = 0;for(int i=0; i theInts.length; i+)for(int j=0; jtheIntsi.length; j+)if (theIntsij % 2 != 0)numOdds+;return numOdds;【解析】要注意两维数组中不同维调用 length 的差别10在下图所示的小应用程序用户界

14、面上有两个标签(Label) 、一个列表框(Choice)和一个文本区域(TextArea) 。程序实现以下功能:一旦用户在列表框中(单击鼠标)选择了某书名选项,该书名将被立即显示在文本区域中。请完成该程序的init()方法以实现程序的功能。import java.awt.*;import java.awt.event.*;import java.applet.*;public class MyChoice extends Applet String bookstore = “Java 程序设计“,“ C+程序设计“,“网络基础“,“计算机应用基础“,“Visual Basic 程序设计“,“

15、动画设计基础“ ;Label la1=new Label(“请选择你要的书籍 :“);Label la2=new Label(“你的选择是 :“);TextArea t=new TextArea(5,30);Choice c=new Choice(); public void init() / 所需要编写的程序块class CL implements ItemListener public void itemStateChanged(ItemEvent e) t.append(c.getSelectedItem()+“n“); 参考程序:t.setEditable(false); (该语句任选

16、) for(int i = 0; i 6; i+) c.addItem(bookstorei); add(la1);add(c); add(la2); add(t); c.addItemListener(new CL();【解析】该题的重点是认清 applet 中的 init 方法的基本作用:对相关内容进行初始化。该题是对组件进行初始化:添加组件,设置侦听能力11. 从“C:素材库”中取出StringStrip.java文件,在该文件基础上进行补充,完成其中方法static String strip(String s,char c)的编写。该方法的功能是:将指定的字母从给定的字符串中所有出现的地方移去,其中参数s是给定的字符串,参数c是指定的字母。要求用循环语句实现。下图中给出了一个从给定字符串“ABCADaAADFaRAGA”中移去A字符后为“BCDaDFaRG”的演示结果) 。程序完成后将程序编辑及运行所需要的所有文件(包括源程序文件、字节码文件)存放到C:KS目录下。参考程序:static String strip(String s,char c)int n=s.length();String a=“;int i=0;while(in)char sc=s.charAt(i);if(sc=c) i+;else a=a+sc;i+;return a;

展开阅读全文
相关资源
相关搜索
资源标签

当前位置:首页 > 实用文档资料库 > 策划方案

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。