资源描述
编程题
按指定的要求编写程序段,
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 < labels.length;i++)
win.add(new Button(labels[i]));
win.pack();
win.setVisible(true);
}
}
【解析】该题目的知识点在于图形用户界面设计中的布局设计,只能采用GridLayout的方式,不能采用默认的FlowLayout布局方式
2.设计一个applet,加载和显示图象“student.gif”。图像文件与包含applet的HTML文件在同一个目录下。
参考程序:
import java.awt.*;
import java.applet.*;
public class Mypicture extends java.applet.Applet{
Image image;
public void init (){
image=getImage(getDocumentBase(),"Student.gif");
}
public void paint(Graphics g){
g.drawImage(image,0,0, this );
}
}
【解析】该题要求掌握如何在applet中显示图像文件的编程能力,具体可参照参考教材第十章。
3. 编写removeRandChar()方法,该方法有两个参数,类型分别是String和int,第二个参数表示从第一个参数指定的字符串中删除字符的个数。删除哪个字符由随机数决定。如果第二个参数比第一个参数指定的字符串长度大,则该方法返回空字符串。
removeRandChar()方法必须包括调用removeSingChar()方法,该方法定义在test4_1应用程序中。
例如执行完整的test4_1应用程序两次后,产生如下的输出:
C:\> java test4_1
Remove 3 random characters from INTERESTING: INEETING
Remove 6 random characters from INTERESTING: EESTN
C:\> java test4_1
Remove 3 random characters from INTERESTING: ITERETIN
Remove 6 random characters from INTERESTING: IEESG
下面是不包括removeRandChar()方法的test4_1应用程序:
public class test4_1{
public 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
private static String removeRandChar(String str, int howMany){
if (str.length() <= howMany)
return "";
int randNo;
for(int j=0; j<howMany; j++){
randNo = (int) (Math.random() * str.length());
str = removeSingChar(str, randNo);
}
return str;
}
【解析】该题要求掌握编写方法和调用方法的能力,要注意形参的使用,以及有返回值和没有返回值的不同编写形式。
4.仔细阅读下面student类的定义,该类的定义可以存放学生的姓名(name)、测验分数(testMark)和考试分数(examMark)。
public class Student {
private String name;
private int testMark;
private int examMark;
public Student(String theName) {
name = theName;
}
public Student(String theName, int test, int exam) {
name = theName;
testMark = test;
examMark = exam;
}
public void setExamMark(int exam) {
examMark = exam;
System.out.println(name + "\'s exam mark changed to " + examMark);
}
public int getTestMark() {
return testMark;
}
public void displayInfo() {
System.out.println(name + " got " + testMark + " in
the test and " + examMark + " in the exam");
}
public void compareTo(Student other) {
if (examMark > other.examMark)
System.out.println(name + " did better than " + other.name);
else
System.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.out.println()语句,产生的输出只需简单地调用所创建的Student对象的对应方法。
public class test4_3 {
public static void main(String[] args) {
Student student1;
Student student2;
……
}
}
参考程序:
页:5
student1 = new Student("张楠", 70, 85);
student2 = new Student("李浩", 80, 90);
student1.displayInfo();
student2.displayInfo();
student2.setExamMark(40);
pareTo(student1);
【解析】该题考核的重点是面向对象程序设计的基本应用:对象的声明与创建,以及根据需求调用实例方法。
5.编写change( ) 方法,该方法有一个参数,类型为int,通过方法,计算并输出由给定参数(元)的人民币兑换成一元、两元、五元的所有方案。
例如当用户输入10,执行Test4_1应用程序后,产生如下的输出:
import java.io.*;
public class Test4_1{
public static void main(String args[]) throws IOException {
int money;
String str;
BufferedReader buf;
buf=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Input an integer:");
str=buf.readLine();
money=Integer.parseInt(str);
if (money>=5 && money<=100)
change(money);
}
public static void change(int num)
{
…………
}
}
参考程序:
int r1,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);
将输出如下:
one
two
three
参考程序:
private static void uniqueStrings(String[] words) {
for (int i = 0; i < words.length; i++) {
boolean unique = true;
for (int j = 0; j < i; j++) {
if (words[i].equals(words[j]))
unique = false;
}
if (unique)
System.out.println(words[i]);
}
}
【解析】注意形参是数组的编程,以及字符串的比较要使用equals方法
7.现有一段30米长的木材,每天锯去其中的一半,编程序计算并输出多少天后,木材的长度开始小于0.05米。
参考程序:
public class Test4_1{
public static void main(String args[]){
int d=0;
float m=30;
while (m>=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.完成下面的程序:编写带两个参数,返回值为字符串的方法randomLetters(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.println(randomLetters("LOVELY",4));
System.out.println(randomLetters("zhang",1));
System.out.println(randomLetters("zhang",2));
}
private static String randomLetters ( String word, int howMany ){
……
}
}
其中一种输出如下:
LYO
VLO
OOLE
Z
GH
参考程序:
private static String randomLetters ( String word, int howMany ){
String outWord = "";
char c;
int random;
word = word.toUpperCase();
for( int i=0; i<howMany; 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, 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; j<theInts[i].length; j++){
if (theInts[i][j] % 2 != 0)
numOdds++;
}
}
return numOdds;
}
【解析】要注意两维数组中不同维调用length的差别
10.在下图所示的小应用程序用户界面上有两个标签(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程序设计","动画设计基础" };
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); (该语句任选)
for(int i = 0; i < 6; i++)
c.addItem(bookstore[i]);
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(i<n){
char sc=s.charAt(i);
if(sc==c) i++;
else {a=a+sc;i++;};
}
return a;
}
【解析】从2008年开始Java等级考试采用了上机形式。因此参加考试的同学需要从指定的文件夹中取出指定的文件进行编程。希望同学打开文件后马上存放到另一个指定文件夹如本题要求的C:\KS文件夹中,以免你的修改影响到原始的文件,有利于你需要时还可以取用最原始的文件。原始文件中已存放了一些文件编程所需要的非考核重点的程序段,内容有多,有少,便于程序调试。
12. 从“C:\素材库”中取出Division.java文件,在该文件基础上进行补充,完成简单除法计算器(只做整数除法,结果也取整数)的小应用程序编写。运行结果见图1,显示结果的文本域设置为不可编辑。当除数为0时,能抛出ArithmeticException异常,并在捕获异常处对异常进行处理,异常处理要求在屏幕上显示“除数不能为0,请重新输入”,结果见图2。
程序完成后将程序编辑及运行所需要的所有文件(包括源程序文件、字节码文件及html文件)存放到C:\KS目录下。
图1
图2
参考程序:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Division extends Applet implements ActionListener{
Label l1,l2,E;
TextField input1,input2,output1;
int a,b,c;
Panel p=new Panel();
public void init() {
setLayout(new BorderLayout());
input1=new TextField(5);
l1=new Label("÷");
input2=new TextField(5);
l2=new Label("=");
output1=new TextField(9);
E=new Label();
E.setAlignment(Label.CENTER);
E.setForeground(Color.red);
p.add (input1);
p.add (l1);
p.add (input2);
p.add (l2);
p.add (output1);
add("North",p);
add("Center",E);
input2.addActionListener(this);
output1.setEditable(false);
}
public void actionPerformed(ActionEvent e) {
a=Integer.parseInt(input1.getText());
b=Integer.parseInt(input2.getText());
E.setText("");
try {
c=a/b;
output1.setText(Integer.toString(c));
}
catch (ArithmeticException e1) {
String Tx="除数不能为0,请重新输入";
E.setText(Tx);
output1.setText("");
}
}
}
【解析】以上参考程序中非加粗的内容就是原始文件所提供的。该题的知识点有两部分:组件的设置,即界面的设计,以及事件的编程
13.从"C:\素材库"中取出Test5.java文件,在该文件基础上进行补充,完成所需内容的编写。
该applet程序中,窗口区有一个"变色"按钮,程序初始状态见下图1,在中心位置有一个半径为30的蓝色圆。(14分)
当单击窗口的任意位置,在鼠标位置将显示一个圆,其颜色和大小随机而变,但半径不能超过150,见下图2;(10分)
当单击"变色"按钮则圆的颜色也会随机变化。颜色有Color.red、Color.blue、Color.yellow。(10分)
以下图示是用Java开发包直接运行的显示效果,仅作参考。
可根据需要在"C:\素材库"中选取Test5.html文件。
程序完成后将程序编辑及运行所需要的所有文件(包括源程序文件、字节码文件)存放到C:\KS目录下。(2分)
图 1 图2
参考程序:
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
public class Test5 extends Applet implements ActionListener,MouseListener{
Button b1;
Color c;
int x,y,r;
public void init() {
b1=new Button("变色");
add(b1);
b1.addActionListener(this);
addMouseListener(this);
c=Color.blue;
x=150;
y=150;
r=30;
}
public void actionPerformed(ActionEvent e){
int k=(int)(Math.random()*3);
switch (k){
case 0:c=Color.yellow;break;
case 1:c=Color.blue;break;
case 2:c=Color.red;break;
}
repaint();
}
public void paint(Graphics e){
e.setColor(c);
e.fillOval(x,y,r,r);
}
public void mousePressed(MouseEvent e){
x=e.getX();
y=e.getY();
r=(int)(Math.random()*150);
int k=(int)(Math.random()*3);
switch (k){
case 0:c=Color.yellow;break;
case 1:c=Color.blue;break;
case 2:c=Color.red;break;
}
repaint();
}
public void mouseClicked(MouseEvent e) { }
public void mouseReleased(MouseEvent e) { }
public void mouseEntered(MouseEvent e) { }
public void mouseExited(MouseEvent e) { }
}
【解析】该题考核的内容与上一题相同。
展开阅读全文